LeetCode第[66]题(Java):Plus One
题目:数组加一
难度:Easy
题目内容:
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
翻译:
给定一个非空的数字数组,表示一个非负整数,加上1到整数。
这些数字被存储起来,使得最重要的数字位于列表的头部,数组中的每个元素都包含一个数字。
您可以假设这个整数不包含任何前导零,除了数字0本身
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
我的思路:一开始想到的是,用StringBuffer将他们一个一个都取出来,然后转为int再相加,然后再转为int数组返回,然后发现,有几个测试用例特别长。。。。无法用String转为int或者long。
所以只能考虑遍历,从尾开始遍历,如果当前值加上进位 大于9,那么当前值就为0,并且继续进位,否则则直接返回。
然后最后退出循环的时候也要判断一下是否最后一位也是有进位,如果是,则最前面比原来多出一位“1”,所以必须重新new一个int[]。
我的代码:
public int[] plusOne(int[] digits) {
int i = digits.length-1;
while (i > -1) {
if (digits[i] + 1 > 9) {
digits[i] = 0;
i--;
} else {
digits[i]++;
return digits;
}
}
int[] ans = new int[digits.length+1];
ans[0] = 1;
for (int j = 0; j < digits.length; j++) {
ans[j+1] = digits[j];
}
return ans;
}
我的复杂度:O(n) 空间复杂度也是O(n)
编程过程中的问题:
无
答案代码:
public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
答案复杂度:O(n) 空间复杂度也是O(m*n) 和我的一样
答案思路:和我的是一样的,也是从尾到头循环,但是最后再退出循环的时候因为是+1,此时如果还没有return,说明digits全是9999...,所以直接return一个最前面是1,其他是0的数组就好了,不需要再将digits后面的值(肯定是0)赋给它。
扩展:67. Add Binary (二进制相加)
输入两个String,表示两个二进制数,返回一个String表示他们俩的和。
思路:和本题一样,从最右边开始计算,同时计算进位符号。
主要思想是用了两个指针和一个进位变量carry,两个任何一个大于等于0就继续加,同时利用StringBuffer()一个一个放进去,最后再反转。
代码:
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() -1, carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (j >= 0) sum += b.charAt(j--) - '0';
if (i >= 0) sum += a.charAt(i--) - '0';
sb.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) sb.append(carry);
return sb.reverse().toString();
}
注意,需要将 char - ‘0’ 转为数组,并且别忘了 i -- 和 j --。
LeetCode第[66]题(Java):Plus One的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[15]题(Java):3Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
随机推荐
- java.lang.IllegalStateException: Queue full
其实异常说的很清楚 队列满了! ArrayBlockingQueue FIFO 的队列: ArrayBlockingQueue内部是通过一个Object数组和一个ReentrantLock实现的.同时 ...
- T420 开启麦克风
买来之后一直没注意过麦克风的问题,今天基友们群视频,才发现我的机器是哑的 打开录音设备,发现没有设备 重装驱动无果 打开BIOS,在安全选项——IO/Access中将Microphone 设为 Ena ...
- 单源最短距离 Single Source Shortest Path
单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...
- cookies与session
一.cookies 本质:浏览器端保存的键值对 方便客户按照自己的习惯操作页面或软件,例如:用户验证,登陆界面,右侧菜单隐藏,控制页面列表显示条数... cookies是由服务端写在浏览器端,以后每次 ...
- day5笔记 列表 list 增删改查
列表的使用 一.索引和切片 # 索引和切片,用法与字符串一样 l = [1,2,3,'af','re',4,'45'] print(l[0]) print(l[3]) print(l[-1]) # ' ...
- 解决hash冲突的办法
1.开发定址法 2.再哈希法 3.链地址法 4.建立一个公共溢出区
- Win10 IIS 安装.net 4.5
更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...
- Python框架之Tornado(源码之褪去模板外衣的前戏)
执行字符串表示的函数,并为该函数提供全局变量 本篇的内容从题目中就可以看出来,就是为之后剖析tornado模板做准备,也是由于该知识点使用的巧妙,所有就单独用一篇来介绍了.废话不多说,直接上代码: # ...
- 437. Path Sum III(路径可以任意点开始,任意点结束)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- VMWare 安装时报错 tools-windows.msi failed报错解决办法
1.我用的是7.1.3版本的,到官方网站上下载这个版本的tools安装包 http://softwareupdate.vmware.com/cds/vmw-desktop/ws/7.1.3/32428 ...