leetcode 66. Plus One

原创 2016年05月31日 15:30:31

题目

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.

public class Solution {
  public int[] plusOne(int[] digits) {

            int j=digits.length-1;
            digits[j]+=1;
            while(j>0){
                if(digits[j]>9){
                    digits[j]=digits[j]-10;
                    digits[j-1]+=1;
                }

                j--;
            }


            if(digits[0]<10){
                return digits;
            }else
            {
                 int carry=0;
                 if(digits[0]>9){
                        digits[0]=digits[0]-10;
                        carry=1;
                 }
                 int[] ret=new int[digits.length+1];
                 ret[0]=carry;
                 for(int i=1;i<ret.length;i++){
                     ret[i]=digits[i-1];
                 }
                 return ret;

            }

        }
}

相关文章推荐

LeetCode --- 66. Plus One

题目链接:Plus One Given a non-negative number represented as an array of digits, plus one to the number...
  • makuiyu
  • makuiyu
  • 2015年03月19日 21:53
  • 6600

leetcode 66. Plus One

leetcode 66. Plus One 题目大意:给定一个非负整数,存储在数组中,把这个数字加1,这个数的最高位在最前面。 解题思路:如果这个数的最低位为小于9,直接加到最低位去 如果这个数的最低...

leetcode-66. Plus One

Given a non-negative number represented as an array of digits, plus one to the number. The digits...
  • qj30212
  • qj30212
  • 2016年09月02日 00:34
  • 163

[C语言][LeetCode][66]Plus One

题目Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...
  • Timsley
  • Timsley
  • 2015年08月11日 08:58
  • 1072

Plus One - LeetCode 66

题目描述:Given a non-negative number represented as an array of digits, plus one to the number. The d...
  • bu_min
  • bu_min
  • 2015年04月28日 22:21
  • 266

<LeetCode OJ> 66. Plus One

66. Plus One My Submissions Question Total Accepted: 77253 Total Submissions: 242942 Difficulty:...

LeetCode66: Plus One

Given a non-negative number represented as an array of digits, plus one to the number. The digits a...

[LeetCode] S66 Plus One

Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

LeetCode笔记:66. Plus One

用数组表示多位数字并加一

LeetCode 66 — Plus One(C++ Java Python)

题目:http://oj.leetcode.com/problems/plus-one/ Given a non-negative number represented as an arra...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 66. Plus One
举报原因:
原因补充:

(最多只允许输入30个字)