leetCode练习(66)

编程入门 行业动态 更新时间:2024-10-28 00:28:54

题目:Plus One

难度:medium

问题描述:

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 int[] plusOne(int[] digits) {
        int jiwei=0;
        int temp;
        temp=digits[digits.length-1]+1;
        jiwei=temp/10;
        digits[digits.length-1]=temp%10;
        for(int i=digits.length-2;i>=0;i--){
        	temp=digits[i]+jiwei;
        	jiwei=temp/10;
        	digits[i]=temp%10;
        }
        if(jiwei==0){
        	return digits;
        }else{
        	int[]res=new int[digits.length+1];
        	res[0]=jiwei;
        	for(int i=0;i<digits.length;i++){
        		res[i+1]=digits[i];
        	}
        	return res;
        }
    }

更多推荐

leetCode练习(66)

本文发布于:2023-06-13 09:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/679115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:leetCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!