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
/
Array23.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
30 lines (28 loc) · 1.32 KB
master
Breadcrumbs
learnpython
/
Code
/
Leetcode
/
top-interview-questions-easy
/
Array23.py
Copy path
Top
File metadata and controls
Code
Blame
30 lines (28 loc) · 1.32 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
'''
功能:旋转数组
来源:https://leetcode-cn.com/explore/featured/card/top-interview-questions-easy/1/array/23/
重点:切片运算
作者:薛景
最后修改于:2019/07/04
'''
# 使用列表的切片操作,可以轻松完成列表元素的后移操作
# 该方案战胜 99.41 % 的 python3 提交记录
class
Solution
:
def
rotate
(
self
,
nums
:
list
,
k
:
int
)
->
None
:
"""
Do not return anything, modify nums in-place instead.
"""
# 这个题目有个恶心的测试用例,就是k非常大,大过了数列长度,所以我们要想
# 到如果k等于数列长度,就等价于数列没有发生移动,所以我们对k做一个求余
# 数的运算。举例:数列长度为2,移动5位,那么其实等价于移动1位的效果
k
=
k
%
len
(
nums
)
# 另外,如果k为0,即不做任何移动
if
k
>
0
:
nums
[
k
:]
=
nums
# 从第k位开始存放原来的数列,即后移操作
nums
[:
k
]
=
nums
[
-
k
:]
# 把最后几个超出原数列长度的数据复制到数列的开头
nums
[
-
k
:]
=
[]
# 复制完成后,删除超出原长度的数据
# 以下是本地测试代码,提交时只需复制上面的代码块即可
solution
=
Solution
()
nums
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
]
solution
.
rotate
(
nums
,
3
)
print
(
nums
)
You can’t perform that action at this time.