Leetcode之67. Add Binary 66. Plus One

67. Add Binary

iven two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:
Input: a = “11”, b = “1”
Output: “100”

Two pointers are used to point to the end of a and b respectively, and then one character is taken out each time and converted to a number. If the character cannot be taken out, it is processed as 0, and then the carry is defined, initialized to 0, and the three are added up. The remainder is the number of the current digit, and the quotient of 2 is the value of the current carry. Remember to judge the carry at the end. If it is 1, add a 1 to the top of the result.

class Solution {
public:
    string addBinary(string a, string b) {
        string res="";
        int m=a.size()-1,n=b.size()-1,carry=0;
        while(m>=0||n>=0){
            int p = m >= 0 ? a[m--] - '0' : 0;
            int q = n >= 0 ? b[n--] - '0' : 0;
            int sum = p + q + carry;
            res = to_string(sum % 2) + res;
            carry = sum / 2;
        }
        return carry == 1 ? "1" + res : res;
    }
};

66. Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Method 1:
You can use a similar approach to the previous Add Binary. We initialize carry to 1, and then add a 0 to digits. The processing method is the same as the previous question.

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        if (digits.empty()) return digits;
        int carry = 1, n = digits.size();
        for (int i = n - 1; i >= 0; --i) {
            if (carry == 0) return digits;
            int sum = digits[i] + carry;
            digits[i] = sum % 10;
            carry = sum / 10;
        }
        if (carry == 1) digits.insert(digits.begin(), 1);
        return digits;
    }
};

Method 2:
Store the numbers on each bit of a number into a one-dimensional vector, the highest bit is at the beginning, we need to add one to this number, that is, add one to the last number, if the last number is 9, then There will be a carry problem, and if the digit on the previous bit is still 9, you need to continue to carry forward. The specific algorithm is as follows: first determine whether the last digit is 9, if not, directly add one and return, if so, assign 0 to the digit, and then continue to check the previous digit. In the same way, until the first digit is checked. If the first digit is originally 9, and a new digit will be generated after adding one, then the last thing to do is to check whether the first digit after the operation is 0, and if so, add a 1 at the top.

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        if(digits.empty())
            return digits;
        for(int i=digits.size()-1;i>=0;i--){
            if(digits[i]==9)
                digits[i]=0;
            else{
                digits[i]+=1;
                return digits;
            }
        }
        if(digits[0]==0)
            digits.insert(digits.begin(),1);
        return digits;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600054&siteId=291194637