Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
xuejing80
learnpython
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learnpython
/
Code
/
Leetcode
/
top-interview-questions-easy
/
Array27.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
47 lines (44 loc) · 2 KB
master
Breadcrumbs
learnpython
/
Code
/
Leetcode
/
top-interview-questions-easy
/
Array27.py
Copy path
Top
File metadata and controls
Code
Blame
47 lines (44 loc) · 2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'''
功能:加一
来源:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/27/
重点:使用range函数构建倒序循环、循环结构中else分支的使用方法
作者:薛景
最后修改于:2019/07/09
'''
# 方案一,传统方案,把数列中的每一个元素取出来构成一个整数,再将这个整数+1,然后
# 利用求余数的操作将结果从后向前逐位取出,添加到列表中
# 该方案战胜 51.51 % 的 python3 提交记录
class
Solution
:
def
plusOne
(
self
,
digits
:
list
)
->
list
:
n
=
0
for
i
in
digits
:
n
=
n
*
10
+
i
n
=
n
+
1
res
=
[]
while
n
>
0
:
# insert方法的第一个参数表示位置(下标),第二个参数表示要插入的数据
res
.
insert
(
0
,
n
%
10
)
n
=
n
//
10
return
res
# 方案二,优化方案,通过观察,我们发现只有数字9,在+1的时候会产生进位,所以我们
# 只需对原始列表,从后向前进行遍历,将连续的值为9的元素替换成0,再将这些连续的9
# 之前的那个元素+1,就可以得到答案了
# 该方案战胜 98.17 % 的 python3 提交记录
class
Solution
:
def
plusOne
(
self
,
digits
:
list
)
->
list
:
# 这里必须认真观察range函数的三个参数,它们分别表示起始值、终止值(这个
# 值是取不到的)、步进值(递增或递减值)
for
index
in
range
(
len
(
digits
)
-
1
,
-
1
,
-
1
):
if
digits
[
index
]
==
9
:
digits
[
index
]
=
0
# 如果该位是9,则替换成0
else
:
digits
[
index
]
+=
1
# 若不为9,则+1,结束循环
break
# 这个else分支用来处理每一位都是9的情况,即需要在这个数列最前方插入元素1
else
:
digits
.
insert
(
0
,
1
)
return
digits
# 以下是本地测试代码,提交时只需复制上面的代码块即可
solution
=
Solution
()
digits
=
[
9
,
9
]
print
(
solution
.
plusOne
(
digits
))
You can’t perform that action at this time.