首页 > 其他 > 详细

66 Plus One

时间:2015-07-01 14:15:22      阅读:143      评论:0      收藏:0      [点我收藏+]

66 Plus One

链接:https://leetcode.com/problems/plus-one/
问题描述:
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.

Hide Tags Array Math

看半天看不懂这个问题要求什么,后来百度才明白,是把一个数字存在一个数组中。每一位的数字存在一个数组单元中,数组0索引位置存最高位。比如 21这个数字,存在arr数组中时arr[0]=2,arr[1]=1;然后求数组表达的数字加1后的结果。明白了题目应该就很容易解了。关键注意进位问题。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

66 Plus One

原文:http://blog.csdn.net/efergrehbtrj/article/details/46708913

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