Skip to content

Commit ab5595c

Browse files
committed
init comit
0 parents  commit ab5595c

File tree

16 files changed

+759
-0
lines changed

16 files changed

+759
-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: 127 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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# java_valid_palindrome
2+
3+
A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
4+
5+
Given a string `s`, return `true` *if it is a **palindrome**, or* `false` *otherwise*.
6+
7+
## Examples
8+
9+
**Example 1:**
10+
11+
```
12+
Input: s = "A man, a plan, a canal: Panama"
13+
Output: true
14+
Explanation: "amanaplanacanalpanama" is a palindrome.
15+
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: s = "race a car"
22+
Output: false
23+
Explanation: "raceacar" is not a palindrome.
24+
25+
```
26+
27+
**Example 3:**
28+
29+
```
30+
Input: s = " "
31+
Output: true
32+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
33+
Since an empty string reads the same forward and backward, it is a palindrome.
34+
```
35+
36+
**Constraints:**
37+
38+
- `1 <= s.length <= 2 * 105`
39+
- `s` consists only of printable ASCII characters.
40+
41+
## 解析
42+
43+
給定一個字串 s ,
44+
45+
假設經過一個運算只看是英文字母的字元,且把所有大寫英文字元轉成小寫後變成 s’
46+
47+
要求寫一個演算法判斷 s 在經過以上轉換後是否是回文字串
48+
49+
一個字串 s 如果是回文, 代表 i = 0..(n/2-1)
50+
51+
s[i] = s[n-1-i], where n = len(s)
52+
53+
由以上定義可以發現透過給定 2 個指標 一個從字串最前面 一個從字串最後面
54+
55+
當兩個指標位置不同時做以下比較
56+
57+
每次當遇到兩個字元相同時,兩個指標共同推進一個單位往下一個可以比較的字元去比較
58+
59+
遇到兩個字元不同時,則代表不是回文 回傳 false
60+
61+
特別要注意的是當其中一個指標遇到不是非英文字元,則需要直接把指標往下一個位置移動
62+
63+
然後往一下個開始指標開始比較
64+
65+
假設直到最後一個指標都相同則代表是回文 所以回傳 true
66+
67+
演算法詳細如下圖
68+
69+
![](https://i.imgur.com/BxTUXpO.png)
70+
71+
## 程式碼
72+
```java
73+
public class Solution {
74+
public boolean isPalindrome(String s) {
75+
int lp = 0, rp = s.length() - 1;
76+
while(lp <= rp) {
77+
if (!isAlphabet(s.charAt(lp))) {
78+
lp++;
79+
continue;
80+
}
81+
if (!isAlphabet(s.charAt(rp))) {
82+
rp--;
83+
continue;
84+
}
85+
if (toLower(s.charAt(rp)) != toLower(s.charAt(lp)) ) {
86+
return false;
87+
}
88+
lp++;
89+
rp--;
90+
}
91+
return true;
92+
}
93+
private boolean isAlphabet(char c) {
94+
if (('a' <= c && c < 'z') || ('A' <= c && c < 'Z') || ('0' <= c && c <= '9')) {
95+
return true;
96+
}
97+
return false;
98+
}
99+
private char toLower(char c) {
100+
if ('A' <= c && c < 'Z') {
101+
return (char)(c - 'A' + 'a');
102+
}
103+
return c;
104+
}
105+
}
106+
107+
```
108+
109+
## 困難點
110+
111+
1. 從 palindrome 定義思考出透過左右指標同時逼近來檢查
112+
113+
## Solve Point
114+
115+
- [x] 初始化 lp = 0, rp = len(s) -1
116+
- [x] for lp ≤ rp 做以下運算
117+
- [x] 當 s[lp] 不在 ‘a’,’z’ 之間 且不在 ‘A’ ,‘Z’之間, 更新 lp += 1 continue
118+
- [x] 當 s[rp] 不在 ‘a’,’z’ 之間 且不在 ‘A’ ,‘Z’之間, 更新 rp -= 1 continue
119+
- [x] 當 toLower(s[rp]) ≠ toLower(s[lp]) , 回傳 false
120+
- [x] rp—; lp++
121+
- [x] 回傳 true

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)