diff --git a/Algorithms/125.Valid-Palindrome/README.md b/Algorithms/125.Valid-Palindrome/README.md new file mode 100644 index 00000000..9ddcff27 --- /dev/null +++ b/Algorithms/125.Valid-Palindrome/README.md @@ -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. + +## 题目大意 + +判断所给的字符串是否是有效的回文串 \ No newline at end of file diff --git a/Algorithms/125.Valid-Palindrome/Valid Palindrome.go b/Algorithms/125.Valid-Palindrome/Valid Palindrome.go new file mode 100644 index 00000000..dad5d16e --- /dev/null +++ b/Algorithms/125.Valid-Palindrome/Valid Palindrome.go @@ -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 +} diff --git a/Algorithms/125.Valid-Palindrome/Valid Palindrome_test.go b/Algorithms/125.Valid-Palindrome/Valid Palindrome_test.go new file mode 100644 index 00000000..db04a54c --- /dev/null +++ b/Algorithms/125.Valid-Palindrome/Valid Palindrome_test.go @@ -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") +} diff --git a/Algorithms/41.First-Missing-Positive/README.md b/Algorithms/41.First-Missing-Positive/README.md index 8c54813d..66a00207 100644 --- a/Algorithms/41.First-Missing-Positive/README.md +++ b/Algorithms/41.First-Missing-Positive/README.md @@ -27,4 +27,11 @@ Output: 1 Note: -Your algorithm should run in O(n) time and uses constant extra space. \ No newline at end of file +Your algorithm should run in O(n) time and uses constant extra space. + +## 题目大意 + +找到缺失的第一个正整数。 + + +为了减少时间复杂度,可以把 input 数组都装到 map 中,然后 i 循环从 1 开始,依次比对 map 中是否存在 i,只要不存在 i 就立即返回结果,即所求。 \ No newline at end of file diff --git a/Algorithms/412.Fizz-Buzz/README.md b/Algorithms/412.Fizz-Buzz/README.md index 6bf77cdd..84078dda 100644 --- a/Algorithms/412.Fizz-Buzz/README.md +++ b/Algorithms/412.Fizz-Buzz/README.md @@ -30,3 +30,7 @@ Return: "FizzBuzz" ] ``` + +## 题目大意 + +3的倍数输出 "Fizz",5的倍数输出 "Buzz",15的倍数输出 "FizzBuzz",其他时候都输出原本的数字。 \ No newline at end of file diff --git a/Algorithms/88.Merge-Sorted-Array/README.md b/Algorithms/88.Merge-Sorted-Array/README.md index a36f5b6f..211896e0 100644 --- a/Algorithms/88.Merge-Sorted-Array/README.md +++ b/Algorithms/88.Merge-Sorted-Array/README.md @@ -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. \ No newline at end of file +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个数组长度之和的最后一个位置开始,依次选取两个数组中大的数,从第一个数组的尾巴开始往头放,只要循环一次以后,就生成了合并以后的数组了。 \ No newline at end of file