首页 > 其他 > 详细

leetcode 66. Plus One(高精度加法)

时间:2016-04-17 00:28:58      阅读:307      评论:0      收藏:0      [点我收藏+]

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

题解:简单的高精度加法,要注意[9]这种输入,要多一位。

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int add=1;
        int n=digits.size();
        stack<int>st;
        vector<int>ans(0);
        while(add){
            if(n==0){
                st.push(1);
                break;
            }
            int t=(digits[n-1]+add)%10;
            add=(digits[n-1]+add)/10;
            st.push(t);
            n--;
        }
        if(n){
            while(n--){
                st.push(digits[n]);
            }
        }
        while(!st.empty()){
            ans.push_back(st.top());
            st.pop();
        }
        return ans;
    }
};

 

leetcode 66. Plus One(高精度加法)

原文:http://www.cnblogs.com/zywscq/p/5399783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!