Skip to content

Commit edf1339

Browse files
authored
Merge pull request algorithm007-class02#330 from safeie/master
0170_Week02(Go)
2 parents f3a2dbe + 4e9899e commit edf1339

4 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func isAnagram(s string, t string) bool {
2+
if len(s) != len(t) {
3+
return false
4+
}
5+
6+
var count [26]int
7+
for _,c := range s {
8+
count[c - 'a']++
9+
}
10+
for _,c := range t {
11+
count[c - 'a']--
12+
if count[c - 'a'] < 0 {
13+
return false
14+
}
15+
}
16+
return true
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func trap(height []int) int {
2+
cap := 0
3+
leftMax := 0
4+
rightMax := 0
5+
left, right := 0, len(height) - 1
6+
for left < right {
7+
if height[left] < height[right] {
8+
if height[left] >= leftMax {
9+
leftMax = height[left]
10+
} else {
11+
cap += leftMax - height[left]
12+
}
13+
left++
14+
} else {
15+
if height[right] > rightMax {
16+
rightMax = height[right]
17+
} else {
18+
cap += rightMax - height[right]
19+
}
20+
right--
21+
}
22+
}
23+
24+
return cap
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func groupAnagrams(strs []string) [][]string {
2+
store := make(map[string][]string, len(strs))
3+
var key string
4+
for _, str := range strs {
5+
ss := []byte(str)
6+
sort.Slice(ss, func(i, j int) bool { return ss[i] < ss[j] })
7+
key = string(ss)
8+
if _, ok := store[key]; ok {
9+
store[key] = append(store[key], str)
10+
} else {
11+
store[key] = []string{str}
12+
}
13+
}
14+
15+
arrs := make([][]string, 0, len(store))
16+
for i := range store {
17+
arrs = append(arrs, store[i])
18+
}
19+
20+
return arrs
21+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
type MyCircularDeque struct {
2+
data []int
3+
cap int
4+
count int
5+
head int
6+
tail int
7+
}
8+
9+
10+
/** Initialize your data structure here. Set the size of the deque to be k. */
11+
func Constructor(k int) MyCircularDeque {
12+
t := MyCircularDeque{}
13+
t.data = make([]int, k)
14+
t.cap = k
15+
t.count = 0
16+
t.head = -1
17+
t.tail = -1
18+
return t
19+
}
20+
21+
22+
/** Adds an item at the front of Deque. Return true if the operation is successful. */
23+
func (this *MyCircularDeque) InsertFront(value int) bool {
24+
if this.count == this.cap {
25+
return false
26+
}
27+
this.head = (this.head - 1 + this.cap) % this.cap
28+
this.data[this.head] = value
29+
if this.tail == - 1 {
30+
this.tail = this.head
31+
}
32+
this.count++
33+
return true
34+
}
35+
36+
37+
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
38+
func (this *MyCircularDeque) InsertLast(value int) bool {
39+
if this.count == this.cap {
40+
return false
41+
}
42+
this.tail = (this.tail + 1 + this.cap) % this.cap
43+
this.data[this.tail] = value
44+
if this.head == - 1 {
45+
this.head = this.tail
46+
}
47+
this.count++
48+
return true
49+
}
50+
51+
52+
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
53+
func (this *MyCircularDeque) DeleteFront() bool {
54+
if this.count == 0 {
55+
return false
56+
}
57+
this.data[this.head] = 0
58+
this.head = (this.head + 1 + this.cap) % this.cap
59+
this.count--
60+
return true
61+
}
62+
63+
64+
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
65+
func (this *MyCircularDeque) DeleteLast() bool {
66+
if this.count == 0 {
67+
return false
68+
}
69+
this.data[this.tail] = 0
70+
this.tail = (this.tail - 1 + this.cap) % this.cap
71+
this.count--
72+
return true
73+
}
74+
75+
76+
/** Get the front item from the deque. */
77+
func (this *MyCircularDeque) GetFront() int {
78+
if this.count == 0 {
79+
return -1
80+
}
81+
return this.data[this.head]
82+
}
83+
84+
85+
/** Get the last item from the deque. */
86+
func (this *MyCircularDeque) GetRear() int {
87+
if this.count == 0 {
88+
return -1
89+
}
90+
return this.data[this.tail]
91+
}
92+
93+
94+
/** Checks whether the circular deque is empty or not. */
95+
func (this *MyCircularDeque) IsEmpty() bool {
96+
return this.count == 0
97+
}
98+
99+
100+
/** Checks whether the circular deque is full or not. */
101+
func (this *MyCircularDeque) IsFull() bool {
102+
return this.count == this.cap
103+
}
104+
105+
106+
/**
107+
* Your MyCircularDeque object will be instantiated and called as such:
108+
* obj := Constructor(k);
109+
* param_1 := obj.InsertFront(value);
110+
* param_2 := obj.InsertLast(value);
111+
* param_3 := obj.DeleteFront();
112+
* param_4 := obj.DeleteLast();
113+
* param_5 := obj.GetFront();
114+
* param_6 := obj.GetRear();
115+
* param_7 := obj.IsEmpty();
116+
* param_8 := obj.IsFull();
117+
*/
118+

0 commit comments

Comments
 (0)