LeetCode[Array]: 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.

一开始我误解了题目的意思,以为是新建一个vector返回,于是有了下面的解法:

C++ code

    vector<int> plusOne(vector<int> &digits) {
        vector<int> result;
        int carry = 1;
        for (int i = digits.size() - 1; i >= 0; --i){
            result.insert(result.begin(), (digits[i] + carry) % 10);
            carry = (digits[i] + carry == 10);
        }

        if (carry)
            result.insert(result.begin(), 1);

        return result;
    }

后来看到Discuss中别人的做法都是在原数组上做的,又重新审了一下题,发现题目中的“the number”指的应该是在原数组。这样在做法上还带来一点区别就是:发现没有进位时即可退出循环。解法如下:

C++ code

    vector<int> plusOne(vector<int> &digits) {
        int carry = 1;
        for (int i = digits.size() - 1; i >= 0; --i){
            if (carry)
            {
                digits[i] += carry;
                carry = (digits[i] == 10);
                digits[i] %= 10;
            }
            else
                break;
        }

        if (carry)
        {
            digits[0] = 1;
            digits.push_back(0);
        }

        return digits;
    }
时间: 2024-10-16 23:12:30

LeetCode[Array]: Plus One的相关文章

LeetCode[Array]: Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 参考LeetCode[Array]: Spiral Matrix II的迭代思路,先完成最外环的旋转,然后依次旋转内环.我的C++代码如下: void rotate(vector<vector<int> >

LeetCode[Array]: Pascal&#39;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]. Note: Could you optimize your algorithm to use only O(k) extra space? 这个问题比较简单. vector<int> getRow(int rowIndex) { vector<int> row, prev

LeetCode[Array]: Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 这个题目跟LeetCode[Array]: Spiral Matrix不同的是:这个题目并不

Leetcode Array 4 Median of Two Sorted Arrays

做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法了.刚开始没啥思路,就用暴力的方法,用双层循环遍历的一下两个已经排好序的数组 ,在中间位置停止找道中位数.这样时间复杂度是肯定不能满足题目要求的,但是程序测试还是过了. 苦于自己没有思路,又不甘心就这样水过一道题,还是搜了一下博客,膜拜了一下大神.最好的方法是将中位数  -- 两个数组数据排好序之后

LeetCode[Array]: Trapping Rain Water

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. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

[LeetCode]Array主题系列

1.内容介绍 开一篇文章记录在leetcode中array主题下面的题目和自己的思考以及优化过程,具体内容层次按照{题目,分析,初解,初解结果,优化解,优化解结果,想法}的格式来记录,供日后复习和反思.题目的顺序按照leetcode给出的题目顺序,有些题目在并不是按照题目本身序号顺序排列的,也不是严格按照难易程度来排列的. 因此,这篇文章并不具有很强的归类总结性,归类总结性知识将会在其他文章记录,本篇重点在记录解题过程中的思路,希望能对自己有所启发. 2.题目和解题过程 2.1 Containe

Leetcode Array 16 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

[LeetCode] Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explan

LeetCode[Array]: Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1], 3 is a peak eleme