Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Type
/
to 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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
UtilitiesCore
/
leetcode
Public
forked from
reedfan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
leetcode
/
src
/
main
/
java
/
leetcode
/
array
/
N001twoSum.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
39 lines (30 loc) · 1.52 KB
master
Breadcrumbs
leetcode
/
src
/
main
/
java
/
leetcode
/
array
/
N001twoSum.java
Copy path
Top
File metadata and controls
Code
Blame
39 lines (30 loc) · 1.52 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
package
leetcode
.
array
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
/*
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public
class
N001twoSum
{
/*
本题最简单的方法为两重循环暴力破解,但是这种方式时间复杂度为O(n^2),效率很低,面试中这种代码显然是不能让面试官满意的。我们可以利用hashMap来减少查询时间。hashmap的key为某个位置上的值,value为此位置的坐标。如果target - nums[i]为hashmap的某个key。则找到了两个数的和为target,返回这两个值即可。如果target - nums[i]不在hashmap的key中。则将其加入hashmap中。
*/
public
int
[]
twoSum
(
int
[]
nums
,
int
target
) {
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<>();
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++) {
int
value
=
target
-
nums
[
i
];
if
(
map
.
containsKey
(
value
)) {
return
new
int
[] {
map
.
get
(
value
),
i
};
}
map
.
put
(
nums
[
i
],
i
);
}
return
null
;
}
}
You can’t perform that action at this time.