疯狂毒贩夫妇 8000万钞票当睡床# Joke - 肚皮舞运动
h*n
1 楼
How can you convert a number which is available in the form of array to a
new array by adding 1 to it. How would you do it in Java without using any
extra storage & with best time complexity of O(n)?
Example: Input number is also in the form of array.
748 [7,4,8] would be 749 as [7,4,9]
999 [9,9,9] would be 1000 as [1,0,0,0]
最笨的想法:
1. 从左到右读,变读边算出最终的值,然后+1,然后转回新的数组
2. 或者对从右往左,先右边第一个+1,如果有carry,带着carry往左加,直到头,如
果不够放,需要allocate一个新数组,然后把旧的数组放进新数组,然后新数组第一个元素放上原先不够放的元素
大家还有啥想法比这个好的?
new array by adding 1 to it. How would you do it in Java without using any
extra storage & with best time complexity of O(n)?
Example: Input number is also in the form of array.
748 [7,4,8] would be 749 as [7,4,9]
999 [9,9,9] would be 1000 as [1,0,0,0]
最笨的想法:
1. 从左到右读,变读边算出最终的值,然后+1,然后转回新的数组
2. 或者对从右往左,先右边第一个+1,如果有carry,带着carry往左加,直到头,如
果不够放,需要allocate一个新数组,然后把旧的数组放进新数组,然后新数组第一个元素放上原先不够放的元素
大家还有啥想法比这个好的?