问题描述:

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的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  3. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  4. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  8. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  9. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  10. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

随机推荐

  1. 【做题】HDU6331 Walking Plan——矩阵&分块

    题意:给出一个有\(n\)个结点的有向图,边有边权.有\(q\)组询问,每次给出\(s,t,k\),问从\(s\)到\(t\)至少经过\(k\)条边的最短路. \(n \leq 50, \, q \l ...

  2. 关于msf反弹后门的免杀Tips

    msf是一个很强大的工具,我经常会在渗透用它来反弹shell,不过它生成的反弹后门会被不少杀软kill,这篇文章只是讲讲我在msf中一个简单的免杀小技巧 思路 我以前接触过一款python的远控,其实 ...

  3. CentOS7设置定时任务 每隔30分钟执行一次命令

    ref   https://blog.csdn.net/xiangxianghehe/article/details/78149094 一.安装 crontabs服务并设置开机自启: yum inst ...

  4. java 安装环境

    网上关于win10 jdk安装.配置环境变量的经验有很多,但是按照方法配置后出现了运行javac 报告javac不是内部或外部命令,但是运行java.java-version正常.并不是说那些经验不正 ...

  5. CF981D Bookshelves

    按位贪心+DP的好题qwq 首先看到题目的要求,统计价值的时候的操作是按位与,就要有按位分别计算的意识 开始没意识到结果想了好久还是看了题解才想到 由于统计价值的方式不是加和,所以可能会出现两个较大的 ...

  6. 论文笔记:Capsules for Object Segmentation

    Capsules for Object Segmentation 2018-04-16  21:49:14 Introduction: ----

  7. LOJ 534 花团(线段树+dfs栈)

    题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...

  8. 3、Python迭代器、列表解析及生成器(0530)

    1.动态语言 sys.getrefcount()    //查看对象的引用计数 增加对象的引用计数场景 对象创建时:以赋值的方式,创建变量名的同时就会创建变量 将对象添加进容器时:类似list.app ...

  9. MySQL GROUP BY语句

    GROUP BY 语句根据一个或多个列对结果集进行分组 在分组的列上我们可以使用COUNT.SUM.AVG等函数 SELECT column_name,function(column_name) FR ...

  10. 阿里巴巴数据库连接池 druid配置详解

    一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...