question:
//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.
answer:
public class pulsOne {
public static int[] plusOne(int[] array){
int one =1;
int sum =0;
for (int i =array.length-1;i>=0;i--){
sum = one + array[i];
one = sum/10;
array[i] = sum%10;
}
if(one==0){
return array;
}
int[] plusresult = new int[array.length+1];
plusresult[0] =1;
for(int i=0;i<array.length;i++){
plusresult[i+1] = array[i];
}
return plusresult;
}
public static void main(String[] args) {
int[] array1 ={8,7};
System.out.println(Arrays.toString(plusOne(array1)));
// TODO Auto-generated method stub
}
之前蠢了,打印数组用Arrays.toString()
方便很多
直接在System.out.println()
打印数组,出来只是数组的内存地址
继续学习