这道题其实让我意识到了我的英文水平还有待加强。。。。

题目如下:

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的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. [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 ...

  3. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  4. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  5. 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 ...

  6. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  7. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  8. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

  9. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  10. Leetcode 15. Sum(二分或者暴力或者哈希都可以)

    15. 3Sum Medium Given an array nums of n integers, are there elements a, b, c in nums such that a +  ...

随机推荐

  1. express + mongodb 搭建一个简易网站 (四)

    express + mongodb 搭建一个简易网站 (四) 目前网站整体页面都已经能全部展示了,但是,整个网站还有两个块需要做完才能算完整,一个连接数据库,目前网站上的数据都是抓取的本地假数据,所以 ...

  2. IN_sales_order带后续P IN_ITEM_SITE带P\SP\TP DUMMY

    IN_sales_order带后续P IN_ITEM_SITE带P\SP\TP DUMMY SAP_MATERIAL_SO 处理材料订单缺少BOM,ROUTING信息

  3. Java的indexOf返回的是第一个匹配到的字符的索引位置,substring(a,b)获得字符串的一部分内容

    背景:我要实现一个功能,需要匹配两个字符串是否有相同的字符,所以就写了下面一个小方法,定义两个字符串a和b,循环遍历 b,如果a中有b的子串就将匹配数量num+1   遇到的问题:开始判断字符串中是否 ...

  4. MySQL+Navicat for MySQL安装

    一.安装MySQL 1.下载MySQL http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-winx64.zip 2.安装 2.1解压安装包 ...

  5. Web标准:四、纵向导航菜单及二级弹出菜单

    Web标准:四.纵向导航菜单及二级弹出菜单 知识点: 1.纵向列表 2.标签的默认样式 3.css派生选择器 4.css选择器的分组 5.纵向二级列表 6.相对定位和绝对定位   1)纵向列表 可以看 ...

  6. js base64转二进制

    base64 编码规则 1.把3个字符变成4个字符.2.每76个字符加一个换行符.3.最后的结束符也要处理. 转换前 11111101, 11111111, 11111111 (二进制) 转换后 00 ...

  7. overflow: scroll

    overflow: scroll在安卓5.0的情况下,不论内容是否填满屏幕,都会强制解析出滚动条,所以最好是使用overflow: auto

  8. Python format 格式化函数。

    Python format 格式化函数  Python 字符串 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 ...

  9. 43. Multiply Strings (String)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  10. ECMAScript5新特性之isSealed、seal

    封闭对象后: 1 不能增加属性. 2 不能删除属性. 3 可以修改属性.(赋值) 4 不能修改属性描述符.(抛异常) var fruit = { name : '苹果', desc : '红富士' } ...