zoukankan      html  css  js  c++  java
  • plus one

    1. 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.
    View Code

    2. Solution

    public class Solution {
        public int[] plusOne( int[] digits ){
            digits[digits.length-1]++;
            int i;
            for( i=digits.length-1; i>0 && digits[i]/10 != 0; i-- ){
                digits[i-1] += digits[i]/10;
                digits[i] = digits[i] % 10;
            }
            int[] res = null;
            int k;    //the start point of res to copy from digits
            if( digits[i]/10!=0 ){
                res = new int[ digits.length + 1 ];
                res[0] = digits[i] / 10;
                digits[i] = digits[i] % 10;
                k = 1;
            }
            else{
                res = new int[ digits.length ];
                k = 0;
            }
            System.arraycopy(digits, 0, res, k, digits.length);
            return res;
        }
    }
    View Code
  • 相关阅读:
    机器学习之线性回归
    Anaconda使用
    Pycharm使用总结
    Mysql使用小tips
    技术转型与考研总结
    C语言的学习
    python 使用小结
    RedHat Linux 忘记密码
    设计模式之单例模式
    Java 读写Properties配置文件
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/4598695.html
Copyright © 2011-2022 走看看