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 ...
随机推荐
- 别人的Linux私房菜(21)基础系统设置与备份策略
网络设置,手动设置IP,DHCP自动获取. 以太网协议开发出来的网卡ethN,N为数字. CentOS7对网卡命名的规则:eno代表由主板BIOS内建立的网卡,ens1由主板BIOS内建的PCI-E界 ...
- 在香港用什么软件可以唱歌?香港K歌app推荐
KTV的源头来自于日本,KTV是Karaok TV的缩写.随着互联网时代越来越发达,手机K歌成了很多人会选择的方式,那么在香港有什么好用的K歌软件呢?这里qt6小编给大家推荐几款好用的,让你足不出户即 ...
- Java集合不能存放基本数据类型
Java集合不能存放基本数据类型,只能存放对象的引用. 每个集合元素都是一个引用变量,实际内容都存放在堆内或方法区里面, 但是基本数据类型是在栈内存上分配空间的,栈上的数据随时会被收回. 如何解决? ...
- mysql 模糊查询条件带‘%’问题
- EC20指令测试
cat /dev/ttyUSB2 & echo -e "AT+CGMM\r\n" >/dev/ttyUSB2 //输出模块型号 echo -e "AT+ ...
- shell编写自动化安装dhcp服务
#!/bin/bash#Auth:Darius#自动化安装dhcp服务#"$1"为测试IP,用来查看IP段是否能通eno=`ifconfig|awk '{print $1}'|he ...
- ASCII记录
符号 十进制 + 43 - 45
- Xamarin常见问题
1. Could not locate Java 6 or 7 SDK. (Download from http://www.oracle.com/technetwork/java/javase/do ...
- Drools为什么没有规则流Flow Flie
哪个大神能告诉我,我安装的是Drools7.7.0,为什么没有网上说的flow file啊?怎么才能出来规则流呢? 上图是我本地的显示,下图是网上的图片.
- win10 win7 环境下 oracle 11g和Plsql的安装、卸载遇到的问题。
* win7一体机在安装好oracle和PlSQL后,无法连接到orcl数据库,同时也忘记了sys设置的密码.(在这里应注意在安装过程中,应选择统一口令,这里我均设置成了orcl,同时也应该注意在最后 ...