返回介绍

solution / 0900-0999 / 0989.Add to Array-Form of Integer / README_EN

发布于 2024-06-17 01:03:32 字数 4772 浏览 0 评论 0 收藏 0

989. Add to Array-Form of Integer

中文文档

Description

The array-form of an integer num is an array representing its digits in left to right order.

  • For example, for num = 1321, the array form is [1,3,2,1].

Given num, the array-form of an integer, and an integer k, return _the array-form of the integer_ num + k.

 

Example 1:

Input: num = [1,2,0,0], k = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

Example 2:

Input: num = [2,7,4], k = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455

Example 3:

Input: num = [2,1,5], k = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021

 

Constraints:

  • 1 <= num.length <= 104
  • 0 <= num[i] <= 9
  • num does not contain any leading zeros except for the zero itself.
  • 1 <= k <= 104

Solutions

Solution 1

class Solution:
  def addToArrayForm(self, num: List[int], k: int) -> List[int]:
    i, carry = len(num) - 1, 0
    ans = []
    while i >= 0 or k or carry:
      carry += (0 if i < 0 else num[i]) + (k % 10)
      carry, v = divmod(carry, 10)
      ans.append(v)
      k //= 10
      i -= 1
    return ans[::-1]
class Solution {
  public List<Integer> addToArrayForm(int[] num, int k) {
    int i = num.length - 1, carry = 0;
    LinkedList<Integer> ans = new LinkedList<>();
    while (i >= 0 || k > 0 || carry > 0) {
      carry += (i < 0 ? 0 : num[i--]) + k % 10;
      ans.addFirst(carry % 10);
      carry /= 10;
      k /= 10;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> addToArrayForm(vector<int>& num, int k) {
    int i = num.size() - 1, carry = 0;
    vector<int> ans;
    for (; i >= 0 || k || carry; --i) {
      carry += (i < 0 ? 0 : num[i]) + k % 10;
      ans.push_back(carry % 10);
      carry /= 10;
      k /= 10;
    }
    reverse(ans.begin(), ans.end());
    return ans;
  }
};
func addToArrayForm(num []int, k int) []int {
  i, carry := len(num)-1, 0
  ans := []int{}
  for ; i >= 0 || k > 0 || carry > 0; i-- {
    if i >= 0 {
      carry += num[i]
    }
    carry += k % 10
    ans = append(ans, carry%10)
    carry /= 10
    k /= 10
  }
  for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
    ans[i], ans[j] = ans[j], ans[i]
  }
  return ans
}
function addToArrayForm(num: number[], k: number): number[] {
  let arr2 = [...String(k)].map(Number);
  let ans = [];
  let sum = 0;
  while (num.length || arr2.length || sum) {
    let a = num.pop() || 0,
      b = arr2.pop() || 0;
    sum += a + b;
    ans.unshift(sum % 10);
    sum = Math.floor(sum / 10);
  }
  return ans;
}
impl Solution {
  pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
    let n = num.len();
    let mut res = vec![];
    let mut i = 0;
    let mut sum = 0;
    while i < n || sum != 0 || k != 0 {
      sum += num.get(n - i - 1).unwrap_or(&0);
      sum += k % 10;
      res.push(sum % 10);

      i += 1;
      k /= 10;
      sum /= 10;
    }
    res.reverse();
    res
  }
}

Solution 2

function addToArrayForm(num: number[], k: number): number[] {
  const n = num.length;
  const res = [];
  let sum = 0;
  for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
    sum += num[n - i - 1] ?? 0;
    sum += k % 10;
    res.push(sum % 10);
    k = Math.floor(k / 10);
    sum = Math.floor(sum / 10);
  }
  return res.reverse();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文