Plus One leetcode java
问题描述:
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.
分析:非负整数,存储在数组中。the most significant digit 最高位存储在列表的第一个,例如:98, array[0] 存储9,array[1]存储8。
定义变量carry存储进位,遍历数组,每次修改digits[i] 与 carry
public int[] plusOne(int[] digits) {
if(digits == null || digits.length == 0)
return null;
int len = digits.length;
int carry = 0; //进位,只借用一个变量,不需要数组
for(int i = len - 1; i >= 0; i--){
if(i == len - 1){
carry = (digits[i] + 1 ) / 10; //进位
digits[i] = (digits[i] + 1 ) % 10; //本位
} else {
int digit = (digits[i] + carry) % 10; //本位
carry = (digits[i] + carry) / 10; //进位
digits[i] = digit;
}
}
if(carry == 0)
return digits;
else {
int[] new_digits = new int[len + 1];
for (int i = 1; i < new_digits.length; i++) {
new_digits[i] = digits[i - 1];
}
new_digits[0] = carry;
return new_digits;
}
}
Plus One leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
随机推荐
- (转)开源项目miaosha(上)
石墨文档:https://shimo.im/docs/iTDoZs4CVfICgSfV/ (二期)19.开源秒杀项目miaosha解读(上) [课程19]几张图.xmind0.6MB [课程19]开源 ...
- office完全卸载
第一步:先暂停office服务,再通过 控制面板--卸载程序 --卸载office应用 第二步:通过office_move(自己命名的工具)软件卸载 工具分享:https://pan.baidu. ...
- html页面使用前端框架布局时,弹出子页面等情况若出现布局混乱,可能是回发导致
解决方法:需要带调用 弹框 那里 写一段return false防止回发
- LuoguP3674 小清新人渣的本愿 && BZOJ4810: [Ynoi2017]由乃的玉米田
题目地址 小清新人渣的本愿 [Ynoi2017]由乃的玉米田 所以这两题也就输出不一样而已 题解 这种lxl的题还是没修改操作的题基本就是莫队 分开考虑每个询问 1.减法 \(a-b=x⇒a=b+x\ ...
- 论文笔记:Semantic Segmentation using Adversarial Networks
Semantic Segmentation using Adversarial Networks 2018-04-27 09:36:48 Abstract: 对于产生式图像建模来说,对抗训练已经取得了 ...
- delimiters 插值 选项
delimiters差值选项vue默认是{{}},这个选项可以把这个差值形式进行改变,这里讲,默认插值改成${} html <div id="app"> <div ...
- [转载]解决linux 下多线程错误 undefined reference to `sem_init'
转自:https://blog.csdn.net/yzycqu/article/details/7396498?utm_source=copy 解决linux 下多线程错误 undefined ref ...
- -第1章 HTMLCSS方法实现下拉菜单
中英文的自动换行问题 把下面代码中的 javascript 改成 子菜单1 试试, 如果英文的话宽度会自动撑开, 用中文不会, 而直接转行下来. <ul> <li><a ...
- Lintcode449-Char to Integer-Naive
Description Convert a char to an integer. You can assume the char is in ASCII code (See Definition, ...
- mark mem
韦达定理 http://baike.baidu.com/link?url=M45ozZEnQ4BtKD7l22WWgQuGnmDYV7TFynQcPEO2Tt8leYGhyEa1flt-RM34NG4 ...