Plus One

来源:互联网 发布:华为网络机顶盒插上u盘 编辑:程序博客网 时间:2024/05/01 19:43

Given a number represented as an array of digits, plus one to the number.

public class Solution {    public int[] plusOne(int[] digits) {// Start typing your Java solution below// DO NOT write main() functionArrayList<Integer> res = new ArrayList<Integer>();int carry = 1;for(int i = digits.length - 1; i >= 0; i--){int sum = carry + digits[i];res.add(sum % 10);carry = sum / 10;}if(carry != 0){res.add(carry);}int[] result = new int[res.size()];for(int i = 0; i < res.size(); i++)result[i] = res.get(res.size() - 1 - i);return result;}}


原创粉丝点击