Java [Leetcode 66]Plus One

题目描述:

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.

解题思路:

设置进位,如果数组不够的话记得重新开辟空间。

代码如下:

public class Solution {
    public int[] plusOne(int[] digits) {
    	int carry = 0;
    	if(digits == null || digits.length == 0)
    		return null;
    	else
    		digits[digits.length - 1] += 1;
        for(int i = digits.length - 1; i >= 0; i--){
        	int cur = carry + digits[i];
        	carry = cur / 10;
        	digits[i] = cur % 10;
        }
        if(carry != 0){
        	int[] newDigits = new int[digits.length + 1];
        	System.arraycopy(digits, 0, newDigits, 1, digits.length);
        	newDigits[0] = carry;
        	return newDigits;
        } else {
        	return digits;
        }
    }
}

  

时间: 2024-12-07 13:44:00

Java [Leetcode 66]Plus One的相关文章

Java [Leetcode 119]Pascal's Triangle II

题目描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode 66. 加一(java)

题目: https://leetcode-cn.com/problems/plus-one/ 如果digits数组最后一位小于9,则只需要将digits数组最后一个数+1,返回digits数组即可:如果最后一位等于9,则需要设置一个循环进行加法模拟,即对每一位进行判断,小于10退出循环,如果等于10,进位,这种情况则需创建一个新数组,长度为digits长度加一,将数据保存进去即可. 代码: class Solution { public int[] plusOne(int[] digits) {

LeetCode 66. Plus One(加1)

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at

Java [leetcode 1] Two Sum

小二终于开通博客了,真是高兴.最近在看Java,所以就拿leetcode练练手了.以后我会将自己解体过程中的思路写下来,分享给大家,其中有我自己原创的部分,也有参考别人的代码加入自己理解的部分,希望大家看了多提意见,一块加油. 问题描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return

(Java) LeetCode 334. Increasing Triplet Subsequence —— 递增的三元子序列

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return

(Java) LeetCode 433. Minimum Genetic Mutation —— 最小基因变化

A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE m

LeetCode 66. 加1

题目: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 思路与解答: 题解一: 这道题最直观的方法就是首先判断末位是否为9,不为9,直接加1返回,具体为: 从末位开

Java [Leetcode 357]Count Numbers with Unique Digits

题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) 解题思路: This