-
Notifications
You must be signed in to change notification settings - Fork 149
103-第二周作业 #325
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
base: master
Are you sure you want to change the base?
103-第二周作业 #325
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package HashTable | ||
|
|
||
| //已通过 | ||
| func IsAnagram(s string, t string) bool { | ||
| if len(s) != len(t){ | ||
| return false | ||
| } | ||
| sb :=[]byte(s) | ||
| collectionMap := map[byte]int{} | ||
| for i := 0; i< len(sb);i++ { | ||
| value,ok := collectionMap[sb[i]] | ||
| if !ok { | ||
| collectionMap[sb[i]] = 1 | ||
| }else{ | ||
| collectionMap[sb[i]] = value + 1 | ||
| } | ||
| } | ||
|
|
||
| st := []byte(t) | ||
| for i := 0; i< len(st);i++ { | ||
| value,ok := collectionMap[st[i]] | ||
| if !ok { | ||
| return false | ||
| }else{ | ||
| if value == 0{ | ||
| return false | ||
| } | ||
| collectionMap[st[i]] = value - 1 | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func IsAnagram2(s string, t string) bool { | ||
| tmpA,tmpB :=make([]int,26),make([]int,26) | ||
| for i:=0;i<len(s);i++ { | ||
| tmpA[s[i]-'a']++ | ||
| } | ||
| for i:=0;i<len(t);i++ { | ||
| tmpB[t[i]-'a']++ | ||
| } | ||
| for i:=0;i<26;i++ { | ||
| if tmpA[i] != tmpB[i] { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package HashTable | ||
|
|
||
| import "math" | ||
| //Finished | ||
| var res = -1 | ||
| func findSecondMinimumValue(root *TreeNode) int { | ||
| min := minValue(root) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 没注释思路比较费解,不过 go 语言的缩进比较奇特😆 |
||
| res = math.MaxInt64 | ||
| helpFind(root,min) | ||
| if res == math.MaxInt64{ | ||
| return -1 | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func helpFind(root *TreeNode, min int) { | ||
| if root != nil{ | ||
| if root.Val > min && root.Val < res{ | ||
| res = root.Val | ||
| } | ||
| helpFind(root.Left,min) | ||
| helpFind(root.Right,min) | ||
| } | ||
| } | ||
|
|
||
| func minValue(root *TreeNode) int { | ||
| if root.Left == nil && root.Right == nil{ | ||
| return root.Val | ||
| } | ||
| if root.Left == nil{ | ||
| return minValue(root.Right) | ||
| } | ||
| if root.Right == nil{ | ||
| return minValue(root.Left) | ||
| } | ||
| if root.Left.Val < root.Right.Val{ | ||
| return minValue(root.Left) | ||
| }else { | ||
| return minValue(root.Right) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package HashTable | ||
|
|
||
| import ( | ||
| "sort" | ||
| ) | ||
| //测试没过,因为顺序问题 | ||
| func TopKFrequent(words []string, k int) []string { | ||
| collectionMap := map[string]int{} | ||
|
|
||
| for _, v := range words { | ||
| if intV,ok:=collectionMap[v];ok{ | ||
| collectionMap[v] = intV +1 | ||
| }else{ | ||
| collectionMap[v] = 1 | ||
| } | ||
| } | ||
| res := []string{} | ||
| counts := make([]int,0) | ||
| for _, v := range collectionMap { | ||
| counts = append(counts, v) | ||
| } | ||
| sort.Ints(counts) | ||
| searchResult := counts[len(counts)-k:] | ||
|
|
||
| for k,v := range collectionMap { | ||
| if help(searchResult,v){ | ||
| res = append(res, k) | ||
| } | ||
| } | ||
| return res | ||
| } | ||
|
|
||
| func help (arr []int,compare int) bool{ | ||
| for e := range arr { | ||
| if compare == arr[e]{ | ||
| return true | ||
| } | ||
| } | ||
| return false; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package HashTable | ||
|
|
||
| type TreeNode struct { | ||
| Val int | ||
| Left *TreeNode | ||
| Right *TreeNode | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else前后空一个格看起来会好点。