plus one

作者: 夏的背影 | 来源:发表于2017-03-28 19:03 被阅读38次

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()打印数组,出来只是数组的内存地址
继续学习

相关文章

  • LeetCode 66-70

    66. Plus One[https://leetcode-cn.com/problems/plus-one/] ...

  • 66. Plus One

    66. Plus One 题目:https://leetcode.com/problems/plus-one/ 难...

  • Plus One

    https://leetcode.com/problems/plus-one/description/ 思路 反向...

  • Plus One

  • one plus

    Easy, Math Question 一个数以digit序列的形式给出,加一返回对应digit序列 Notes ...

  • Plus One

    Given a non-negative number represented as an array of di...

  • plus one

    question: answer: 之前蠢了,打印数组用Arrays.toString()方便很多直接在Syste...

  • Plus one

    Given a non-negative number represented as an array of di...

  • Plus one

    Given a non-negative number represented as an array of di...

  • Plus One

    题目描述Given a non-empty array of digits representing a non-...

网友评论

      本文标题:plus one

      本文链接:https://www.haomeiwen.com/subject/nsbrottx.html