leetcode -- 66. Plus One
时间:2019-02-11
本文章向大家介绍leetcode -- 66. Plus One,主要包括leetcode -- 66. Plus One使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
题目描述
题目难度:Easy
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
- Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
- Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
AC代码1
class Solution {
public int[] plusOne(int[] digits) {
int len = digits.length;
int carry = 1, tmp = 0;
int[] res = digits;
for(int i = len - 1; i >= 0; i--){
tmp = carry;
carry = (digits[i] + tmp) / 10;
digits[i] = (digits[i] + tmp) % 10;
}
if(carry != 0){
res = new int[len + 1];
res[0] = carry;
for(int i = 0;i < len;i++) res[i + 1] = digits[i];
}
return res;
}
}
AC代码2
大神的代码,经过了优化,很优秀。
public class Solution {
public int[] plusOne(int[] digits) {
for(int i = digits.length - 1;i >= 0;i--){
if(digits[i] < 9){
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] temp = new int[digits.length + 1];
temp[0] = 1;
return temp;
}
}
- 如何使用管道操作符优雅的书写R语言代码
- 第四周编程作业(二)-Deep Neural Network for Image Classification: ApplicationDeep Neural Network for Image Cl
- 第四周编程作业(一)-Building your Deep Neural Network: Step by StepBuilding your Deep Neural Network: Step by
- Python数据抓取与可视化实战——网易云课堂人工智能与大数据板块课程实战
- 商务图表案例——仿经济学人分组漏斗图~
- 第二周编程作业 -Logistic Regression with a Neural Network mindsetLogistic Regression with a Neural Network
- 左手用R右手Python系列——任务进度管理
- 左右用R右手Pyhon系列——趣直播课程抓取实战
- 第三周编程作业-Planar data classification with one hidden layerPlanar data classification with one hidden l
- 用R语言照葫芦画瓢撸了一个简易代理~
- 左手用R右手Python——CSS网页解析实战
- R语言数据抓取实战——RCurl+XML组合与XPath解析
- Python网络数据抓取实战——Xpath解析豆瓣书评
- 左手用R右手Python系列17——CSS表达式与网页解析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法