LeetCode Notebook 2020-2021
  • 序
  • 目录
  • 线性表
    • 数组
      • Remove Duplicates from Sorted Array
      • Remove Duplicates from Sorted Array II
      • Exercises I
      • Search in Rotated Sorted Array
      • Search in Rotated Sorted Array II
      • Median of Two Sorted Arrays
      • Longest Consecutive Sequence
      • Exercises II
      • Two Sum
      • 3Sum
      • 3Sum Closest
      • 4Sum
      • Exercises III
      • Next Permutation
      • Permutation Sequence
      • Exercises IV
      • Valid Sudoku
      • Trapping Rain Water
      • Rotate Image
      • Plus One
      • Exercises V
      • Climbing Stairs
      • Set Matrix Zeroes
      • Gray Code
      • Gas Station
      • Exercises VI
      • Candy
      • Single Number
      • Single Number II
      • Exercises VII
    • 链表
      • Add Two Numbers
      • Reverse Linked List II
      • Partition List
      • Swap-nodes-in-pairs
  • 字符串
    • 字符串
      • Valid Palindrome
      • Implement strStr()
      • String to Integer(atoi)
      • Add Binary
      • Longest Palindromic Substring
      • Longest Common Subsequence
      • Regular Expression Matching
      • Rearrange Spaces Between Words
      • Longest Common Prefix
      • Wildcard Matching
      • subdomain visit count
  • 栈和队列
    • 栈
      • Min Stack
      • Valid Parentheses
    • 队列
  • 树
    • 二叉树的遍历
      • Binary Tree Preorder Traversal
      • Binary Tree Inorder Traversal
      • Binary Tree Postorder Traversal
      • Search in a Binary Search Tree
    • 二叉树的构造
      • Untitled
    • 二叉查找树
      • Validate Binary Search Tree
    • 二叉树的递归
      • Untitled
  • 图
    • 图
  • 排序
    • 排序
      • Merge Sorted Array
      • Merge Two Sorted Lists
      • Merge k Sorted Lists
      • Exercises I
      • Insertion Sort List
      • Sort List
      • Sort Color
      • First Missing Positive
      • Exercises II
  • 查找
    • 查找
      • Random Pick with Weight
      • Prefix and Suffix Search
      • Search Insert Position
      • Search a 2D Matrix
  • BFS
    • BFS
  • DFS
    • DFS
      • Split a String Into the Max Number of Unique Substrings
      • Palindrome Partitioning
      • Unique Path
      • Unique Path II
  • 动态规划
    • DP
      • Coin Change
      • Maximal Rectangle
      • Super Egg Drop
      • Exercises
      • Best Team With No Conflicts
  • 分治法
    • Divide And Conquer
  • 贪心法
    • Greedy
      • Jump Game
      • Jump Game II
      • Best time to buy and sell stock
      • Best time to buy and sell stock II
      • Container with most water
      • Exercises
  • 暴力枚举
    • Brute Force
      • Subsets
      • Subsets II
      • Permutation
      • Permutation II
      • Combinations
      • Letter Combinations of a Phone Number
  • others
    • 小岛题
    • 滑动窗口
      • Longest substring without repeating characters
      • Sliding Window Maximum
      • Max Consecutive One III
      • Count Number of Nice Subarrays
    • Roman number code
    • Untitled
由 GitBook 提供支持
在本页
  • Plus One
  • 描述
  • 例子
  • 解法一
  • 解法二
  • 解法三

这有帮助吗?

  1. 线性表
  2. 数组

Plus One

easy 原题链接:https://leetcode.com/problems/plus-one/

上一页Rotate Image下一页Exercises V

最后更新于4年前

这有帮助吗?

Plus One

原题链接:

描述

Given a non-empty array of digits representing a non-negative integer, increment 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 contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

例子

Input: digits = [1,2,3]

Output: [1,2,4]

Explanation: The array represents the integer 123.

解法一

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        deque<int> dig;
        dig.assign(digits.begin(),digits.end());
	    for (int i = n - 1; i >= 0; --i)
	    {
		    if (dig[i] == 9)
		    {
			    dig[i] = 0;
                if(i==0){
                    dig.push_front(1);
                }
            }else{
                dig[i]++;
                break;
            }
	    }
        vector<int>res;
        res.assign(dig.begin(),dig.end());
        return res;
    }
};

解法二

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

解法三

class Solution {
public:
      vector<int> plusOne(vector<int> &digits) {
          add(digits, 1);
          return digits;
      }
private:
// 0 <= digit <= 9
    void add(vector<int> &digits, int digit) {
        int c = digit; // carry, 󿰤󿷰
        for (auto it = digits.rbegin(); it != digits.rend(); ++it) { 
            *it += c;
            c = *it / 10;
            *it %= 10; 
            }
        if (c > 0) digits.insert(digits.begin(), 1); 
        }
};

i

https://leetcode.com/problems/plus-one/
deque
nsert