Code17 plus one

topic

leetcode66. Add one
Given a non-negative integer represented by a non-empty array of integers, add one to the number.
The highest digit is stored in the first position of the array, and each element in the array only stores a single number.
You can assume that in addition to the integer 0, this integer does not start with a zero.

Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The input array represents the number 123.

Example 2:
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The input array represents the number 4321.

Example 3:
Input: digits = [0]
Output: [1]

提示:
1 <= digits.length <= 100
0 <= digits[i] <= 9

Code

  • C code
  • Ideas:
    • The C code involves malloc, so you need to confirm the size of the requested resource first.
    • Only when the numbers are all 9, returnSize = digitsSize + 1, otherwise returnSize = digitsSize.
    • In the first case, the first digit of the result is 1, and the other digits are all 0, and the result can be returned directly.
    • In the second case, just traverse the processing from the end to the front. The logic of addition: use modulo for standard position and divide for carry.
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#include <malloc.h>
#include <string.h>
int* plusOne(int* digits, int digitsSize, int* returnSize){
    
    
  *returnSize = digitsSize;

  // 只有全9才会比digitsSize多一位
  bool need_one_more = true;
  for (int i = 0; i < digitsSize; ++i){
    
    
    if (9 != digits[i]) {
    
    
      need_one_more = false;
      break;
    }
  }
  if (need_one_more){
    
    
    *returnSize = digitsSize + 1;
  }

  int* res = (int*)malloc(sizeof(int) * (*returnSize));
  // 第1种情况
  if (need_one_more){
    
    
    res[0] = 1;
    for (int i = 1; i < *returnSize; ++i){
    
    
      res[i] = 0;
    }
    return res;
  }

  // 第2种情况
  int temp = 1;
  for (int i = digitsSize - 1; i >=0; --i){
    
    
    res[i] = (digits[i] + temp) % 10;
    temp = (digits[i] + temp) / 10;
  }
  return res;
}
  • C++ code
    • Does not involve resource application issues, just use pre-insertion in the vector.
#include <vector>
using namespace std;

class Solution {
    
    
 public:
  vector<int> plusOne(vector<int>& digits) {
    
    
    int temp = 1;
    vector<int> res;
    for(auto it = digits.rbegin(); it != digits.rend(); ++it){
    
    
      res.emplace(res.begin(), (*it + temp) % 10);
      temp = (*it + temp) / 10;
    }
    // 注意最后可能的进位
    if (temp){
    
    
      res.emplace(res.begin(), temp);
    }
    return res;
  }
};

test

#include <iostream>

template <typename C>
void print(C c, int size, char* s) {
    
    
  cout << s << ": ";
  for (int i = 0; i < size; ++i){
    
    
    cout << c[i] << " ";
  }
  cout << endl;
}

int main() {
    
    
  {
    
    
    int digits[] = {
    
    9};
    int n = sizeof(digits) / sizeof(digits[0]);
    print(digits, n, "digits");
    int size;
    int* output = plusOne(digits, n, &size);
    print(output, size, "output");
  }
  {
    
    
    vector<int> digits = {
    
    9};
    print(digits, digits.size(), "digits");
    Solution s;
    auto output = s.plusOne(digits);
    print(output, output.size(), "output");
  }
  cin.get();
  return 0;
}
  • result
digits: 9
output: 1 0
digits: 9
output: 1 0

Guess you like

Origin blog.csdn.net/luoshabugui/article/details/109680169