zoukankan      html  css  js  c++  java
  • 66. 加一

    66. 加一

    https://leetcode-cn.com/problems/plus-one/description/

    package com.test;
    
    import java.util.Arrays;
    
    /**
     * @Author stono
     * @Date 2018/8/23 上午8:13
     */
    public class Lesson066 {
        public static void main(String[] args) {
            int[] digits = {1};
            int[] ints = plusOne(digits);
            System.out.println(Arrays.toString(ints));
        }
    
        public static int[] plusOne(int[] digits) {
            int length = digits.length;
            // 如果最后一位小于9,不需要进位
            if (digits[length - 1] < 9) {
                digits[length - 1] = digits[length - 1] + 1;
                return digits;
            }
            int last = digits[length - 1];
            // 判断不等于9的是哪一位
            while (last - 9 == 0 ) {
                length--;
                if (length < 0) {
                    break;
                }
                last = digits[length];
            }
            // 找到中间某一位比9小
            if (length > -1) {
                // 把该位加1
                digits[length] = digits[length] + 1;
                // 该位之后的数字都加1
                for (int i = length + 1; i < digits.length; i++) {
                    digits[i] = 0;
                }
                return digits;
            }
            // 如果全部都等于9,就进位处理
            int[] res = new int[digits.length + 1];
            res[0] = 1;
            for (int i = 1; i < res.length; i++) {
                res[i] = 0;
            }
            return res;
        }
    }
  • 相关阅读:
    Python--魔法方法
    Flask之request实现思想
    关于python一切皆对象的理解
    python快速生成依赖包
    redis的安装和使用
    linux-环境变量的配置
    python-虚拟环境的配置
    11-Linux-系统服务
    10-Linux-进程管理
    [SCOI2014]方伯伯的玉米田
  • 原文地址:https://www.cnblogs.com/stono/p/9521629.html
Copyright © 2011-2022 走看看