leetcode — plus-one
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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 企业微信自建应用移动端动态获取li并给其事件问题总结
前段时间一个项目增加企业微信移动端应用,其中几个小功能用到ul-li列表点击并获得相应数据: 开始用var lis=$('#ul li'); for(var=i;i<lis.length;i++ ...
- sequence测试中的使用
1. create sequence : create sequence TEST_SEQUENCE minvalue 1 maxvalue 1000000000 start with 1 incre ...
- 小白的CTF学习之路8——节约内存的编程方式
今天第二更,废话不说上干货 上一章我们学习了内存和cpu间的互动方式,了解到内存的空间非常有限,所以这样就需要我们在编程的时候尽可能的节省内存空间,用最少的空间发挥最大的效果,以下是几种节约内存的方法 ...
- #pragma常用预处理指令
#pragma pack(1):1字节对齐#pragma once:指定头文件被编译一次#pragma message("message"):编译时输出message文本#prag ...
- Python开发——9.面向对象编程
一.面向对象设计(Object oriented design) 面向对象设计是将一类具体事物的数据和动作整合到一起的过程,不会要求面向对象语言编程,但如果想要构造具备对象性质和特点的数据类型,需要更 ...
- spring filter lister servlet
https://blog.csdn.net/nacey5201/article/details/8547772 https://blog.csdn.net/xwl617756974/article/d ...
- Jquery 在子页面上设置父页面元素的值
使用情景:因为我父页面上有用art.dialog,而子页面上有项目中的框架弹出方法跟art.dialog冲突,不能使用art.dialog自带的方法传值, 所以只好用一种简单粗暴的方法来设置. var ...
- 0723掰棒子记录--vue的数据渲染
问题1:想要在一个panel标签中添加一个图片,panel中有一个datalist属性.如何设计标签可以插入想要的图片. template: <panel :list="dataLis ...
- some knowledge of language
1:编译型语言2:解释型语言编译型:编译形成结果,再整体运行解释型:运行产生结果,边解释运行java 特殊(.class)再解释3:脚本语言是解释语言它的优点是方便阅读,不需要写非常多的类型相关的代码 ...
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...