-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xieliwei
committed
Oct 22, 2019
1 parent
d7a6e95
commit 03d4984
Showing
2 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package d20 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
/* | ||
* @lc app=leetcode.cn id=20 lang=golang | ||
* | ||
* [20] 有效的括号 | ||
* | ||
* https://leetcode-cn.com/problems/valid-parentheses/description/ | ||
* | ||
* algorithms | ||
* Easy (39.80%) | ||
* Likes: 1132 | ||
* Dislikes: 0 | ||
* Total Accepted: 141.5K | ||
* Total Submissions: 354.9K | ||
* Testcase Example: '"()"' | ||
* | ||
* 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 | ||
* | ||
* 有效字符串需满足: | ||
* | ||
* | ||
* 左括号必须用相同类型的右括号闭合。 | ||
* 左括号必须以正确的顺序闭合。 | ||
* | ||
* | ||
* 注意空字符串可被认为是有效字符串。 | ||
* | ||
* 示例 1: | ||
* | ||
* 输入: "()" | ||
* 输出: true | ||
* | ||
* | ||
* 示例 2: | ||
* | ||
* 输入: "()[]{}" | ||
* 输出: true | ||
* | ||
* | ||
* 示例 3: | ||
* | ||
* 输入: "(]" | ||
* 输出: false | ||
* | ||
* | ||
* 示例 4: | ||
* | ||
* 输入: "([)]" | ||
* 输出: false | ||
* | ||
* | ||
* 示例 5: | ||
* | ||
* 输入: "{[]}" | ||
* 输出: true | ||
* | ||
*/ | ||
func isValid(s string) bool { | ||
md := map[byte]byte{ | ||
'{': '}', | ||
'[': ']', | ||
'(': ')', | ||
} | ||
l := len(s) | ||
if l % 2 == 1 { | ||
return false | ||
} | ||
if l == 0 { | ||
return true | ||
} | ||
stack := make([]byte, 0, l) | ||
for i:=0; i<l; i++ { | ||
ls := len(stack) | ||
if ls == 0 || md[stack[ls-1]] != s[i] { | ||
stack = append(stack, s[i]) | ||
} else { | ||
stack = stack[:ls-1] | ||
} | ||
} | ||
return len(stack) == 0 | ||
} | ||
|
||
|
||
func Test_a(t *testing.T) { | ||
str := "[{}]" | ||
fmt.Println(isValid(str)) | ||
str = "()[]{}" | ||
fmt.Println(isValid(str)) | ||
str = "(([]){})" | ||
fmt.Println(isValid(str)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package d4 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
/* | ||
* @lc app=leetcode.cn id=4 lang=golang | ||
* | ||
* [4] 寻找两个有序数组的中位数 | ||
* | ||
* https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ | ||
* | ||
* algorithms | ||
* Hard (36.11%) | ||
* Likes: 1628 | ||
* Dislikes: 0 | ||
* Total Accepted: 101.5K | ||
* Total Submissions: 281K | ||
* Testcase Example: '[1,3]\n[2]' | ||
* | ||
* 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 | ||
* | ||
* 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 | ||
* | ||
* 你可以假设 nums1 和 nums2 不会同时为空。 | ||
* | ||
* 示例 1: | ||
* | ||
* nums1 = [1, 3] | ||
* nums2 = [2] | ||
* | ||
* 则中位数是 2.0 | ||
* | ||
* | ||
* 示例 2: | ||
* | ||
* nums1 = [1, 2] | ||
* nums2 = [3, 4] | ||
* | ||
* 则中位数是 (2 + 3)/2 = 2.5 | ||
* | ||
* | ||
*/ | ||
|
||
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { | ||
c1 := len(nums1) | ||
c2 := len(nums2) | ||
mid := (c1+c2) / 2 + 1 | ||
midVals := [2]int{} | ||
var idx0, idx1, idx2 int | ||
var val int | ||
for ;idx0 < mid; { | ||
if idx1 < c1 && (idx2 >= c2 || nums1[idx1] < nums2[idx2]) { | ||
val = nums1[idx1] | ||
idx1++ | ||
} else { | ||
val = nums2[idx2] | ||
idx2++ | ||
} | ||
midVals[idx0%2] = val | ||
idx0++ | ||
} | ||
if (c1 + c2) % 2 == 0 { | ||
return (float64(midVals[0]) + float64(midVals[1])) / 2 | ||
} else { | ||
if mid % 2 == 0 { | ||
return float64(midVals[1]) | ||
} else { | ||
return float64(midVals[0]) | ||
} | ||
} | ||
} | ||
|
||
func Test_01(t *testing.T) { | ||
nums1 := []int{1,3} | ||
nums2 := []int{2} | ||
fmt.Println(findMedianSortedArrays(nums1, nums2)) | ||
} |