diff --git a/001_two_sum/Makefile b/001_two_sum/Makefile index ffb6456..b5c5338 100644 --- a/001_two_sum/Makefile +++ b/001_two_sum/Makefile @@ -1,2 +1,3 @@ all: gcc -O2 -o test two_sum.c + go build twosum.go diff --git a/001_two_sum/readme b/001_two_sum/readme new file mode 100644 index 0000000..c60fbf2 --- /dev/null +++ b/001_two_sum/readme @@ -0,0 +1,12 @@ +Given an array of integers, return indices of the two numbers such that they add up to a specific target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +Example: + +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` diff --git a/001_two_sum/test b/001_two_sum/test new file mode 100755 index 0000000..2ee40c6 Binary files /dev/null and b/001_two_sum/test differ diff --git a/001_two_sum/twosum b/001_two_sum/twosum new file mode 100755 index 0000000..a3d8da3 Binary files /dev/null and b/001_two_sum/twosum differ diff --git a/001_two_sum/twosum.go b/001_two_sum/twosum.go new file mode 100644 index 0000000..45bdfec --- /dev/null +++ b/001_two_sum/twosum.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" +) + +func twoSum(nums []int, target int) []int { + l := len(nums) + tmpMap := make(map[int]int, len(nums)) + for i := 0; i < l; i++ { + tmpMap[nums[i]] = i + } + + for i := 0; i < l; i++ { + index, ok := tmpMap[target-nums[i]] + if ok && i != index { + return []int{i, index} + } + } + return nil +} + +func main() { + // nums := []int{1, 2, 3, 4, 5} + //target := 8 + nums := []int{11, 15, 1, 8, 0, 9} + target := 9 + + ret := twoSum(nums, target) + fmt.Printf("ret = %v\n", ret) +} diff --git a/002_add_two_numbers/addTwoNumber.go b/002_add_two_numbers/addTwoNumber.go new file mode 100644 index 0000000..3fdefaf --- /dev/null +++ b/002_add_two_numbers/addTwoNumber.go @@ -0,0 +1,125 @@ +package main + +import "fmt" + +type ListNode struct { + Val int + Next *ListNode +} + +func (h *ListNode) Show() { + for i := h; ; i = i.Next { + if i.Next == nil { + fmt.Printf("%v\n", i.Val) + return + } + fmt.Printf("%v -> ", i.Val) + } +} +func CreateList(v int) *ListNode { + h := new(ListNode) + l := h + for v != 0 { + l.Next = new(ListNode) + l.Next.Val = v % 10 + v = v / 10 + l = l.Next + } + return h.Next +} +func main() { + L1 := CreateList(89) + L2 := CreateList(1) + L1.Show() + L2.Show() + + L3 := addTwoNumbers(L1, L2) + L3.Show() + +} + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ + +func checkList(l1 *ListNode, l3 *ListNode, flag int) { + if l1 != nil { + l3.Next = l1 + tmp := flag + flag = (l1.Val + tmp) / 10 + l1.Val = (l1.Val + tmp) % 10 + for flag != 0 { + if l1.Next != nil { + l1 = l1.Next + tmp := flag + flag = (l1.Val + tmp) / 10 + l1.Val = (l1.Val + tmp) % 10 + } else { + l1.Next = new(ListNode) + l1.Next.Val = flag + break + } + } + } +} +func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { + l3 := new(ListNode) + h := l3 + flag := 0 + + for l1 != nil && l2 != nil { + l3.Next = new(ListNode) + l3 = l3.Next + l3.Val = (l1.Val + l2.Val + flag) % 10 + flag = (l1.Val + l2.Val + flag) / 10 + l1 = l1.Next + l2 = l2.Next + } + checkList(l1, l3, flag) + checkList(l2, l3, flag) + if l1 == nil && l2 == nil && flag != 0 { + l3.Next = new(ListNode) + l3.Next.Val = flag + } + return h.Next +} + +// the optimal version + +//func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { +// ret := new(ListNode) +// var tmp *ListNode +// last := 0 +// for { +// sum := last +// if l1 == nil && l2 == nil { +// break +// } else if l1 == nil { +// sum += l2.Val +// l2 = l2.Next +// } else if l2 == nil { +// sum += l1.Val +// l1 = l1.Next +// } else { +// sum += l1.Val + l2.Val +// l1 = l1.Next +// l2 = l2.Next +// } +// if tmp == nil { +// tmp = ret +// } else { +// tmp.Next = new(ListNode) +// tmp = tmp.Next +// } +// tmp.Val = sum % 10 +// last = sum / 10 +// } +// if last == 1 { +// tmp.Next = &ListNode{Val: 1, Next: nil} +// } +// return ret +//} diff --git a/002_add_two_numbers/readme b/002_add_two_numbers/readme new file mode 100644 index 0000000..68a7588 --- /dev/null +++ b/002_add_two_numbers/readme @@ -0,0 +1,8 @@ +You are given two non-empty linked lists representing two non-negative integers. +The digits are stored in reverse order and each of their nodes contain a single digit. +Add the two numbers and return it as a linked list. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself. + +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 0 -> 8 \ No newline at end of file diff --git a/003_longest_substring_without_repeat/longSubStr.go b/003_longest_substring_without_repeat/longSubStr.go new file mode 100644 index 0000000..6da8dd9 --- /dev/null +++ b/003_longest_substring_without_repeat/longSubStr.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "log" +) + +type HashSet struct { + m map[rune]bool +} + +func NewHashSet() *HashSet { + return &HashSet{m: make(map[rune]bool)} +} + +func (set *HashSet) Add(c rune) bool { + if !set.m[c] { + set.m[c] = true + return true + } + return false +} +func (set *HashSet) Remove(c rune) { + delete(set.m, c) +} +func (set *HashSet) Contains(c rune) bool { + return set.m[c] +} + +func max(a, b int) int { + if a < b { + return b + } else { + return a + } +} +func main() { + i := lengthOfLongestSubstring("abcabcbb") + fmt.Printf("i:%v\n", i) + i = lengthOfLongestSubstring("bbb") + fmt.Printf("i:%v\n", i) + i = a("bbb") + fmt.Printf("i:%v\n", i) +} + +func lengthOfLongestSubstring(s string) int { + lenSub := 0 + strBytes := []byte(s) + strLen := len(s) + set := NewHashSet() + for i, j := 0, 0; i < strLen && j < strLen; { + if !set.Contains(rune(strBytes[j])) { + set.Add(rune(strBytes[j])) + log.Printf("set add %c\n", rune(strBytes[j])) + lenSub = max(lenSub, j-i) + j++ + } else { + set.Remove(rune(strBytes[i])) + i++ + } + } + return lenSub + 1 +} + +// The best solution: +// The character arrary contains the last index of all characters. +// At every loop ,Max is updateed to ensure that the Max is the highest index +// of all characters +// the variable count is the length of substring +// the variable last is the last index of substring that contains different characters + +// func lengthOfLongestSubstring(s string) int { +// character := [128]int{} +// max, count, last := 0, 0, 0 +// for index, ch := range s { +// if character[ch] < last { +// count += 1 +// } else { +// last = character[ch] +// if count > max { +// max = count +// } +// count = index + 1 - last +// } +// character[ch] = index + 1 +// } +// if count > max { +// max = count +// } +// return max +// } +//func lengthOfLongestSubstring(s string) int { +func a(s string) int { + char := make(map[rune]int) + max, last, count := 0, 0, 0 + for index, ch := range s { + if char[ch] < last { + count += 1 + } else { + last = char[ch] + if count > max { + max = count + } + count = index + 1 - last + } + char[ch] = index + 1 + } + if max < count { + max = count + } + return max +} diff --git a/003_longest_substring_without_repeat/readme.md b/003_longest_substring_without_repeat/readme.md new file mode 100644 index 0000000..1c02d6d --- /dev/null +++ b/003_longest_substring_without_repeat/readme.md @@ -0,0 +1,12 @@ +Given a string, find the length of the longest substring without repeating characters. + +Examples: + +Given "abcabcbb", the answer is "abc", which the length is 3. + +Given "bbbbb", the answer is "b", with the length of 1. + +Given "pwwkew", the answer is "wke", with the length of 3. +Note that the answer must be a substring, "pwke" is a subsequence and not a substring. + +[reference](http://www.lpnote.com/2017/09/08/leetcode-3-longest-substring-without-repeating-characters/) diff --git a/004_median_of_two_sorted_array/Makefile b/004_median_of_two_sorted_array/Makefile index 6fb3d51..761366f 100644 --- a/004_median_of_two_sorted_array/Makefile +++ b/004_median_of_two_sorted_array/Makefile @@ -1,2 +1,3 @@ all: gcc -O2 -o test median_of_two_sorted_array.c + go build medianSortedArray.go diff --git a/004_median_of_two_sorted_array/medianSortedArray.go b/004_median_of_two_sorted_array/medianSortedArray.go new file mode 100644 index 0000000..72a070e --- /dev/null +++ b/004_median_of_two_sorted_array/medianSortedArray.go @@ -0,0 +1,76 @@ +package main + +import "fmt" + +func main() { + a1 := []int{} + a2 := []int{1} + b1 := []int{1, 2} + b2 := []int{3, 4} + + fmt.Printf("a1,a2:%v\n", findMedianSortedArrays(a1, a2)) + fmt.Printf("b1,b2:%v\n", findMedianSortedArrays(b1, b2)) +} +// runtime test:38ms + +func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { + len1 := len(nums1) + len2 := len(nums2) + array := make([]int, len1+len2) + var i, j int + var k int + for i, j, k = 0, 0, 0; i < len1 && j < len2; { + switch t := nums1[i] - nums2[j]; { + case t < 0: + array[k] = nums1[i] + i++ + k++ + case t > 0: + array[k] = nums2[j] + j++ + k++ + case t == 0: + array[k] = nums1[i] + k++ + i++ + array[k] = nums2[j] + k++ + j++ + } + } + if i != len1 { + for ; i < len1; i++ { + array[k] = nums1[i] + k++ + } + } + if j != len2 { + for ; j < len2; j++ { + array[k] = nums2[j] + k++ + } + } + fmt.Printf("array :%v\n", array) + lenArray := len(array) + if lenArray%2 == 0 { + return float64((array[lenArray/2-1] + array[lenArray/2])) / 2 + } else { + return float64(array[lenArray/2]) + } +} + +//the simplest way runtime test :52ms +// import "sort" +//func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { +// merged := append(nums1, nums2...) +// sort.Ints(merged) +// +// totalLen := len(merged) +// if totalLen % 2 == 0 { +// +// return float64((merged[(totalLen/2)-1] + merged[totalLen/2])) / 2.0 +// } else { +// +// return float64(merged[totalLen/2]) +// } +//} diff --git a/004_median_of_two_sorted_array/readme.md b/004_median_of_two_sorted_array/readme.md new file mode 100644 index 0000000..f960234 --- /dev/null +++ b/004_median_of_two_sorted_array/readme.md @@ -0,0 +1,14 @@ +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +Example 1: +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 +Example 2: +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 \ No newline at end of file diff --git a/004_median_of_two_sorted_array/test b/004_median_of_two_sorted_array/test new file mode 100755 index 0000000..bf5b072 Binary files /dev/null and b/004_median_of_two_sorted_array/test differ diff --git a/005_longest_palindromic_substring/Makefile b/005_longest_palindromic_substring/Makefile index d2434e6..78a0f2a 100644 --- a/005_longest_palindromic_substring/Makefile +++ b/005_longest_palindromic_substring/Makefile @@ -1,2 +1,3 @@ all: gcc -O2 -o test longest_palindromic_substring.c + go build longestPalindromicSubStr.go diff --git a/005_longest_palindromic_substring/longestPalindromicSubStr.go b/005_longest_palindromic_substring/longestPalindromicSubStr.go new file mode 100644 index 0000000..bf66edc --- /dev/null +++ b/005_longest_palindromic_substring/longestPalindromicSubStr.go @@ -0,0 +1,70 @@ +package main + +import "fmt" + +func main() { + // s := "cabadda" + //s := "caababa" + s := "abcbaabcba" + fmt.Printf("the %s longest palindromic substring :%v\n", s, longestPalindrome(s)) +} + +func isPalindromic(s []byte, head, tail int) bool { + if s[head] == s[tail] { + if head < tail { + return isPalindromic(s, head+1, tail-1) + } + if head >= tail { + return true + } + } + return false +} +func maxString(a, b string) string { + if len(a) > len(b) { + return a + } + return b +} + +func longestPalindrome(s string) (maxStr string) { + for i, j := len(s)-1, 0; i >= 0; i-- { + for ; j < i; j++ { + if isPalindromic([]byte(s), j, i) { + maxStr = maxString(maxStr, s[j:i+1]) + break + } + + } + i = j + } + return +} + +// func longestPalindrome(s string) string { +// maxS := "" +// for i := 0; i < len(s); { +// j := i+1 +// for j < len(s) && s[i] == s[j] { +// j++ +// } +// maxS = maxString(maxS, getLongestPalindromeAt(s, i, j-1)) +// i = j +// } +// return maxS +// } + +// func getLongestPalindromeAt(s string, i, j int) string { +// for i >= 0 && j < len(s) && s[i] == s[j] { +// i-- +// j++ +// } +// return s[i+1:j] +// } + +// func maxString(a, b string) string { +// if len(a) > len(b) { +// return a +// } +// return b +// } diff --git a/005_longest_palindromic_substring/readme.md b/005_longest_palindromic_substring/readme.md new file mode 100644 index 0000000..3cc0f90 --- /dev/null +++ b/005_longest_palindromic_substring/readme.md @@ -0,0 +1,14 @@ +Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. + +Example: + +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. +Example: + +Input: "cbbd" + +Output: "bb" diff --git a/006_zigzag_conversion/readme.md b/006_zigzag_conversion/readme.md new file mode 100644 index 0000000..46d8c43 --- /dev/null +++ b/006_zigzag_conversion/readme.md @@ -0,0 +1,13 @@ +/*n=numRows +Δ=2n-2 1 2n-1 4n-3 +Δ= 2 2n-2 2n 4n-4 4n-2 +Δ= 3 2n-3 2n+1 4n-5 . +Δ= . . . . . +Δ= . n+2 . 3n . +Δ= n-1 n+1 3n-3 3n-1 5n-5 +Δ=2n-2 n 3n-2 5n-4 +*/ + + a zigzag pattern + + diff --git a/006_zigzag_conversion/zigzagConversion.go b/006_zigzag_conversion/zigzagConversion.go new file mode 100644 index 0000000..5ffaee7 --- /dev/null +++ b/006_zigzag_conversion/zigzagConversion.go @@ -0,0 +1,38 @@ +package main + +import ( + "bytes" + "fmt" +) + +func main() { + fmt.Println("string: " + convert("PAYPALISHIRING", 3)) + fmt.Println("string: " + convert("A", 1)) + fmt.Println("string: " + convert("ABCD", 2)) + fmt.Println("string: " + convert("ABCDEF", 4)) +} + +func convert(s string, numRows int) string { + if numRows == 1 { + return s + } + convertedStr := make([][]byte, numRows) + n := len(s) + ret := new(bytes.Buffer) + + for i := 0; i < n; { + for j := 0; j < numRows && i < n; j++ { + convertedStr[j] = append(convertedStr[j], s[i]) + i++ + } + for j := numRows - 2; j > 0 && i < n; j-- { + convertedStr[j] = append(convertedStr[j], s[i]) + i++ + } + } + for _, v := range convertedStr { + ret.WriteString(string(v)) + + } + return ret.String() +} diff --git a/007_reverse_integer/readme.md b/007_reverse_integer/readme.md new file mode 100644 index 0000000..e985327 --- /dev/null +++ b/007_reverse_integer/readme.md @@ -0,0 +1,16 @@ +Given a 32-bit signed integer, reverse digits of an integer. + +Example 1: + +Input: 123 +Output: 321 +Example 2: + +Input: -123 +Output: -321 +Example 3: + +Input: 120 +Output: 21 +Note: +Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. diff --git a/007_reverse_integer/reverseInteger.go b/007_reverse_integer/reverseInteger.go new file mode 100644 index 0000000..d6f1b93 --- /dev/null +++ b/007_reverse_integer/reverseInteger.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + fmt.Printf("x :%v\n", reverse(123)) + fmt.Printf("x :%v\n", reverse(1534236469)) +} + +func reverse(x int) int { + var ret int + for x/10 != 0 { + ret = ret*10 + x%10 + x = (x - x%10) / 10 + } + ret = ret*10 + x%10 + if ret > math.MaxInt32 || ret < math.MinInt32 { + return 0 + } + return ret + +} + +// func reverse(x int) int { +// a := x / 10 +// b := x % 10 +// integer := []int{} +// for a != 0 { +// integer = append(integer, b) +// b = a % 10 +// a = a / 10 +// } +// integer = append(integer, b) +// if integer[0] == 0 { +// integer = integer[1:] +// } +// ret := 0 +// for _, v := range integer { +// ret = ret*10 + v +// } +// if ret > 2147483647 || ret < -2147483647 { +// return 0 +// } +// return ret +// } diff --git a/009_palindrome_number/palindromeNumber.go b/009_palindrome_number/palindromeNumber.go new file mode 100644 index 0000000..5aedca6 --- /dev/null +++ b/009_palindrome_number/palindromeNumber.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" +) + +func main() { + i := -2147447412 + fmt.Printf("The number %v is Palindrome ? :%v\n", i, isPalindrome(i)) +} + +func isPalindrome(i int) bool { + return i == func(x int) int { + var ret int + for x/10 != 0 { + ret = ret*10 + x%10 + x = (x - x%10) / 10 + } + ret = ret*10 + x%10 + return ret + }(i) +} diff --git a/010_regular_expression_matching/readme.md b/010_regular_expression_matching/readme.md new file mode 100644 index 0000000..2af343d --- /dev/null +++ b/010_regular_expression_matching/readme.md @@ -0,0 +1,21 @@ + +Implement regular expression matching with support for '.' and '*'. + +``` +'.' Matches any single character. +'*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") → false +isMatch("aa","aa") → true +isMatch("aaa","aa") → false +isMatch("aa", "a*") → true +isMatch("aa", ".*") → true +isMatch("ab", ".*") → true +isMatch("aab", "c*a*b") → true +``` \ No newline at end of file