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
Events
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 }}
JayFu
learningnote
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learningnote
/
twoSum.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
26 lines (24 loc) · 1.13 KB
master
Breadcrumbs
learningnote
/
twoSum.py
Copy path
Top
File metadata and controls
Code
Blame
26 lines (24 loc) · 1.13 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
#输入整数阵列和目标,输出可以相加得到目标的两个数字的下标
#输入nums = [2, 7, 11, 15],target = 9
##因为num[0]+num[1]=2+7=9
#输出[0, 1]
class Solution(object):
def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i, len(nums)):
# if nums[i]+nums[j]==target: print([i, j])
#该题为LeetCode上算法题,测试该网站代码以return作为答案标准,不能使用print
# if nums[i]+nums[j]==target: return [i, j]
#测试解答发现期望解只有一个,但是不能使用同一个元素
if nums[i]+nums[j]==target:
if i == j: continue
return [i, j]
#在LeetCode上解答的第一个算法题,运行时间为5102ms,击败了5.35%的提交者
#两层循环导致时间增多,查看别人源码,使用的是一层循环加上dic的方式
def twoSum(self, nums, target):
NumDic = {}
for i in range(len(nums)):
if target - nums[i] in NumDic:
return [NumDic[target-nums[i]], i]
NumDic[nums[i]] = i
return 0
You can’t perform that action at this time.