加一

/*
问题描述:
给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。该数字按照大小进行排列,最大的数在列表的最前面。
日期:2017-6-23

作者:syt

思路:按位相加,注意进位

*/


#include <iostream>
#include <vector>
using namespace std;
class AddOne{
public:
	/**
	* @param digits a number represented as an array of digits
	* @return the result
	*/
	vector<int> plusOne(vector<int>& digits) {
		// Write your code here
		bool is = false;
		int i = digits.size() - 1;
		digits[i]++;
		while (i >= 0)
		{
			if (is)
			{
				digits[i]++;
				is = false;
			}
			if (digits[i] == 10)
			{
				if (i == 0)
				{
					digits[i] = 1;
					digits.push_back(0);
				}
				else
					digits[i] = 0;
				is = true;
				i--;
			}
			else
				break;
			
		}
		return digits;
	}
};


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值