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 }}
JsonChao
Awesome-Algorithm-Study
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Awesome-Algorithm-Study
/
data_struct_study
/
src
/
array_problem
/
Solution167.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
55 lines (48 loc) · 1.81 KB
master
Breadcrumbs
Awesome-Algorithm-Study
/
data_struct_study
/
src
/
array_problem
/
Solution167.java
Copy path
Top
File metadata and controls
Code
Blame
55 lines (48 loc) · 1.81 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
48
49
50
51
52
53
54
55
package array_problem;
import java.util.Arrays;
/**
* 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。
* 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。
*
* 说明:
* 返回的下标值(index1 和 index2)不是从零开始的。
* 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。
* 示例:
*
* 输入: numbers = [2, 7, 11, 15], target = 9
* 输出: [1,2]
* 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
*
* 1、如果没有解?保证有解。
* 2、如果有多个解?返回任意解。
* 3、双层遍历 O(n^2)。(没有利用有序)
* 4、循环 + 二分查找 O(nlogn):在有序数组中寻找 target - nums[i]。
* 5、对撞指针:使用两个索引,两个索引在中间的位置靠近。
*
* O(n ^ 2)
* O(1)
*/
public class Solution167 {
// 1、双层遍历每一种可能的组合:时间复杂度 O(n^2)。(没有利用有序)
public int[] twoSum(int[] arr, int target) {
// 1、有效性判断
int n = arr.length;
if (n < 2) {
throw new IllegalArgumentException("length of arr is illegal");
}
// 2、双层for循环查找每一种可能的组合
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
int[] res = {i + 1, j + 1};
return res;
}
}
}
throw new IllegalArgumentException("no target!");
}
public static void main(String[] args) {
int[] arr = {2, 7, 11, 15};
System.out.println(Arrays.toString(new Solution167().twoSum(arr, 9)));
}
}
You can’t perform that action at this time.