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. php中的问题整理

    1.什么是 CSRF 攻击 ?XSS 攻击?如何防范? CSRF,跨站请求伪造,攻击方伪装用户身份发送请求从而窃取信息或者破坏系统.讲述基本原理:用户访问A网站登陆并生成了cookie,再访问B网站, ...

  2. SAS 选取部分观测

    SAS  对部分观测得处理 在建立新数据集时,有以下两种方式可以从已经存在的数据集中选取观测到新数据集中. ·通过删除不满足条件的观测来保留想要的观测. ·仅接受满足条件的观测. 条件可以由IF语句. ...

  3. 把mysql中的记录封装成集合

    package cn.hu3.com;import cn.hu1.com.JdbcUtils;import java.sql.Connection;import java.sql.PreparedSt ...

  4. ubuntu 安装vue+element

    1.安装npm sudo apt install npm 检测安装npm -v 因为npm安装软件慢,可设置淘宝镜像 npm config set registry https://registry. ...

  5. Rabbimq 安装过程,还有踩得坑!centos 安装

    一 .安装erlang 1 添加yum 源,在/etc/yum.repos.d 下添加 rabbitmq-erlang.repo,内容如下 # In /etc/yum.repos.d/rabbitmq ...

  6. go 闭包

    看程序 package main import "fmt" func main() { f:=test2() fmt.Println(f()) fmt.Println(f()) } ...

  7. submit与execute区别

    1.可以接受的任务类型 submit: execute: 可以看出: execute只能接受Runnable类型的任务 submit不管是Runnable还是Callable类型的任务都可以接受,但是 ...

  8. 进军微信小程序之准备工作

    小程序这么火,不去浪一浪怎么行?   更何况,现在微信“赦免”了个人认证,又更新了web开发工具,现在正是搞搞小程序的最佳时期! 那么一起来看看要做什么准备吧~   官方的文档很详细,可参考:小程序官 ...

  9. 最近一个dish项目的建设思考

    系统通用能力的沉淀:a.核心模型的数据沉淀 b.通用服务能力的沉淀 ps1:以前重心主要放在了业务的抽象和通过设计模式来增加可复用的扩展性.局限在于,抽象的范围会被单个业务或者当前的业务所束缚,在更大 ...

  10. 1.准备工作之Groovy

    Groovy(读做:gu : ru : wei) Groovy是一种运行在jvm上的动态语言,它吸取了Python.Ruby和SmallTalk等语言的优点:在Java的基础之上增加了许多特色功能,相 ...