Skip to content

Commit 85a8cf4

Browse files
committed
add 9 solutions
106 169 233 297 504 507 599 798 946
1 parent 35f8420 commit 85a8cf4

19 files changed

+249
-53
lines changed

src/unresolved/106.construct-binary-tree-from-inorder-and-postorder-traversal.jl renamed to src/problems/106.construct-binary-tree-from-inorder-and-postorder-traversal.jl

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 106. Construct Binary Tree from Inorder and Postorder Traversal
33
# id: problem106
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-02
66
# difficulty: Medium
77
# categories: Array, Tree, Depth-first Search
88
# link: <https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/>
@@ -36,5 +36,19 @@
3636
## @lc code=start
3737
using LeetCode
3838

39-
## add your code here:
39+
function build_tree_inpost(inorder::AbstractArray, postorder::AbstractArray)::TreeNode
40+
root = TreeNode(last(postorder))
41+
pos = findfirst(==(root.val), inorder)
42+
pos != 1 && (
43+
root.left = build_tree_inpost(
44+
@view(inorder[1:(pos - 1)]), @view(postorder[1:(pos - 1)])
45+
)
46+
)
47+
pos != length(postorder) && (
48+
root.right = build_tree_inpost(
49+
@view(inorder[(pos + 1):end]), @view(postorder[pos:(end - 1)])
50+
)
51+
)
52+
return root
53+
end
4054
## @lc code=end

src/unresolved/169.majority-element.jl renamed to src/problems/169.majority-element.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 169. Majority Element
33
# id: problem169
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-10
66
# difficulty: Easy
77
# categories: Array, Divide and Conquer, Bit Manipulation
88
# link: <https://leetcode.com/problems/majority-element/description/>
@@ -34,5 +34,16 @@
3434
## @lc code=start
3535
using LeetCode
3636

37-
## add your code here:
37+
function majority_element(nums::Vector{Int})::Int
38+
res = vote = 0
39+
for num in nums
40+
if vote == 0
41+
vote += 1
42+
res = num
43+
else
44+
vote += (res == num) ? 1 : -1
45+
end
46+
end
47+
return res
48+
end
3849
## @lc code=end
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# ---
2+
# title: 233. Number of Digit One
3+
# id: problem233
4+
# author: zhwang
5+
# date: 2022-03-12
6+
# difficulty: Hard
7+
# categories: Math
8+
# link: <https://leetcode.com/problems/number-of-digit-one/description/>
9+
# hidden: true
10+
# ---
11+
#
12+
# Given an integer n, count the total number of digit 1 appearing in all non-
13+
# negative integers less than or equal to n.
14+
#
15+
# **Example:**
16+
#
17+
#
18+
#
19+
# Input: 13
20+
# Output: 6
21+
# Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
22+
#
23+
#
24+
#
25+
## @lc code=start
26+
using LeetCode
27+
28+
function count_digit_one(n::Int)::Int
29+
nums = reverse!([parse(Int, i) for i in string(n + 1)])
30+
res, ones = 0, count(==(1), nums)
31+
for (i, num) in enumerate(nums)
32+
num == 1 && (ones -= 1)
33+
res += 10^(i - 1) * (num * ones + (num > 1))
34+
res += i > 1 && (10^(i - 2) * (i - 1) * num)
35+
end
36+
return res
37+
end
38+
39+
## example: reduce 21413 to 0
40+
## num: step => reduced num
41+
## 2141|4|-1: 4*2*1 + 4*0 + (4>1)*10 => 21410-1
42+
## 214|1|0-1: 1*1*10 + 1*1*1 + (1>1)*10 => 21400-1
43+
## 21|4|00-1: 4*1*100 + 4*2*10 + (4>1)*100 => 21000-1
44+
## 2|1|000-1: 1*0*1000 + 1*3*100 + (1>1)*1000 => 20000-1
45+
## |2|0000-1: 2*0*10000 + 2*4*10000 + (2>1)*10000 => 0
46+
47+
## @lc code=end

src/unresolved/297.serialize-and-deserialize-binary-tree.jl renamed to src/problems/297.serialize-and-deserialize-binary-tree.jl

+44-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 297. Serialize and Deserialize Binary Tree
33
# id: problem297
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-11
66
# difficulty: Hard
77
# categories: Tree, Design
88
# link: <https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/>
@@ -71,5 +71,46 @@
7171
## @lc code=start
7272
using LeetCode
7373

74-
## add your code here:
74+
serialize(::Nothing) = "[]"
75+
function serialize(root::TreeNode{Int})::String
76+
res, queue, hasnew = String[], Union{TreeNode,Nothing}[root], true
77+
while hasnew
78+
hasnew = false
79+
for _ in 1:length(queue)
80+
node = popfirst!(queue)
81+
if !isnothing(node)
82+
push!(res, string(node.val))
83+
push!(queue, node.left)
84+
push!(queue, node.right)
85+
all(isnothing.([node.left, node.right])) || (hasnew = true)
86+
else
87+
push!(res, "null")
88+
end
89+
end
90+
end
91+
return "[" * join(res, ',') * "]"
92+
end
93+
94+
function deserialize(data::String)::Union{TreeNode,Nothing}
95+
data == "[]" && return nothing
96+
vals = split(data[2:(end - 1)], ',')
97+
root, n = TreeNode(parse(Int, vals[1])), length(vals)
98+
queue, m = [root], 2
99+
while m <= n
100+
for _ in 1:length(queue) # new nodes
101+
node = popfirst!(queue)
102+
if vals[m] != "null"
103+
node.left = TreeNode(parse(Int, vals[m]))
104+
push!(queue, node.left)
105+
end
106+
if vals[m + 1] != "null"
107+
node.right = TreeNode(parse(Int, vals[m + 1]))
108+
push!(queue, node.right)
109+
end
110+
m += 2
111+
end
112+
end
113+
return root
114+
end
115+
75116
## @lc code=end

src/unresolved/504.base-7.jl renamed to src/problems/504.base-7.jl

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 504. Base 7
33
# id: problem504
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-10
66
# difficulty: Easy
77
# categories:
88
# link: <https://leetcode.com/problems/base-7/description/>
@@ -33,5 +33,17 @@
3333
## @lc code=start
3434
using LeetCode
3535

36-
## add your code here:
36+
function convert_to_base7(num::Int)::String
37+
num == 0 && return "0"
38+
sign = num < 0 ? -1 : 1
39+
num *= sign
40+
res = Int[]
41+
while num > 0
42+
push!(res, num % 7)
43+
num ÷= 7
44+
end
45+
reverse!(res)
46+
return sign != -1 ? join(res) : "-" * join(res)
47+
end
48+
3749
## @lc code=end

src/unresolved/507.perfect-number.jl renamed to src/problems/507.perfect-number.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 507. Perfect Number
33
# id: problem507
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-10
66
# difficulty: Easy
77
# categories: Math
88
# link: <https://leetcode.com/problems/perfect-number/description/>
@@ -71,5 +71,6 @@
7171
## @lc code=start
7272
using LeetCode
7373

74-
## add your code here:
74+
perfect_number(num::Int) = num in [6, 28, 496, 8128, 33550336]
75+
7576
## @lc code=end

src/unresolved/599.minimum-index-sum-of-two-lists.jl renamed to src/problems/599.minimum-index-sum-of-two-lists.jl

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 599. Minimum Index Sum of Two Lists
33
# id: problem599
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-14
66
# difficulty: Easy
77
# categories: Hash Table
88
# link: <https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/>
@@ -74,5 +74,19 @@
7474
## @lc code=start
7575
using LeetCode
7676

77-
## add your code here:
77+
function find_restaurant(list1::Vector{String}, list2::Vector{String})::Vector{String}
78+
dic = Dict(st => (i - 1) for (i, st) in enumerate(list1))
79+
cost, res = typemax(1), String[]
80+
for (i, s) in enumerate(list2)
81+
!haskey(dic, s) && continue
82+
newcost = i - 1 + dic[s]
83+
if newcost < cost
84+
cost, res = newcost, [s]
85+
elseif newcost == cost
86+
push!(res, s)
87+
end
88+
end
89+
return res
90+
end
91+
7892
## @lc code=end

src/unresolved/798.smallest-rotation-with-highest-score.jl renamed to src/problems/798.smallest-rotation-with-highest-score.jl

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 798. Smallest Rotation with Highest Score
33
# id: problem798
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-10
66
# difficulty: Hard
77
# categories:
88
# link: <https://leetcode.com/problems/smallest-rotation-with-highest-score/description/>
@@ -58,5 +58,20 @@
5858
## @lc code=start
5959
using LeetCode
6060

61-
## add your code here:
61+
function best_rotation(nums::Vector{Int})::Int
62+
n = length(nums)
63+
diffs = fill(0, n)
64+
for (i, num) in enumerate(nums)
65+
diffs[mod(i - num - 1, n) + 1] -= 1
66+
diffs[i] += 1
67+
end
68+
ans = maxscore = score = 0
69+
for (i, num) in enumerate(@view(diffs[1:(end - 1)]))
70+
score += num
71+
if score > maxscore
72+
maxscore, ans = score, i
73+
end
74+
end
75+
return ans
76+
end
6277
## @lc code=end

src/unresolved/946.validate-stack-sequences.jl renamed to src/problems/946.validate-stack-sequences.jl

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 946. Validate Stack Sequences
33
# id: problem946
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-03-11
66
# difficulty: Medium
77
# categories: Stack
88
# link: <https://leetcode.com/problems/validate-stack-sequences/description/>
@@ -47,6 +47,19 @@
4747
#
4848
## @lc code=start
4949
using LeetCode
50+
function validate_stack_sequences(pushed::Vector{Int}, popped::Vector{Int})
51+
return validate_stack_sequences!(copy(pushed), popped)
52+
end
53+
function validate_stack_sequences!(pushed::Vector{Int}, popped::Vector{Int})::Bool
54+
stack = Int[]
55+
for num in popped
56+
while isempty(stack) || last(stack) != num
57+
isempty(pushed) && return false
58+
push!(stack, popfirst!(pushed))
59+
end
60+
pop!(stack)
61+
end
62+
return true
63+
end
5064

51-
## add your code here:
5265
## @lc code=end

src/unresolved/233.number-of-digit-one.jl

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "106.construct-binary-tree-from-inorder-and-postorder-traversal.jl" begin
2+
inorder = [9, 3, 15, 20, 7]
3+
postorder = [9, 15, 7, 20, 3]
4+
tree = build_tree_inpost(inorder, postorder)
5+
@test inorder_traversal(tree) == inorder && postorder_traversal(tree) == postorder
6+
end

test/problems/169.majority-element.jl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@testset "169.majority-element.jl" begin
2+
@test majority_element([3, 2, 3]) == 3
3+
@test majority_element([2, 2, 1, 1, 1, 2, 2]) == 2
4+
end
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@testset "233.number-of-digit-one.jl" begin
2+
@test count_digit_one(0) == 0
3+
@test all(count_digit_one(i) == 1 for i in 1:9)
4+
@test count_digit_one(13) == 6
5+
@test count_digit_one(999) == 300
6+
@test count_digit_one(213789199) == 274085040
7+
@test count_digit_one(100) == 21
8+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@testset "297.serialize-and-deserialize-binary-tree.jl" begin
2+
root = TreeNode{Int}([1, 2, 3, nothing, nothing, 4, 5])
3+
@test deserialize(serialize(root)) == root
4+
@test deserialize(serialize(nothing)) === nothing
5+
@test deserialize(serialize(TreeNode(1))) == TreeNode(1)
6+
@test deserialize(serialize(TreeNode{Int}([1, 2]))) == TreeNode{Int}([1, 2])
7+
@test deserialize(serialize(TreeNode{Int}([3, 2, 4, 3]))) == TreeNode{Int}([3, 2, 4, 3])
8+
end

test/problems/504.base-7.jl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@testset "504.base-7.jl" begin
2+
@test convert_to_base7(100) == "202"
3+
@test convert_to_base7(-7) == "-10"
4+
@test convert_to_base7(0) == "0"
5+
end

test/problems/507.perfect-number.jl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@testset "507.perfect-number.jl" begin
2+
@test perfect_number(28)
3+
@test perfect_number(7) == false
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@testset "599.minimum-index-sum-of-two-lists.jl" begin
2+
list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"]
3+
list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
4+
list3 = ["KFC", "Shogun", "Burger King"]
5+
list4 = ["KFC", "Burger King", "Tapioca Express", "Shogun"]
6+
list5 = ["KNN", "KFC", "Burger King", "Tapioca Express", "Shogun"]
7+
@test find_restaurant(list1, list2) == ["Shogun"]
8+
@test find_restaurant(list1, list3) == ["Shogun"]
9+
@test find_restaurant(list1, list4) == list4
10+
@test find_restaurant(list1, list5) ==
11+
["KFC", "Burger King", "Tapioca Express", "Shogun"]
12+
@test find_restaurant(["KFC"], ["KFC"]) == ["KFC"]
13+
end

0 commit comments

Comments
 (0)