File tree 6 files changed +133
-0
lines changed
6 files changed +133
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=204 lang=golang
3
+ *
4
+ * [204] 计数质数
5
+ */
6
+
7
+ // @lc code=start
8
+ func countPrimes (n int ) int {
9
+ if n < 3 {
10
+ return 0
11
+ }
12
+ res := 1
13
+ for i := 3 ; i < n ; i ++ {
14
+ if i & 1 == 0 {
15
+ continue
16
+ }
17
+ sign := true
18
+ for j := 3 ; j * j <= i ; j += 2 {
19
+ if i % j == 0 {
20
+ sign = false
21
+ break
22
+ }
23
+ }
24
+ if sign {
25
+ res ++
26
+ }
27
+ }
28
+ return res
29
+ }
30
+ // @lc code=end
31
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=205 lang=golang
3
+ *
4
+ * [205] 同构字符串
5
+ */
6
+
7
+ // @lc code=start
8
+ func isIsomorphic (s string , t string ) bool {
9
+ s2t ,t2s := map [byte ]byte {}, map [byte ]byte {}
10
+ for i := range s {
11
+ x , y := s [i ], t [i ]
12
+ if s2t [x ] > 0 && s2t [x ] != y || t2s [y ] > 0 && t2s [y ] != x {
13
+ return false
14
+ }
15
+ s2t [x ] = y
16
+ t2s [y ] = x
17
+ }
18
+ return true
19
+ }
20
+ // @lc code=end
21
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=217 lang=golang
3
+ *
4
+ * [217] 存在重复元素
5
+ */
6
+
7
+ // @lc code=start
8
+ func containsDuplicate (nums []int ) bool {
9
+ numsM := map [int ]bool {}
10
+ for _ , i := range nums {
11
+ if numsM [i ] == true {
12
+ return true
13
+ }
14
+ numsM [i ] = true
15
+ }
16
+ return false
17
+ }
18
+ // @lc code=end
19
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=219 lang=golang
3
+ *
4
+ * [219] 存在重复元素 II
5
+ */
6
+
7
+ // @lc code=start
8
+ func containsNearbyDuplicate (nums []int , k int ) bool {
9
+ l := len (nums )
10
+ numsM := make (map [int ]int , l )
11
+ for i := 0 ; i < l ; i ++ {
12
+ n := nums [i ]
13
+ if numsM [n ] > 0 {
14
+ if j := i - numsM [n ] + 1 ; j <= k {
15
+ return true
16
+ }
17
+ }
18
+ numsM [n ] = i + 1
19
+ }
20
+ return false
21
+ }
22
+
23
+ // @lc code=end
24
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=41 lang=golang
3
+ *
4
+ * [41] 缺失的第一个正数
5
+ */
6
+
7
+ // @lc code=start
8
+ func firstMissingPositive (nums []int ) int {
9
+ l := len (nums )
10
+ if l == 0 {
11
+ return 1
12
+ }
13
+ for i := 0 ; i < l ; i ++ {
14
+ if nums [i ] <= 0 {
15
+ nums [i ] = l + 1
16
+ }
17
+ }
18
+ for i := 0 ; i < l ; i ++ {
19
+ j := nums [i ]
20
+ if j < 0 {
21
+ j = j * - 1
22
+ }
23
+ if j > 0 && j <= l {
24
+ if nums [j - 1 ] > 0 {
25
+ nums [j - 1 ] = - nums [j - 1 ]
26
+ }
27
+ }
28
+ }
29
+ for i , n := range nums {
30
+ if n > 0 {
31
+ return i + 1
32
+ }
33
+ }
34
+ return l + 1
35
+ }
36
+
37
+ // @lc code=end
38
+
You can’t perform that action at this time.
0 commit comments