题目:数组加一

难度: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. [NSUserDefaults]的使用:登陆后不再显示登录界面。

    简介: NSUserDefaults是IOS应用用来存储用户偏好和配置信息的途径,就像是一个数据库,但是它通过键值对(key-value)的方式存储. 比如["Thematrix" ...

  2. 第19章—后端分页(PageHelper)

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  3. 总结学习! xml与java对象转换 --- JDK自带的JAXB(Java Architecture for XML Binding)

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  4. 使用idea的条件断点快速定位注解的处理类

    看代码时会碰到注解的处理类难定位的情况,比如spring的某个注解我们想知道到底是谁在处理他,他起什么作用,通过普通的代码搜索功能不容易找到,比如好用的方法就是条件断点. 比如下断:Accessibl ...

  5. CWM是什么?

    CWM [1]  (CommonWarehouseMetamodel公共仓库元模型)是OMG组织在数据仓库系统中定义了一套完整的元模型体系结构,用于数据仓库构建和应用的元数据建模.公共仓库元模型指定的 ...

  6. 请教Hibernate和JPA什么区别?

    JPA是Java的持久化规范.Hibernate早期是一个ORM框架,后期是JPA的一个实现.

  7. TimeQuest学习总结

    1. 基本时钟约束:creat_clock 2. 生成时钟约束:creat_generated_clock 3. I/O输入输出约束:(1)纯组合逻辑:set_max_delay & set_ ...

  8. Appium中的logger

    原文地址http://blog.csdn.net/itfootball/article/details/45395901 appium中的log输出量很大,我们分析问题的时候会依赖于这些log,但是你 ...

  9. appium的API

    使用的语言是java,appium的版本是1.3.4,java-client的版本是java-client-2.1.0,建议多参考java-client-2.1.0-javadoc. 1.使用Andr ...

  10. Django CSRF cookie not set.错误

    post提交表单报错: Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message b ...