【LeetCode】Plus One (2 solutions)

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.

从个位数字往前扫,只要一个数字不为9,即可加1返回。

如果直到最高位仍为9,则说明结果为100…0

解法一:

可能会使用额外空间与复制数组

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        for(int i = digits.size()-1; i >= 0; i --)
        {
            if(digits[i] <= 8)
            {
                digits[i] += 1;
                return digits;
            }
            else
            {//9
                if(i != 0)
                    digits[i] = 0;
                else
                {
                    vector<int> result(1,1);
                    result.push_back(0);
                    for(int j = 1; j < digits.size(); j ++)
                        result.push_back(digits[j]);
                    return result;
                }
            }
        }
    }
};

解法二:

inplace且不用复制数组

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        for(int i = digits.size()-1; i >= 0; i --)
        {
            if(digits[i] <= 8)
            {
                digits[i] += 1;
                return digits;
            }
            else
            {//9
                if(i != 0)
                    digits[i] = 0;
                else
                {
                    digits[0] = 1;
                    digits.push_back(0);
                    return digits;
                }
            }
        }
    }
};

时间: 2025-01-31 20:18:11

【LeetCode】Plus One (2 solutions)的相关文章

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Same Tree (2 solutions)

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解法一:递归 /** * Definition for binary tree * struct TreeNod

【LeetCode】Generate Parentheses (2 solutions)

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

【LeetCode】Sqrt(x) (2 solutions)

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)=x2-n的零点 设初始值为x0,注,不要设为0,以免出现除数为0,见后. 则过(x0,f(x0))点的切线为g(x)=f(x0)+f'(x0)*(x-x0) g(x)与x轴的交点为x1=x0-f(x0)/f'(x0) 递推关系为xn+1=xn-f(xn)/f'(xn) 当收敛时即为解. class

【LeetCode】Climbing Stairs (2 solutions)

Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2) 因此建立vector存放中间结果即可. class Solution

【LeetCode】Symmetric Tree (2 solutions)

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could sol

【LeetCode】Next Permutation (2 solutions)

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

【LeetCode】Clone Graph (2 solutions)

Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label an