LintCode : 加一

来源:互联网 发布:饭卡充值软件 编辑:程序博客网 时间:2024/04/28 06:21

LintCode : 加一

给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。

该数字按照大小进行排列,最大的数在列表的最前面。


先把数组组装成字符串,再把字符串转成数组,然后加1,再转成字符串,最后转成数组,有点麻烦。


public class Solution {    /**     * @param digits a number represented as an array of digits     * @return the result     */    public int[] plusOne(int[] digits) {        // Write your code here        String s = "";        for(int i=0; i<digits.length; i++){            s += digits[i];        }        long m = Long.valueOf(s) + 1;        String s1 = m + "";        int[] ans = new int[s1.length()];        for(int i=0; i<s1.length(); i++){            ans[i] = (Integer.valueOf(s1.charAt(i) + ""));        }        return ans;    }}
0 0
原创粉丝点击