-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (102 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Example 3:
Input: arr[] = {3, 1, 4, 4, 5, 2, 6, 1}, k = 2
Output: [4,1]
*/
package main
import (
"fmt"
"sort"
"strings"
// "reflect"
"unicode"
)
func decryptMsg(str string) {
// Remove all numbers 0 to 9 from the string and return the reversed message.
var builder strings.Builder
for _, r := range strings.ReplaceAll(str, " ", "") {
if !unicode.IsDigit(r) {
builder.WriteRune(r)
}
}
decrypted := builder.String()
var reversed string
for i := len(decrypted) - 1; i >= 0; i-- {
reversed += string(decrypted[i])
}
fmt.Println(reversed)
}
func topKFrequent(nums []int, k int) []int {
var want map[int]int
want = make(map[int]int)
for i := 0; i < len(nums); i++ {
want[nums[i]] = want[nums[i]] + 1
}
keys := make([]int, len(want))
i := 0
for k := range want {
keys[i] = k
i++
}
sort.SliceStable(keys, func(i, j int) bool {
return want[keys[i]] > want[keys[j]]
})
fmt.Println(keys)
result := make([]int, k)
x := 0
for y := range keys {
result[x] = keys[y]
x++
if x == k {
break
}
}
fmt.Println(result)
return result
}
func main() {
topKFrequent([]int{3, 1, 4, 4, 5, 2, 6, 1}, 2)
// decryptMsg(" 036o l leH1297 ")
// type test struct {
// nums []int
// k int
// want []int
// }
// tests := []test{
// {
// []int{1, 1, 1, 2, 2, 3},
// 2,
// []int{1, 2},
// },
// {
// []int{1, 1, 1, 2, 2, 3, 3, 3, 3},
// 2,
// []int{1, 3},
// },
// {
// []int{1},
// 1,
// []int{1},
// },
// {
// []int{3, 1, 4, 4, 5, 2, 6, 1},
// 2,
// []int{4, 1},
// },
// }
// for i, tt := range tests {
// got := topKFrequent(tt.nums, tt.k)
// res := "PASSED"
// if !reflect.DeepEqual(got, tt.want) {
// res = "FAILED"
// }
// fmt.Printf("\n%s case=%d got=%v want=%v", res, i, got, tt.want)
// }
}