[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 + ...
随机推荐
- Hadoop 初始化系统
hadoop namenode -format 或者 hdfs namenode -format 2.执行hadoop sbin 目录下的 start-dfs.sh start-yarn.sh3.查看 ...
- samtools软件的使用
1)samtools简介--------------------------------------------------------------------------背景:前面我们讲过sam/b ...
- 【英宝通Unity4.0公开课学习 】(一)资源管理
经过多次面试后发现自己对Unity3D的框架缺乏一个整体的认识. 而前面由于离职等原因总是忙于修修补补,疲于奔命,感觉相当疲惫. 还好,后来经过调整,开始淡定了起来.得特别感谢一本书哇:<高效人 ...
- 【C基础】之联合体
1.联合体 联合体(union)与结构体(struct)有一些相似之处.但两者有本质上的不同.在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和.而在联合体中,各成员共享一段内 ...
- python之类之多继承
class A(object): def test_func(self): print("from A") class B(A): pass # def test_func(sel ...
- TextBox 宽度无效
TextBox 宽度设置 Width="550px" 在IE10.1011下无效 改为用 style="width:550px; height:200px;" ...
- 2-Qt关闭子窗口时执行特定代码
https://blog.csdn.net/naibozhuan3744/article/details/82689434 本文主要总结在关闭qt的QWidget子窗口瞬间,执行特定代码.由于主窗口关 ...
- 【转】HttpApplication的认识与加深理解
原文:http://www.cnblogs.com/whtydn/archive/2009/10/16/1584584.html HttpApplication对象是经由HttpApplication ...
- Spark cache、checkpoint机制笔记
Spark学习笔记总结 03. Spark cache和checkpoint机制 1. RDD cache缓存 当持久化某个RDD后,每一个节点都将把计算的分片结果保存在内存中,并在对此RDD或衍生出 ...
- PS抠发丝技巧 「选择并遮住…」
PS抠发丝技巧 「选择并遮住…」 现在的海报设计,大多数都有模特MM,然而MM的头发实用太多了,有的还飘起来…… 对于设计师(特别是淘宝美工)没有一个强大.快速.实用的抠发丝技巧真的混不去哦.而PS ...