题目:数组加一

难度: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的更多相关文章

  1. 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 + ...

  2. 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 ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. 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 ...

  5. 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 ...

  6. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  7. 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 ...

  8. 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  ...

  9. 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 ...

随机推荐

  1. Spring容器初始化的时候如何添加一个定时器?

    昨天遇到这个问题,在项目启动的时候添加一个定时器隔一段时间扫描有没有定时发送的邮件(当然也可以是你自己的业务逻辑),也在网上找了资料,加上自己的修改,终于成功了.所以来做个记录. 1.ServletC ...

  2. Linux网络调试工具资料链接

    Dropbox: https://huoding.com/2016/12/15/574 Tcpdump: http://roclinux.cn/?p=2474

  3. (转)专项:Android 内存泄露实践分析

    今天看到一篇关于Android 内存泄露实践分析的文章,感觉不错,讲的还算详细,mark到这里. 原文发表于:Testerhome: 作者:ycwdaaaa ;  原文链接:https://teste ...

  4. mysql聚合函数操作

    1.mysql对中文进行排序 注:是用convert函数用gb2312编码转换 SELECT * FROM 表名 ORDER BY CONVERT(字段名 USING gb2312 ) ASC;

  5. LRU经典算法的原理与实现

    LRU  least recently used.顾名思义,是根据数据的活跃度进行更新的缓存算法. LRU Cache的LinkedHashMap实现: LinkedHashMap自身已经实现了顺序存 ...

  6. s5_day13作业

    #对之前文件进行的增删改查操作实现日志操作,日志输出用户进行过的操作. def log(): import logging logger_obj=logging.getLogger() logger_ ...

  7. SQL server 数据库 操作及简单查询

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  8. $git学习总结系列(4)——gitignore文件

    有时候工作区中会有我们创建的一些密码配置文件,或者自动生成的一些临时文件,比如python代码编译产生的.pyc文件和java代码编译产生的.class文件等,我们在提交代码的时候没有必要把这些文件也 ...

  9. xshell如何同时打开多个标签

    查看标签>>>>>回话选项卡>>>>>> 打钩即可

  10. Winform ObservableCollection 添加删除修改无效

    WPF中ObservableCollection 可以很好的使用. 在Winform中,之前使用ObservableCollection 做了几个功能,貌似增删改都可以. 今天写个Demo的时候发现不 ...