Skip to content

Commit 81063f6

Browse files
committed
update
1 parent e629758 commit 81063f6

7 files changed

+600
-0
lines changed

1323.6-和-9-组成的最大数字.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* @lc app=leetcode.cn id=1323 lang=golang
3+
*
4+
* [1323] 6 和 9 组成的最大数字
5+
*
6+
* https://leetcode-cn.com/problems/maximum-69-number/description/
7+
*
8+
* algorithms
9+
* Easy (75.25%)
10+
* Likes: 59
11+
* Dislikes: 0
12+
* Total Accepted: 28.4K
13+
* Total Submissions: 37.7K
14+
* Testcase Example: '9669'
15+
*
16+
* 给你一个仅由数字 6 和 9 组成的正整数 num。
17+
*
18+
* 你最多只能翻转一位数字,将 6 变成 9,或者把 9 变成 6 。
19+
*
20+
* 请返回你可以得到的最大数字。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
* 输入:num = 9669
27+
* 输出:9969
28+
* 解释:
29+
* 改变第一位数字可以得到 6669 。
30+
* 改变第二位数字可以得到 9969 。
31+
* 改变第三位数字可以得到 9699 。
32+
* 改变第四位数字可以得到 9666 。
33+
* 其中最大的数字是 9969 。
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
* 输入:num = 9996
39+
* 输出:9999
40+
* 解释:将最后一位从 6 变到 9,其结果 9999 是最大的数。
41+
*
42+
* 示例 3:
43+
*
44+
* 输入:num = 9999
45+
* 输出:9999
46+
* 解释:无需改变就已经是最大的数字了。
47+
*
48+
*
49+
*
50+
* 提示:
51+
*
52+
*
53+
* 1 <= num <= 10^4
54+
* num 每一位上的数字都是 6 或者 9 。
55+
*
56+
*
57+
*/
58+
59+
// 使用字符串
60+
func bak(num int) int {
61+
s := strconv.Itoa(num)
62+
b := []byte(s)
63+
for i := range b {
64+
if b[i] == '6' {
65+
b[i] = '9'
66+
break
67+
}
68+
}
69+
70+
ans, _ := strconv.Atoi(string(b))
71+
return ans
72+
}
73+
74+
// @lc code=start
75+
func maximum69Number(num int) int {
76+
i := 1
77+
match := 0
78+
79+
last := 0
80+
81+
for s := num; s != 0; {
82+
last = s % 10
83+
if last == 6 {
84+
match = 3 * i
85+
}
86+
s /= 10
87+
i *= 10
88+
}
89+
return match + num
90+
}
91+
92+
// @lc code=end
93+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @lc app=leetcode.cn id=1342 lang=golang
3+
*
4+
* [1342] 将数字变成 0 的操作次数
5+
*
6+
* https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/description/
7+
*
8+
* algorithms
9+
* Easy (82.67%)
10+
* Likes: 110
11+
* Dislikes: 0
12+
* Total Accepted: 66.2K
13+
* Total Submissions: 80.1K
14+
* Testcase Example: '14'
15+
*
16+
* 给你一个非负整数 num ,请你返回将它变成 0 所需要的步数。 如果当前数字是偶数,你需要把它除以 2 ;否则,减去 1 。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入:num = 14
23+
* 输出:6
24+
* 解释:
25+
* 步骤 1) 14 是偶数,除以 2 得到 7 。
26+
* 步骤 2) 7 是奇数,减 1 得到 6 。
27+
* 步骤 3) 6 是偶数,除以 2 得到 3 。
28+
* 步骤 4) 3 是奇数,减 1 得到 2 。
29+
* 步骤 5) 2 是偶数,除以 2 得到 1 。
30+
* 步骤 6) 1 是奇数,减 1 得到 0 。
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
* 输入:num = 8
36+
* 输出:4
37+
* 解释:
38+
* 步骤 1) 8 是偶数,除以 2 得到 4 。
39+
* 步骤 2) 4 是偶数,除以 2 得到 2 。
40+
* 步骤 3) 2 是偶数,除以 2 得到 1 。
41+
* 步骤 4) 1 是奇数,减 1 得到 0 。
42+
*
43+
*
44+
* 示例 3:
45+
*
46+
* 输入:num = 123
47+
* 输出:12
48+
*
49+
*
50+
*
51+
*
52+
* 提示:
53+
*
54+
*
55+
* 0 <= num <= 10^6
56+
*
57+
*
58+
*/
59+
60+
// @lc code=start
61+
func numberOfSteps(num int) int {
62+
63+
i := 0
64+
for num != 0 {
65+
if num%2 == 0 {
66+
num /= 2
67+
} else {
68+
num--
69+
}
70+
i++
71+
}
72+
return i
73+
}
74+
75+
// @lc code=end
76+

1394.找出数组中的幸运数.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @lc app=leetcode.cn id=1394 lang=golang
3+
*
4+
* [1394] 找出数组中的幸运数
5+
*
6+
* https://leetcode-cn.com/problems/find-lucky-integer-in-an-array/description/
7+
*
8+
* algorithms
9+
* Easy (66.29%)
10+
* Likes: 32
11+
* Dislikes: 0
12+
* Total Accepted: 19.9K
13+
* Total Submissions: 30.1K
14+
* Testcase Example: '[2,2,3,4]'
15+
*
16+
* 在整数数组中,如果一个整数的出现频次和它的数值大小相等,我们就称这个整数为「幸运数」。
17+
*
18+
* 给你一个整数数组 arr,请你从中找出并返回一个幸运数。
19+
*
20+
*
21+
* 如果数组中存在多个幸运数,只需返回 最大 的那个。
22+
* 如果数组中不含幸运数,则返回 -1 。
23+
*
24+
*
25+
*
26+
*
27+
* 示例 1:
28+
*
29+
* 输入:arr = [2,2,3,4]
30+
* 输出:2
31+
* 解释:数组中唯一的幸运数是 2 ,因为数值 2 的出现频次也是 2 。
32+
*
33+
*
34+
* 示例 2:
35+
*
36+
* 输入:arr = [1,2,2,3,3,3]
37+
* 输出:3
38+
* 解释:1、2 以及 3 都是幸运数,只需要返回其中最大的 3 。
39+
*
40+
*
41+
* 示例 3:
42+
*
43+
* 输入:arr = [2,2,2,3,3]
44+
* 输出:-1
45+
* 解释:数组中不存在幸运数。
46+
*
47+
*
48+
* 示例 4:
49+
*
50+
* 输入:arr = [5]
51+
* 输出:-1
52+
*
53+
*
54+
* 示例 5:
55+
*
56+
* 输入:arr = [7,7,7,7,7,7,7]
57+
* 输出:7
58+
*
59+
*
60+
*
61+
*
62+
* 提示:
63+
*
64+
*
65+
* 1 <= arr.length <= 500
66+
* 1 <= arr[i] <= 500
67+
*
68+
*
69+
*/
70+
71+
// @lc code=start
72+
func findLucky(arr []int) int {
73+
cache := make(map[int]int)
74+
75+
for i := range arr {
76+
cache[arr[i]]++
77+
}
78+
79+
ans := -1
80+
for k, v := range cache {
81+
if k == v {
82+
if k > ans {
83+
ans = k
84+
}
85+
}
86+
}
87+
return ans
88+
}
89+
90+
// @lc code=end
91+

1441.用栈操作构建数组.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* @lc app=leetcode.cn id=1441 lang=golang
3+
*
4+
* [1441] 用栈操作构建数组
5+
*
6+
* https://leetcode-cn.com/problems/build-an-array-with-stack-operations/description/
7+
*
8+
* algorithms
9+
* Easy (65.01%)
10+
* Likes: 40
11+
* Dislikes: 0
12+
* Total Accepted: 20.6K
13+
* Total Submissions: 31.7K
14+
* Testcase Example: '[1,3]\n3'
15+
*
16+
* 给你一个目标数组 target 和一个整数 n。每次迭代,需要从  list = {1,2,3..., n} 中依序读取一个数字。
17+
*
18+
* 请使用下述操作来构建目标数组 target :
19+
*
20+
*
21+
* Push:从 list 中读取一个新元素, 并将其推入数组中。
22+
* Pop:删除数组中的最后一个元素。
23+
* 如果目标数组构建完成,就停止读取更多元素。
24+
*
25+
*
26+
* 题目数据保证目标数组严格递增,并且只包含 1 到 n 之间的数字。
27+
*
28+
* 请返回构建目标数组所用的操作序列。
29+
*
30+
* 题目数据保证答案是唯一的。
31+
*
32+
*
33+
*
34+
* 示例 1:
35+
*
36+
*
37+
* 输入:target = [1,3], n = 3
38+
* 输出:["Push","Push","Pop","Push"]
39+
* 解释:
40+
* 读取 1 并自动推入数组 -> [1]
41+
* 读取 2 并自动推入数组,然后删除它 -> [1]
42+
* 读取 3 并自动推入数组 -> [1,3]
43+
*
44+
*
45+
* 示例 2:
46+
*
47+
*
48+
* 输入:target = [1,2,3], n = 3
49+
* 输出:["Push","Push","Push"]
50+
*
51+
*
52+
* 示例 3:
53+
*
54+
*
55+
* 输入:target = [1,2], n = 4
56+
* 输出:["Push","Push"]
57+
* 解释:只需要读取前 2 个数字就可以停止。
58+
*
59+
*
60+
*
61+
*
62+
* 提示:
63+
*
64+
*
65+
* 1 <= target.length <= 100
66+
* 1 <= target[i] <= 100
67+
* 1 <= n <= 100
68+
* target 是严格递增的
69+
*
70+
*
71+
*/
72+
// @lc code=start
73+
func buildArray(target []int, n int) []string {
74+
ans := make([]string, 0)
75+
76+
s := 1
77+
78+
for i := 0; i < len(target); i++ {
79+
for target[i] != s {
80+
s++
81+
ans = append(ans, "Push", "Pop")
82+
}
83+
ans = append(ans, "Push")
84+
s++
85+
}
86+
87+
return ans
88+
}
89+
90+
// @lc code=end

0 commit comments

Comments
 (0)