zoukankan      html  css  js  c++  java
  • 66. Plus One

    题目:

    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.

    链接:  http://leetcode.com/problems/plus-one/

    题解:

    Time Complexity - O(n), Space Complexity - O(n)。

    public class Solution {
        public int[] plusOne(int[] digits) {
            int carry = 1;
            for(int i = digits.length - 1; i >= 0; i --){
                int curValue = digits[i];
                digits[i] = (curValue + carry) % 10;
                carry = (curValue + carry) >= 10 ? 1 : 0;
            }
            
            if(carry == 1){
                int[] result = new int[digits.length + 1];
                result[0] = 1;
                for(int i = 1; i < result.length; i ++){
                    result[i] = digits[i - 1];
                }
                return result;
            } else 
                return digits;
        }
    }

    Update:

    public class Solution {
        public int[] plusOne(int[] digits) {
            if(digits == null || digits.length == 0)
                return digits;
            int carry = 1;
            
            for(int i = digits.length - 1; i >=0; i--) {
                int cur = digits[i];
                digits[i] = (cur + carry) % 10;
                carry = (cur + carry) >= 10 ? 1 : 0;
            }
            
            if(carry == 1) {
                int[] newDigits = new int[digits.length + 1];
                newDigits[0] = 1;
                
                for(int i = 1; i < newDigits.length; i++)
                    newDigits[i] = digits[i - 1];
                    
                return newDigits;
            }
            
            return digits;
        }
    }

    二刷:

    注意carry最后为1的情况

    Java:

    Time Complexity - O(n), Space Complexity - O(n)。

    public class Solution {
        public int[] plusOne(int[] digits) {
            if (digits == null || digits.length == 0) {
                return digits;
            }
            int carry = 1, cur = 0;
            for (int i = digits.length - 1; i >= 0; i--) {
                cur = (digits[i] + carry) % 10;
                carry = (digits[i] + carry >= 10) ? 1 : 0;
                digits[i] = cur;
            }
            if (carry == 1) {
                int[] newDigits = new int[digits.length + 1];
                newDigits[0] = 1;
                for (int i = 1; i < newDigits.length; i++) {
                    newDigits[i] = digits[i - 1];
                }
                return newDigits;
            } 
            return digits;
        }
    }

    三刷:

    Java:

    public class Solution {
        public int[] plusOne(int[] digits) {
            if (digits == null) {
                return digits;
            }
            int carry = 1;
            int len = digits.length;
            for (int i = len - 1; i >= 0; i--) {
                digits[i] += carry;
                carry = (digits[i] >= 10) ? 1 : 0;
                digits[i] %= 10;
            }
            if (carry == 1) {
                int[] res = new int[len + 1];
                res[0] = 1;
                for (int i = 1; i <= len; i++) {
                    res[i] = digits[i - 1];
                }
                return res;
            }
            return digits;
        }
    }

    从luke处学到的方法,非常好用。

    public class Solution {
        public int[] plusOne(int[] digits) {
            if (digits == null) {
                return digits;
            }
            int i = digits.length - 1;
            while (i >= 0) {
                if (digits[i] == 9) {
                    digits[i--] = 0;
                } else {
                    digits[i] += 1;
                    return digits;
                }
            } 
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            return res;
        }
    }
  • 相关阅读:
    spring----AOP 和 AspectJ
    js----wangEditor
    java易错题----获取路径问题
    Spring MVC----Validation(数据校验)
    java----lombok插件
    jquery----Ztree
    java web----跨域请求解决办法
    git----gitHub和电脑绑定的步骤
    js----DateTime工具
    SQLSTATE[HY000]: General error: 1366 Incorrect string value: 'xF0x9Fx90xA3xF0x9F...' for column
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4436428.html
Copyright © 2011-2022 走看看