mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 125
This commit is contained in:
18
Algorithms/125.Valid-Palindrome/README.md
Normal file
18
Algorithms/125.Valid-Palindrome/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
|
||||
|
||||
For example,
|
||||
"A man, a plan, a canal: Panama" is a palindrome.
|
||||
"race a car" is not a palindrome.
|
||||
|
||||
Note:
|
||||
Have you consider that the string might be empty? This is a good question to ask during an interview.
|
||||
|
||||
For the purpose of this problem, we define empty string as valid palindrome.
|
||||
|
||||
## 题目大意
|
||||
|
||||
判断所给的字符串是否是有效的回文串
|
||||
35
Algorithms/125.Valid-Palindrome/Valid Palindrome.go
Normal file
35
Algorithms/125.Valid-Palindrome/Valid Palindrome.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func isPalindrome(s string) bool {
|
||||
|
||||
s = strings.ToLower(s)
|
||||
|
||||
i, j := 0, len(s)-1
|
||||
for i < j {
|
||||
for i < j && !isChar(s[i]) {
|
||||
i++
|
||||
}
|
||||
for i < j && !isChar(s[j]) {
|
||||
j--
|
||||
}
|
||||
if s[i] != s[j] {
|
||||
return false
|
||||
}
|
||||
i++
|
||||
j--
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 判断 c 是否是字符或者数字
|
||||
func isChar(c byte) bool {
|
||||
if ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
41
Algorithms/125.Valid-Palindrome/Valid Palindrome_test.go
Normal file
41
Algorithms/125.Valid-Palindrome/Valid Palindrome_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem125(t *testing.T) {
|
||||
|
||||
tcs := []struct {
|
||||
s string
|
||||
ans bool
|
||||
}{
|
||||
|
||||
{
|
||||
"0p",
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"0",
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
"race a car",
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"A man, a plan, a canal: Panama",
|
||||
true,
|
||||
},
|
||||
}
|
||||
fmt.Printf("------------------------Leetcode Problem 125------------------------\n")
|
||||
|
||||
for _, tc := range tcs {
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", tc, isPalindrome(tc.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
@@ -27,4 +27,11 @@ Output: 1
|
||||
|
||||
Note:
|
||||
|
||||
Your algorithm should run in O(n) time and uses constant extra space.
|
||||
Your algorithm should run in O(n) time and uses constant extra space.
|
||||
|
||||
## 题目大意
|
||||
|
||||
找到缺失的第一个正整数。
|
||||
|
||||
|
||||
为了减少时间复杂度,可以把 input 数组都装到 map 中,然后 i 循环从 1 开始,依次比对 map 中是否存在 i,只要不存在 i 就立即返回结果,即所求。
|
||||
@@ -30,3 +30,7 @@ Return:
|
||||
"FizzBuzz"
|
||||
]
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
3的倍数输出 "Fizz",5的倍数输出 "Buzz",15的倍数输出 "FizzBuzz",其他时候都输出原本的数字。
|
||||
@@ -5,4 +5,10 @@
|
||||
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
|
||||
|
||||
Note:
|
||||
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
|
||||
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
|
||||
|
||||
## 题目大意
|
||||
|
||||
合并两个已经有序的数组,结果放在第一个数组中,第一个数组假设空间足够大。要求算法时间复杂度足够低。
|
||||
|
||||
为了不大量移动元素,就要从2个数组长度之和的最后一个位置开始,依次选取两个数组中大的数,从第一个数组的尾巴开始往头放,只要循环一次以后,就生成了合并以后的数组了。
|
||||
Reference in New Issue
Block a user