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 ...
随机推荐
- .net中读取xml文件中节点的所有属性信息
功能描述: 将数据以xml的格式记录成配置文件,需要获取配置文件中的数据时,则获取对应的配置文件,读取配置文件里对应节点的所有属性. 逻辑实现: 1.将数据配置好在xml文件中. 2.获取xml文件中 ...
- appium使用真机做安卓移动端自动化测试
1.PC往手机上安装apk文件:adb install apk文件完整路径 2.获取app包名和activity的命令:使用aapt;aapt是sdk自带的一个工具,在sdk\builds-tools ...
- android源码查看所有分支切换分支
cd .repo/manifests git branch -a repo init -b android-4.1.2_r1 repo sync
- 写Java程序的三十个基本规则【新手必读】
(1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母.例如: ThisIsAClassName this ...
- linux虚拟机正常安装完成后获取不到IP的解决办法
通常正常情况下安装完linux虚拟机,只需要使用桥接并修改配置文件/etc/sysconfig/network-scripts/ifcfg-eth0,将如下参数值改为如下: ONBOOT=yes NM ...
- 永洪BI配置测试及遇到的一些问题
1.连oracle 11g数据库遇到密码不能验证通过. 在11g中, 数据库默认密码的大小写是敏感的,jdbc在给oracle密码时, 会将其变成大写.其他个别情况也会出现变成小写的. 为统一,去除o ...
- 第二章App框架设计与重构
response标准格式: { "isError":false, "errorType": 0, "errorMessage": " ...
- 实验吧Web-FALSE
笔记: PHP函数isset(): 检测变量是否设置 只能用于变量,传递任何其它参数都将造成解析错误.若想检测常量是否已设置,可使用 defined() 函数 格式: isset ( mixed v ...
- Node.js:模块
概要:本篇博客主要介绍node.js的模块 1.创建模块 在node.js中创建一个模块非常简单,因为一个文件就是一个模块.我们只需要明白如何从其他文件中获取这个模块.Node.js提供了 expor ...
- 区块链Readme.md
#Welcome to Azure Blockchain Projects This Repo host Azure related blockchain and distributed ledger ...