Skip to content

Commit 87843d4

Browse files
committed
init commit
0 parents  commit 87843d4

File tree

16 files changed

+700
-0
lines changed

16 files changed

+700
-0
lines changed

.github/workflows/java.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Java
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Setup JDK 11
18+
uses: actions/setup-java@v3
19+
with:
20+
distribution: 'adopt'
21+
java-version: '11'
22+
- name: Grant execution permission for gradlew
23+
run: chmod +x gradlew
24+
- name: Cache Gradle packages
25+
uses: actions/cache@v3
26+
with:
27+
path: ~/.gradle/caches
28+
key: ${{ runner.os }}-gradle-${{ github.event.repository.name }}-${{ hashFiles('**/*.gradle')}}
29+
restore-keys: ${{ runner.os }}-gradle-${{ github.event.repository.name }}
30+
- name: Build with Gradlew
31+
run: ./gradlew build
32+
- name: Test with Gradlew
33+
run: ./gradlew :test --tests "SolutionTest"

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
./gradlew :test --tests "SolutionTest"

.idea/gradle.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# java_target_sum_input_array_sorted
2+
3+
Given a **1-indexed** array of integers `numbers` that is already ***sorted in non-decreasing order***, find two numbers such that they add up to a specific `target` number. Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`.
4+
5+
Return *the indices of the two numbers,* `index1` *and* `index2`***added by one** as an integer array* `[index1, index2]` *of length 2.*
6+
7+
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.
8+
9+
Your solution must use only constant extra space.
10+
11+
## Examples
12+
13+
**Example 1:**
14+
15+
```
16+
Input: numbers = [2,7,11,15], target = 9
17+
Output: [1,2]
18+
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
19+
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: numbers = [2,3,4], target = 6
26+
Output: [1,3]
27+
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
28+
29+
```
30+
31+
**Example 3:**
32+
33+
```
34+
Input: numbers = [-1,0], target = -1
35+
Output: [1,2]
36+
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
37+
38+
```
39+
40+
**Constraints:**
41+
42+
- `2 <= numbers.length <= $3 * 10^4$`
43+
- `1000 <= numbers[i] <= 1000`
44+
- `numbers` is sorted in **non-decreasing order**.
45+
- `1000 <= target <= 1000`
46+
- The tests are generated such that there is **exactly one solution**.
47+
48+
## 解析
49+
50+
給定一個由小到大排序過的整數陣列 numbers. 給定一個整數 target
51+
52+
要求實作一個演算法找出 numbers 中兩個數相加合的 target
53+
54+
並且要求空間複雜度必須要是 O(1)
55+
56+
因為是排序過的整數陣列
57+
58+
所以可以透過兩個 pointer , lp =0, rp = len(numbers) -1
59+
60+
當 lp < rp 時做以下判斷
61+
62+
當 numbers[lp] + numbers[rp] == target 則找到這兩個index 回傳 [lp + 1 , rp +1 ] // 因為題目要求是 1-indexed 的座標 而 golang 與 java 都是 0-indexed
63+
64+
當 numbers[lp] + numbers[rp] > target
65+
66+
因為左界已經到最小值,所以要讓兩數相加變小只能把右界向左移 更新 rp -=1
67+
68+
當 numbers[lp] + numbers[rp] < target
69+
70+
因為左界已經到最大值,所以要讓兩數相加變大只能把左界向右移 更新 lp +=1
71+
72+
當跑到最後都找不到則回傳 nil
73+
74+
詳細作法如下圖
75+
76+
![](https://i.imgur.com/knDWRGn.png)
77+
78+
79+
## 程式碼
80+
```java
81+
public class Solution {
82+
public int[] twoSum(int[] numbers, int target) {
83+
int lp =0, rp = numbers.length -1;
84+
while (lp < rp) {
85+
if (numbers[lp] + numbers[rp] == target) {
86+
return new int[]{lp + 1, rp + 1};
87+
}
88+
if (numbers[lp] + numbers[rp] > target) {
89+
rp--;
90+
}
91+
if (numbers[lp] + numbers[rp] < target) {
92+
lp++;
93+
}
94+
}
95+
return null;
96+
}
97+
}
98+
99+
```
100+
101+
## 困難點
102+
103+
1. 要想出左右指標移動的條件
104+
105+
## Solve Point
106+
107+
- [x] 初始化 lp = 0, rp = len(numbers) - 1
108+
- [x] 當 lp < rp 時 做以下運算
109+
- [x] 當 numbers[lp] + numbers[rp] == target 則回傳 [lp+1, rp+1]
110+
- [x] 當 numbers[lp] + numbers[rp] > target 則更新 rp -= 1
111+
- [x] 當 numbers[lp] + numbers[rp] < target 則更新 lp += 1
112+
- [x] 當跑到 lp = rp 時 , 回傳 nil 代表沒有找到符合的 pair

build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.example'
6+
version '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
14+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
}

gradle/wrapper/gradle-wrapper.jar

58.4 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)