LeetCode OJ 66. 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,其他位为0,长度加一的新数组。代码如下:
public class Solution {
public int[] plusOne(int[] digits) {
if(digits==null || digits.length==0) return digits;
int i;
for(i = digits.length - 1; i >= 0; i--){
if(digits[i] + 1 < 10){
digits[i] += 1;
break;
}
else digits[i] = 0;
}
if(i < 0){
int[] newdigits = new int[digits.length+1];
newdigits[0] = 1;
return newdigits;
}
return digits;
}
}
LeetCode OJ 66. Plus One的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- JavaScript eval() 为什么使用eval()是一个坏主意 什么时候可以使用eval()
---------------------------------------------------------------------------------------------------- ...
- Managed Switch: Confs
shortcuts: c-w: delete word before c-a: move to first char c-y: delete everything after cursor c-z: ...
- centos7下安装JDK 1.7
选则64位的rpm包下载:jdk-7u25-linux-x64.rpmrpm –ivh 'jdk安装包路径' 环境变量配置 # exportJAVA_HOME=<jdk-install-dir& ...
- 图片验证码自动识别,使用tess4j进行验证码自动识别(java实现)
1.下载tess4j依赖的jar包,maven中央库地址:<dependency> <groupId>net.sourceforge.tess4j< ...
- openstack私有云布署实践【13.2 网络Neutron-compute节点配置(办公网环境)】
所有compute节点 下载安装组件 # yum install openstack-neutron openstack-neutron-linuxbridge ebtables ipset -y ...
- [妙味Ajax]第一课:原理和封装
知识点总结: ajax是异步的javascrip和xml,用异步的形式去操作xml 访问的是服务端,即https://127.0.0.1/ 或者 https://localhost 1.创建一个aja ...
- CSS3的基础知识点
面临找工作之际,又将CSS3的基础知识撸了一把,做了相应的笔记,主要是方便自己查阅,参考的是W3C的知识. 1.CSS背景 (1).background-size 属性 background-s ...
- nodejs querystring
Query String querystring.parse(str[, sep][, eq][, options]) querystring.stringify(obj[, sep][, eq][, ...
- Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...
- 2016弱校联盟十一专场10.2——Around the World
题目链接:Around the World 题意: 给你n个点,有n-1条边,现在这n-1条边又多增加了ci*2-1条边,问你有多少条欧拉回路 题解: 套用best定理 Best Theorem:有向 ...