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 ...
随机推荐
- Jconsole监控tomcat 的JVM内存的设置
主要参考这位仁兄的文章 http://elf8848.iteye.com/blog/471676 照做后发现还是不行,原来是Linux服务器配置了多块网卡,在设置 Djava.rmi.server. ...
- Java中的(构造方法、方法重载、final修饰符使用及继承和抽象)
构造方法: 构造方法的名称和类名相同,没有返回类型,参数列表(类型.个数)不同 方法重载:成员方法和构造方法都可以进行重载 方法名相同但是参数列表(类型,个数)不同,成为方法的重载. 继承:直支持单继 ...
- Java 集合框架之泛型
JDK 1.5 以后出现的安全机制,提高了编译时期的安全性. 泛型出现的好处: 将运行时期的问题 ClassCastException 转到了编译时期 避免了强制转换的麻烦 泛型出现的原因: publ ...
- django博客项目7
................
- rest_framework之访问频率控制
一 自定义频率控制类 class MyThrottle(): visitor_dic = {} def __init__(self): self.history = None def allow_r ...
- 异常处理、socke基于TCP协议编程
一.异常处理 1.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误过不了Python解释器的语法检测,必须在程序执行前改正) #语法错误示范一 if #语法错误示范二 de ...
- spring 整合mybatis找不到${jdbc.driverClass}
1.检查是否设置了mapper扫描org.mybatis.spring.mapper.MapperScannerConfigurer类 在spring里使用org.mybatis.spring.map ...
- 爬虫任务二:爬取(用到htmlunit和jsoup)通过百度搜索引擎关键字搜取到的新闻标题和url,并保存在本地文件中(主体借鉴了网上的资料)
采用maven工程,免着到处找依赖jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...
- 10046 trace详解(1)
10046 trace帮助我们解析一条/多条SQL.PL/SQL语句的运行状态,这些状态包括:Parse/Fetch/Execute三个阶段中遇到的等待事件.消耗的物理和逻辑读.CPU时间.执行计划等 ...
- matlab循环保存dat文件
将数据保存为dat文件 这里有两种方法,第一种是: save filename dataname; 这种方法书写简单,但是功能也很简单.这里的filename就是死的filenam,即filename ...