import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/plus-one/
*
*
* 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.
*
*/
public class PlusOne { /**
* 加法运算,注意进位
*
* @param digit
* @return
*/
public Integer[] plusOne (int[] digit) {
int carry = 1;
List<Integer> result = new ArrayList<Integer>();
for (int i = digit.length - 1; i > -1; i--) {
carry += digit[i];
result.add(0, carry % 10);
carry = carry / 10;
}
if (carry > 0) {
result.add(0, carry);
}
return result.toArray(new Integer[result.size()]);
} public static void main(String[] args) {
PlusOne plusOne = new PlusOne();
int[] arr = new int[]{1,2,3};
int[] arr1 = new int[]{1,9,9};
int[] arr2 = new int[]{9,9,9};
int[] arr3 = new int[]{1};
int[] arr4 = new int[]{9};
int[] arr5 = new int[]{}; System.out.println(Arrays.toString(plusOne.plusOne(arr)));
System.out.println(Arrays.toString(plusOne.plusOne(arr1)));
System.out.println(Arrays.toString(plusOne.plusOne(arr2)));
System.out.println(Arrays.toString(plusOne.plusOne(arr3)));
System.out.println(Arrays.toString(plusOne.plusOne(arr4)));
System.out.println(Arrays.toString(plusOne.plusOne(arr5)));
}
}

leetcode — plus-one的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. canvas绘制圆图输出图片格式

    function drawCircleImage(url, callback) { const canvas = document.createElement('canvas'); const img ...

  2. 工作我们是专业的之css规范

    我一直认为专业是一种态度.不同于业余,专业代表无论技术高低都会遵守一定的规范,专业代表对某一领域不断的精益求精.专业就是比业余逼格高. 习惯书写规范 css 属性声明的顺序:Positioning(定 ...

  3. linux时区和时间设置

    1,修改时区 调整时区使用tzselect [root@lyn ~]# hwclock Tue Nov :: PM AST -0.198205 seconds [root@lyn ~]# tzsele ...

  4. 欣赏<沉默的大多数>——王小波

    君特·格拉斯在<铁皮鼓>里,写了一个不肯长大的人.小奥斯卡发现周围的世界太过荒诞,就暗下决心要永远做小孩子.在冥冥之中,有一种力量成全了他的决心,所以他就成了个侏儒.这个故事太过神奇,但很 ...

  5. Anfora 自动装载类

    1.目录结构 + Anfora | + Autoload | - ClassLoader.php | - Autoload.php - autoload.php 2.注册类加载器 src/Anfora ...

  6. CSS3变换、过渡、动画效果

    为元素创建圆角 1.四个相同的圆角 -webkit-border-radius:10px; border-radius:10px; 2.一个圆角 -webkit-border-top-left-rad ...

  7. ubuntu+apache2设置访问、重定向到https

    环境:ubunt14裸机,apache2,php5 条件:证书(部分商家买域名送一年),域名,为了方便均在root用户下进行的 web目录:/var/www/test 证书目录(自建):/etc/ap ...

  8. 解读IEEE 7417的软件体系架构描述的概念模型

    本文将解读标准IEEE Std 1471-2000(密集型软件的体系结构描述推荐实施规程)的概念模型图部分,从中一窥作为软件架构师的进行架构设计的思考角度与策略.如果我们把世界当做一场游戏,现在要玩的 ...

  9. 搭建node js的运行环境。

    第一步:首先安装一个NVM,就是一个node的版本管理器. nvm的下载地址::https://github.com/coreybutler/nvm-windows/releases,我选择下载的是n ...

  10. unigui+fastreport报表打印

    unigui+fastreport报表打印   unigui+fastreport报表打印 FASTREPORT导出成PDF报表,UNIGUI的TUniURLFrame显示PDF报表并打印. func ...