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[]。

我的代码:

 1     public int[] plusOne(int[] digits) {
 2         int i = digits.length-1;
 3         while (i > -1) {
 4             if (digits[i] + 1 > 9) {
 5                 digits[i] = 0;
 6                 i--;
 7             } else {
 8                 digits[i]++;
 9                 return digits;
10             }
11         }
12         int[] ans = new int[digits.length+1];
13         ans[0] = 1;
14         for (int j = 0; j < digits.length; j++) {
15             ans[j+1] = digits[j];
16         }
17         return ans;
18     }

我的复杂度:O(n)    空间复杂度也是O(n)

编程过程中的问题

答案代码

 1 public int[] plusOne(int[] digits) {
 2
 3     int n = digits.length;
 4     for(int i=n-1; i>=0; i--) {
 5         if(digits[i] < 9) {
 6             digits[i]++;
 7             return digits;
 8         }
 9
10         digits[i] = 0;
11     }
12
13     int[] newNumber = new int [n+1];
14     newNumber[0] = 1;
15
16     return newNumber;

答案复杂度:O(n)    空间复杂度也是O(m*n)  和我的一样

答案思路:和我的是一样的,也是从尾到头循环,但是最后再退出循环的时候因为是+1,此时如果还没有return,说明digits全是9999...,所以直接return一个最前面是1,其他是0的数组就好了,不需要再将digits后面的值(肯定是0)赋给它。

扩展:67. Add Binary  (二进制相加)  

输入两个String,表示两个二进制数,返回一个String表示他们俩的和。

思路:和本题一样,从最右边开始计算,同时计算进位符号。

  主要思想是用了两个指针和一个进位变量carry,两个任何一个大于等于0就继续加,同时利用StringBuffer()一个一个放进去,最后再反转。

代码:

 1     public String addBinary(String a, String b) {
 2         StringBuilder sb = new StringBuilder();
 3         int i = a.length() - 1, j = b.length() -1, carry = 0;
 4         while (i >= 0 || j >= 0) {
 5             int sum = carry;
 6             if (j >= 0) sum += b.charAt(j--) - ‘0‘;
 7             if (i >= 0) sum += a.charAt(i--) - ‘0‘;
 8             sb.append(sum % 2);
 9             carry = sum / 2;
10         }
11         if (carry != 0) sb.append(carry);
12         return sb.reverse().toString();
13     }

注意,需要将 char - ‘0’  转为数组,并且别忘了 i -- 和 j --。

原文地址:https://www.cnblogs.com/Xieyang-blog/p/9055512.html

时间: 2024-08-30 07:02:32

LeetCode第[66]题(Java):Plus One的相关文章

LeetCode 第66题,加一

题目概述 题目:力扣:66.加一 难易:简单 内容: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 来源:力扣(LeetCode) 链接:https://lee

LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extr

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is &q

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

LeetCode第[42]题(Java):Trapping Rain Water

题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In

LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same

leetcode第66题 (array)

题目: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 即用数组代表一个数,给这个数加1后,继续用数组表示. 比较简单,注意进位就可以了,尤其是要注意最高项的进位,容易忽视. 1 vector<int

LeetCode第[10]题(Java):Regular Expression Matching

题目:匹配正则表达式 题目难度:hard 题目内容:Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The fu