【leetcode】Plus One (easy)

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就变成0,如果不是就直接当前位加1,返回。如果全都是9,就在最高位加一个1.

但是我写C++的代码比较少,写的时候对于哪里是最高位弄晕了。绕了好久才AC。

高位在前,就是高位是先压入vector的,那么最高位在digits.begin()。先压入的靠近下标0

如果需要新加一个最高位,那digits里面肯定都是0,压入1后,最高位变成最后压入的了,所以还需要翻转一下。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        int i = digits.size() - 1;
        while(i >= 0)
        {
            if(digits[i] == 9)
            {
                digits[i] = 0;
                i--;
            }
            else
            {
                break;
            }
        }
        if(i < 0)
        {
            digits.push_back(1);
            reverse(digits.begin(), digits.end());
        }
        else
        {
            digits[i] += 1;
        }

        return digits;
    }
};

int main()
{
    Solution s;
    vector<int> in, out;
    in.push_back(1);
    in.push_back(9);
    in.push_back(9);
    out = s.plusOne(in);

    return 0;
}
时间: 2025-01-17 08:41:34

【leetcode】Plus One (easy)的相关文章

【leetcode】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

【leetcode】Palindrome Number (easy)

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【leetcode】Majority Element (easy)(*^__^*)

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 思路: 找主要元素,用major记录主要字母,n记录ma

【leetcode】 Unique Path ||(easy)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【leetcode】Climbing Stairs (easy)

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? 跳台阶,之前写过递归的,这次写个非递归的. 到第n层的方式 其实就是 到第n-1层的方式(爬一层)和到第n-2层的方式(爬两层)的和 class Solution {

【leetcode】Valid Sudoku (easy)

题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否有重复或者无效就可以了  就是单纯的麻烦 不难 #include<iostream> #include<vector> using namespace std; class Solution { public: bool isValidSudoku(vector<vector&l

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n