题目:数组加一

难度: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. Windows File 管理工具:junction And Subinacl

    junction.exe   是 Sysinternals 出品的命令行工具.使用前建议将其复制到%SystemRoot%/system32目录下 创建一个名为 D:/LINK 的[junction ...

  2. 如何在 window 上面输入特殊字符?

    打开 字符映射表 程序 选中任意一个字符,它会在下方显示该字符的 16进制 转换16进制至10进制,并在输入法打开的状态下,按住 Alt 键输入 10 进制数值即可.

  3. windows 系统如何安装 mysql 8.0.15 数据库?

    windows 系统如何安装 mysql 8.0.15 数据库? 1. 下载安装包 下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0. ...

  4. 名义人均GDP的背后,中国真实的人均GDP是1.2万美元!(中国GDP含金量较高)

    来源:天涯社区 根据IMF(国际货币基金组织)在今年4月的报告,2014年份中国人均GDP为7600美元,在185个国家当中排行第78位. 然而,根据楼主在国外行走多年的经验,巴西.墨西哥.马来西亚. ...

  5. 详细介绍Redis的几种数据结构以及使用注意事项(转)

    原文:详细介绍Redis的几种数据结构以及使用注意事项 1. Overview 1.1 资料 <The Little Redis Book>,最好的入门小册子,可以先于一切文档之前看,免费 ...

  6. django中的setting全局变量的导入

    需求:在py文件中导入settings.py中的变量BASE_DIR settings.py文件 import os # Build paths inside the project like thi ...

  7. java jacob调用打印,word,excel横向打印

    public static boolean printOfficeFile(File f) { if (f != null && f.exists()) { String fileNa ...

  8. vuejs项目打包成APP后,首页不显示

  9. web项目的getContextPath()

    伯乐一看小编的这个博文的标题是不是觉得有些小,以点到面,知道了web中getContextPath()这种获取路径的方式,显然其他的方式的是可以以此类推的.常说,工作学习找共同点嘛. 上一段我们也提高 ...

  10. C语言自带快速排序对比插入排序

    #include <stdio.h> #include <stdlib.h> #include <time.h> void getRandomArr (int ar ...