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就可以,一开始尽然还没看懂题目,真是糗,代码如下:

     vector<int> plusOne(vector<int>& digits) {
reverse(digits.begin(), digits.end());
int sz = digits.size();
int flag = ;
for(int i = ; i< sz; ++i){
int val = digits[i] + flag;
digits[i] %= val;
flag = val/;
}
if(flag != )
digits.push_back(flag);
reverse(digits.begin(), digits.end());
return digits;
}

下面是用java写的版本,跟上面还是不太一样的:

 public class Solution {
public int[] plusOne(int[] digits) {
int carry = 1;
for(int i = digits.length-1; i >= 0; --i){
int val = digits[i] + carry;
carry = 0;
digits[i] = val%10;
carry = val/10;
}
if(carry > 0){
int [] ret = new int[digits.length + 1];
ret[0] = carry;
System.arraycopy(digits, 0, ret, 1, digits.length);
return ret;
}
return digits;
}
}

LeetCode OJ:Plus One (加1)的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

随机推荐

  1. Linux环境安装nodejs

    安装node 去官网下载nodejs 根据下载的文件可以看出它的压缩方式是.xz的方式,所以不能直接使用linux命令tar直接下载. xz -d node-v10.6.0-linux-x64.tar ...

  2. Swap 2 Variables in Python

    In Python, it's concise, easy and faster to swap 2 variables compared in other Programming languages ...

  3. iOS消息推送原理

    推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服务端[消息的发起者]): ...

  4. Levenshtein距离

    Levenshtein Distance,又称Edit Distance,在自然语言处理中有着广泛的应用.Levenshtein  Distance 指的是两个字符串之间,由一个转换成另一个所需的最少 ...

  5. poj2996

    /*排序函数要写对,优先级:K,Q,R,B,N,P 白色的:如果优先级一样,那么按照行数大的优先,如果行数一样,那么列数小的优先 黑色的:如果优先级一样,那么按照行数小的优先,如果行数一样,那么列数小 ...

  6. Spring 之自动化装配 bean 尝试

    [Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...

  7. Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中

    Tomcat在windows服务器下,将tomcat控制台日志记录到日志文件中 在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail  ...

  8. java:java静态代理与动态代理简单分析

    java静态代理与动态代理简单分析 转载自:http://www.cnblogs.com/V1haoge/p/5860749.html 1.动态代理(Dynamic Proxy) 代理分为静态代理和动 ...

  9. 安装专业版的linux的方法 图解安装专业版的linux

    按装一个linux系统其实很简单,不再像以前那样光光盘就好些个,一不小心还又可能装错,实在也是一个大问题.现在好了基本上都是简单安装+网络升级先安装主要的后面如果需要什么在装什么?大大简化了流程.不行 ...

  10. EF Code-First 学习之旅 级联删除

    级联删除是当删除主记录的时候会自动删除依赖的记录或者设置外键属性为null public class Student { public Student() { } public int Student ...