Easy 66题 Plus One
2016-09-22 11:29
274 查看
question:
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.
Solution:
花了好久时间,情况有点复杂,用的方法也比较笨,最后调试出来了。。。正在试着改进
然后看了discussion 发现了一个很好的解决方法
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.
Solution:
花了好久时间,情况有点复杂,用的方法也比较笨,最后调试出来了。。。正在试着改进
public class Solution { public int[] plusOne(int[] digits) { int len=digits.length; int[] newNumber = new int [len+1]; int flag=0; int jin=1; if(digits[len-1]!=9) { digits[len-1]++; } else { for(int i=len-1;i>=0;i--) { if((digits[i]==9&&i!=0&&jin==1)) { newNumber[i]=0; digits[i]=0; newNumber[i-1]=digits[i-1]+1; digits[i-1]=digits[i-1]+1; jin=0; } else if(digits[i]==10&&i!=0) { newNumber[i]=0; digits[i]=0; newNumber[i-1]=digits[i-1]+1; digits[i-1]=digits[i-1]+1; jin=1; } else if(digits[i]==10&&i==0||len==1) { for(int j=len;j>=0;j--) { if(j==0) newNumber[j]=1; else if(j==1) newNumber[j]=0; else newNumber[j]=digits[j-1]; } flag=1; } else { jin=0; } } } if(flag==1) return newNumber; else return digits; } }
然后看了discussion 发现了一个很好的解决方法
<pre class="markdown-highlight" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 9.5px; margin-top: 1rem; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; word-wrap: break-word; color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); border: none; border-radius: 0px; max-height: 600px;"><code class="hljs cs" style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace;font-size:undefined; padding: 0.5em; background: rgb(248, 248, 248); border-radius: 0px; display: inline-block; overflow-x: auto; word-wrap: normal; min-width: 100%;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span>[] <span class="hljs-title" style="box-sizing: border-box; color: rgb(153, 0, 0); font-weight: bold;">plusOne</span>(<span class="hljs-params" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span>[] digits</span>) </span>{ <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span> n = digits.length; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span> i=n<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">-1</span>; i>=<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">0</span>; i--) { <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span>(digits[i] < <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">9</span>) { digits[i]++; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> digits; } digits[i] = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">0</span>; } <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span>[] newNumber = <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span> [n+<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">1</span>];//new的数组都为0 newNumber[<span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">0</span>] = <span class="hljs-number" style="box-sizing: border-box; color: rgb(0, 128, 128);">1</span>; <span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span> newNumber; }</code>
相关文章推荐
- Leet Code OJ 66. Plus One [Difficulty: Easy]
- Easy 14 Plus One(66)
- 【LeetCode】(66)Plus One (Easy)
- LeetCode-66-Plus One(水题/数值计算)-Easy
- 66. Plus One 难度:easy
- Easy-题目30:66. Plus One
- [Leetcode 66, Easy] Plus One
- Leetcode 66. Plus One (Easy) (cpp)
- LeetCode】66.Plus One(easy)解题报告
- LeetCode66. Plus One-python(easy)
- 66. Plus One [easy] (Python)
- LeetCode【66】 Plus One
- 2018.3.12 leetcode打卡 #66 plus one
- 66. Plus One
- LeetCode 66 Plus One(加一)(vector)
- 【LeetCode】66. Plus One
- 66. Plus One
- 66. Plus One
- [Leetcode 4] 66 Plus One
- 66. Plus One (计算数字加1后的结果)