GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Algorithm/leetcode_solved/leetcode_0066_Plus_One.cpp at master · OneCodeMonkey/Algorithm

Expand file tree

/

leetcode_0066_Plus_One.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
32 lines (30 loc) · 855 Bytes
/

leetcode_0066_Plus_One.cpp

Copy path

File metadata and controls

32 lines (30 loc) · 855 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* AC:
* Runtime: 4 ms, faster than 65.48% of C++ online submissions for Plus One.
* Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Plus One.
* 思路:写一个按位递进的判断,用一个变量记录上一个位置有无进位就行了。
*
*/
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int size = digits.size();
vector<int> ans;
int flag = 1; // 是否需要进位
for(int i = size - 1; i >= 0; i--) {
if(flag == 1) {
if(digits[i] + 1 >= 10) {
digits[i] = (digits[i] + 1) % 10;
flag = 1;
if(i == 0) {
digits.insert(digits.begin(), 1);
}
} else {
digits[i] += 1;
flag = 0;
}
}
}
return digits;
}
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.