Skip to content

Commit 4c1ca8d

Browse files
committed
3 problems
1 parent ef62be0 commit 4c1ca8d

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
162162
#### [201. Bitwise AND of Numbers Range](https://github.com/hitzzc/go-leetcode/tree/master/bitwise_AND_of_numbers_range)
163163
#### [202. Happy Number](https://github.com/hitzzc/go-leetcode/tree/master/happy_number)
164164
#### [203. Remove Linked List Elements](https://github.com/hitzzc/go-leetcode/tree/master/remove_linked_list_elements)
165+
#### [204. Count Primes](https://github.com/hitzzc/go-leetcode/tree/master/count_primes)
166+
#### [205. Isomorphic Strings](https://github.com/hitzzc/go-leetcode/tree/master/isomorphic_strings)
167+
#### [206. Reverse Linked List](https://github.com/hitzzc/go-leetcode/tree/master/reverse_linked_list)
165168

166169

167170

count_primes/count_primes.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package count_primes
2+
3+
func countPrimes(n int) int {
4+
check := make([]bool, n)
5+
for i := 2; i*i < n; i++ {
6+
if check[i] {
7+
continue
8+
}
9+
for j := i; i*j < n; j++ {
10+
if !check[i*j] {
11+
check[i*j] = true
12+
}
13+
}
14+
}
15+
cnt := 0
16+
for i := 2; i < n; i++ {
17+
if !check[i] {
18+
cnt++
19+
}
20+
}
21+
return cnt
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package isomorphic_strings
2+
3+
func isIsomorphic(s string, t string) bool {
4+
if len(s) != len(t) {
5+
return false
6+
}
7+
m := map[byte]byte{}
8+
n := map[byte]byte{}
9+
for i := range s {
10+
if _, ok := m[t[i]]; !ok {
11+
m[t[i]] = s[i]
12+
}
13+
if _, ok := n[s[i]]; !ok {
14+
n[s[i]] = t[i]
15+
}
16+
if s[i] != m[t[i]] {
17+
return false
18+
}
19+
if t[i] != n[s[i]] {
20+
return false
21+
}
22+
}
23+
return true
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package reverse_linked_list
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func reverseList(head *ListNode) *ListNode {
9+
var tail, next *ListNode
10+
for head != nil && head.Next != nil {
11+
next = head.Next
12+
head.Next = tail
13+
tail = head
14+
head = next
15+
}
16+
if head != nil {
17+
head.Next = tail
18+
}
19+
return head
20+
}

0 commit comments

Comments
 (0)