【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, ...
随机推荐
- nginx(一)- 初识
前言 这里呢,开始我的nginx学习记录.关于nginx是什么?为什么?有什么好处?我就不说了,说的肯定没有网上其他人说的那么细.我在这里就记录我自己学习nginx的过程.只说怎么做,让我们快速用起来 ...
- Spring 操作 jdbc 链接数据库
1. 新建资源文件 db.properities jdbc.user=root jdbc.password=root jdbc.driverClass=com.mysql.jdbc.Driver jd ...
- linux下忘记mysql的root密码
一.处理方案 #1. 结束当前正在运行的mysql进程 /etc/init.d/mysql stop #2. 用mysql安全模式运行并跳过权限验证 mysqld_safe --user=mysql ...
- 剑指offer——面试题11:旋转数组的最小数字
#include"iostream" using namespace std; int GetMinNumber(int *data,int len) { ,right=len-, ...
- PIE SDK矢量数据的修改
1.功能简介 目前PIE SDK支持矢量数据的修改或删除,下面对矢量数据的投影转换功能进行介绍. 2.功能实现说明 2.1. 矢量数据的修改 2.2. 实现思路及原理说明 第一步 获取需要修改的矢量数 ...
- 转 LIST INCARNATION OF DATABASE
incarnation在英文中是“化身”的意思. 那么在oracle中,它又是什么意思呢?有什么作用呢? 我们看一些基本概念 Current Incarnation(当前化身):数据库当前正在使用的化 ...
- linux下统计文本行数的各种方法(二)
上一篇讲的都是统计单个文件的方法,直接在命令行执行就可以.现在试试脚本的方式,统计多个文件的行数 一.统计目录下所有文件的文件数及所有行数 脚本暂时命名为count.sh,代码如下: #!/bin/b ...
- ubuntu安装卸载软件
sudo apt-get remove nagios3 #卸载软件 sudo apt-get autoremove #卸载依附软件包 rpm格式 安装:rpm -ivh *** 查看:rpm -q * ...
- 开源TSDB简介--Druid
开源TSDB简介--Druid Druid是一个以Java编写的开源分布式列式数据存储. Druid的目标是快速提取大量事件数据,并提供低延迟的查询. 德鲁伊的名字来源于许多角色扮演游戏中的变形德鲁伊 ...
- bzoj 5305: [Haoi2018]苹果树
Description Solution \(n\) 个点的二叉树的方案数是 \(n!\) 证明十分显然:新加入的点占掉了 \(1\) 个位置,新加了 \(2\) 个位置,那么多出来一个位置,所以第 ...