[leetcode] 15. 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.
然后我专门随便写了几段然后看了一下他的测试用例,结果明白题目到底是什么了,题意是说:给你一个非负的整数,然后每一位都分别存在数组的每一位上,比如1984会存成[1,9,8,4]这样。然后你给这个数组加个1,再把这个数按这个数组输出。
这套写法很简单,就是判断计算位是不是9,然后加1,然后进位就行。如果最高位是9还要考虑一下是否加一位就行。代码如下:
class Solution {
public:
vector<int> plusOne(vector<int> &digits)
{
if (digits.at(digits.size() - 1) == 9)
{
int flag = 1;
digits.at(digits.size() - 1) = 0;
for (int i = digits.size() - 2; i >= 0; i--)
{
if (flag)
{
(digits.at(i) == 9) ? (digits.at(i) = 0) : (flag = 0, digits.at(i) += 1);
}
else
{
break;
}
}
if (flag)
{
digits.insert(digits.begin(), 1);
}
}
else
{
digits.at(digits.size() - 1) += 1;
}
return digits;
}
};
拿了一个flag来判断进位,操作都很简单。
[leetcode] 15. Plus One的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- [LeetCode] 15. 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- Leetcode 15. Sum(二分或者暴力或者哈希都可以)
15. 3Sum Medium Given an array nums of n integers, are there elements a, b, c in nums such that a + ...
随机推荐
- python yield 浅析-转载
如何生成斐波那契數列 斐波那契(Fibonacci)數列是一个非常简单的递归数列,除第一个和第二个数外,任意一个数都可由前两个数相加得到.用计算机程序输出斐波那契數列的前 N 个数是一个非常简单的问题 ...
- 源码包的解压 .tar.gz /.tar.bz2的解压
解压方式如下: .tar.gz 格式解压为 tar -zxvf xx.tar.gz .tar.bz2 格式解压为 tar -jxvf ...
- 120. Triangle(Array; DP)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Unity即将内置骨骼动画插件Anima2D
Unity一直在寻找新的方法来帮助开发者,并为他们提供最好的工具.在此我们向大家宣布,Unity将内置流行的骨骼动画插件Anima2D,从2017年1月开始免费供所有Unity开发者使用! 同时也欢迎 ...
- .NET中的文件IO操作实例
1.写入文件代码: //1.1 生成文件名和设置文件物理路径 Random random = new Random(DateTime.Now.Millisecond); ); string Physi ...
- 对象之int介绍
#Auther Bob #--*--conding:utf-8 --*-- #创建两个int的对象,age1和age2 age1 = 10 age2 = int(1) #查看对象的类 print(ty ...
- VR
- [PHP] constant variable
print: 3.13 PI 3.14
- chrome 调试工具的使用
Elements chrome devtools 中 Elements panel 是审查 dom 元素和 css 的, 可以实时修改 dom/css. windows: ctrl + shift + ...
- 什么是Jenkins 以及如何使用?
Jenkins是什么? Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成.集成Jenkins可以用于一些测 ...