zoukankan      html  css  js  c++  java
  • leetcode : Plus One 基本功

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

    You may assume the integer do not contain any leading zero, except the number 0 itself.

    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 carry = 1;
    		
    		for(int i = digits.length - 1; i >= 0 && carry == 1; i--){
    			int sum = digits[i] + carry;
    			digits[i] = sum % 10;
    			carry = sum / 10;
    		}
    		
    		if(carry == 0){
    			return digits;
    		}
    	
    		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;
    
        }
        
    }
    

      

  • 相关阅读:
    每周总结8
    每周总结7
    每周总结6
    每周总结5
    每周总结4
    每周总结3
    每周总结2
    每周总结1
    Vue实例: 点击循环列表里的某行,改变该行的样式。默认第一行
    vue进阶面试题
  • 原文地址:https://www.cnblogs.com/superzhaochao/p/6479257.html
Copyright © 2011-2022 走看看