[LeetCode]Plus One
发布日期:2021-11-22 02:48:49 浏览次数:8 分类:技术文章

本文共 549 字,大约阅读时间需要 1 分钟。

1Given 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.

题意:给一个数的数组形式(例如78 digits[0]=7,digits[1]=8),每一位加1,返回一个数组形式。注意99 digits[0]=9,digits[1]=9,返回应该为nums[0]=1,nums[1]=0,nums[2]=0,100

code:

public int[] plusOne(int[] digits) {	   		 for(int i = digits.length-1; i>=0; i--){			 			 if(digits[i]<9){				 digits[i]++;				 return digits;			 }			 digits[i] =0;		 }		 int[] nums = new int[digits.length+1];		 nums[0]=1;		 return nums;	  }

转载地址:https://blog.csdn.net/zxdfc/article/details/48625521 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[LeetCode]House Robber
下一篇:[LeetCode]Binary Tree Level Order Traversal II

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月31日 08时05分25秒