【Leetcode】【Easy】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.
解题方法1:
模拟运算过程,从低位数字到高位数字计算,新建一个变量记录进位情况。
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int len = digits.size();
int sig = ;
digits[len-] += ;
for (int i=len-; i>=; --i) {
digits[i] += sig;
sig = digits[i] / ;
digits[i] = digits[i] % ;
} if (sig == ) {
digits.insert(digits.begin(), );
} return digits;
}
};
更聪明的方法(应该是最棒的方法):
1、计算过程其实问题就是两步,从最低位开始,进行如下循环:如果不是9,则此位++,break;如果是9,则此位为0,continue;
2、当最高位满10需要进位时:最高位满10只有一种情况,即99..99+1,所以在第一条方法基础上,计算后判断首位(最高位)是否是0,是0则表示最高位发生了进位。将最高位置1,最低位补充一位0;
3、更多的细节:如果在1过程中遇到了某位不是9,则不必在break后判断首位是不是0,因此可以直接返回;
既然如此,只要程序在1过程中没有返回,而运行到了循环外,就说明首位发生了进位,可以不用判断,直接进行步骤2。
具体代码:
class Solution {
public:
vector<int> plusOne(vector<int> &digits)
{
int n = digits.size();
for (int i=n-; i>=; --i)
{
if (digits[i] == ) {
digits[i] = ;
} else {
digits[i]++;
return digits;
}
} digits[] =;
digits.push_back(); return digits;
}
};
附录:
vector、list等特性及使用注意。
【Leetcode】【Easy】Plus One的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
随机推荐
- 剑指offer——面试题7:重建二叉树
// 面试题7:重建二叉树 // 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输 // 入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1, // 2, ...
- java中的集合:继承关系和简介
1.继承关系图 2.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collect ...
- Vue省市区三级联选择器V-Distpicker的使用
Vue省市区三级联选择器V-Distpicker的使用 最近用的Vue+Element UI时,有些地方需要用到省市区三联选择器,网上安装并尝试了多种类似的插件,但都因为无法正常实现或是没有眼缘而弃用 ...
- (转)Linux系统sersync数据实时同步
Linux系统sersync数据实时同步 原文:http://blog.csdn.net/mingongge/article/details/52985259 前面介绍了以守护进程的方式传输或同步数据 ...
- 几个免费 IP 归属地查询 API
1.淘宝:同个IP不能连续查询,需要时间间隔 http://ip.taobao.com/service/getIpInfo.php?ip=114.114.114.114 返回结果 { "co ...
- 中文输入法无法在 QtCreator(Linux) 中输入汉字
中文输入法无法在 QtCreator(Linux) 中输入汉字 环境 system: Deepin 15.7 Qt Creator 4.7.0 (GCC 5.3.1) 解决方法 下载 fcitx-qt ...
- [转]EntityFramework中常用的数据修改方式
本文转自:http://blog.csdn.net/itmaxin/article/details/47662151 上一篇文章里提到了 EntityFramework中常用的数据删除方式,那么修改对 ...
- NPOI之C#下载Excel
Java中这个类库叫POI,C#中叫NPOI,很多从Java一直到.Net平台的类库为了区别大部分都是在前面加个N,比如Hibernate和NHibernate. npoi下载地址 一.使用NPOI下 ...
- avalon实现分页组件
前言 分页组件比较常见,但是用avalon实现的见的不多,这个分页组件,可以适配2种分页方式, 第一种是每次点击下一页,就请求一次后台,并返回当页数据和总条数,我称之为假分页: 第二种是一次性把所有数 ...
- 使用waitfor 语句
aitfor语句用于延迟后面语句的执行,可以指定延迟时间长度是具体的时间.参考下面的语句: waitfor delay ’00:01:15’ print N’到时间了’ --也可以不加N 字符串 ...