Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid Palindrome #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pullrequests/valid_palindrome/step1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//lint:file-ignore U1000 Ignore all unused code
package template

import "unicode"

/*
時間:12分

解法自体はすぐに思いついたが、すんなり実装できないところがまだまだ課題。
continueすることを忘れていたりなどケアレスミスが目立った。
*/
func isPalindromeStep1(s string) bool {
sRunes := []rune(s)
for i, j := 0, len(s)-1; i < j; {
if !(unicode.IsDigit(sRunes[i]) || unicode.IsLetter(sRunes[i])) {
i++
continue
}
if !(unicode.IsDigit(sRunes[j]) || unicode.IsLetter(sRunes[j])) {
j--
continue
}
if unicode.ToLower(sRunes[i]) != unicode.ToLower(sRunes[j]) {
return false
}
i++
j--
}
return true
}
30 changes: 30 additions & 0 deletions pullrequests/valid_palindrome/step2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//lint:file-ignore U1000 Ignore all unused code
package template

import "unicode"

/*
Two Pointersなのでi, jではなく、left, rightを使うようにした。

質問:sをrune型配列にキャストした変数名を`sRunes`や`runeS`などにしてみたのですが、あまりしっくりきてないです。良い名前や`sRunes`と`runeS`ならどちらの方が良さげなどありますか
rihib marked this conversation as resolved.
Show resolved Hide resolved
*/
func isPalindromeStep2(s string) bool {
runeS := []rune(s)
left, right := 0, len(s)-1
for left < right {
if !(unicode.IsDigit(runeS[left]) || unicode.IsLetter(runeS[left])) {
left++
continue
}
if !(unicode.IsDigit(runeS[right]) || unicode.IsLetter(runeS[right])) {
right--
continue
}
if unicode.ToLower(runeS[left]) != unicode.ToLower(runeS[right]) {
return false
}
left++
right--
}
return true
}