Skip to content

Commit cc27117

Browse files
authored
Merge pull request JuliaCN#152 from RexWzh/master
add solutions and edit solution 3
2 parents 72aa10f + f975537 commit cc27117

File tree

106 files changed

+1231
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1231
-166
lines changed

src/unresolved/101.symmetric-tree.jl renamed to src/problems/101.symmetric-tree.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 101. Symmetric Tree
33
# id: problem101
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-17
66
# difficulty: Easy
77
# categories: Tree, Depth-first Search, Breadth-first Search
88
# link: <https://leetcode.com/problems/symmetric-tree/description/>
@@ -44,5 +44,13 @@
4444
## @lc code=start
4545
using LeetCode
4646

47-
## add your code here:
47+
is_symmetric_tree(root::TreeNode{Int})::Bool = is_mirror_tree(root.left, root.right)
48+
function is_mirror_tree(
49+
t1::Union{TreeNode{Int},Nothing}, t2::Union{TreeNode{Int},Nothing}
50+
)::Bool
51+
isnothing(t1) && isnothing(t2) && return true
52+
(isnothing(t1) || isnothing(t2)) && return false
53+
t1.val != t2.val && return false
54+
return is_mirror_tree(t1.left, t2.right) && is_mirror_tree(t1.right, t2.left)
55+
end
4856
## @lc code=end

src/unresolved/1020.number-of-enclaves.jl renamed to src/problems/1020.number-of-enclaves.jl

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1020. Number of Enclaves
33
# id: problem1020
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-12
66
# difficulty: Medium
77
# categories: Depth-first Search
88
# link: <https://leetcode.com/problems/number-of-enclaves/description/>
@@ -52,5 +52,33 @@
5252
## @lc code=start
5353
using LeetCode
5454

55-
## add your code here:
55+
## BFS algorithm
56+
function num_of_enclaves_bfs(grid::Vector{Vector{Int}})::Int
57+
m, n, queue = length(grid), length(grid[1]), Tuple{Int,Int}[]
58+
(m <= 2 || n <= 2) && return 0
59+
for i in 1:m ## left and right border
60+
grid[i][1] == 1 && push!(queue, (i, 1))
61+
grid[i][n] == 1 && push!(queue, (i, n))
62+
end
63+
for i in 2:(n - 1)
64+
grid[1][i] == 1 && push!(queue, (1, i))
65+
grid[m][i] == 1 && push!(queue, (m, i))
66+
end
67+
walkoff = copy(queue)
68+
while length(queue) != 0
69+
i, j = popfirst!(queue)
70+
for (a, b) in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]
71+
if a >= 1 &&
72+
b >= 1 &&
73+
a <= m &&
74+
b <= n &&
75+
grid[a][b] != 0 &&
76+
!((a, b) in walkoff)
77+
push!(walkoff, (a, b))
78+
push!(queue, (a, b))
79+
end
80+
end
81+
end
82+
return sum(sum(line) for line in grid) - length(walkoff)
83+
end
5684
## @lc code=end

src/unresolved/113.path-sum-ii.jl renamed to src/problems/113.path-sum-ii.jl

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 113. Path Sum II
33
# id: problem113
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-01-23
66
# difficulty: Medium
77
# categories: Tree, Depth-first Search
88
# link: <https://leetcode.com/problems/path-sum-ii/description/>
@@ -43,5 +43,17 @@
4343
## @lc code=start
4444
using LeetCode
4545

46-
## add your code here:
46+
function path_sum_113(node::Union{Nothing,TreeNode}, target::Int)::Vector{Vector{Int}}
47+
isnothing(node) && return Vector{Vector{Int}}[]
48+
isnothing(node.left) &&
49+
isnothing(node.right) &&
50+
return node.val != target ? Vector{Vector{Int}}[] : [[target]]
51+
paths = path_sum_113(node.left, target - node.val)
52+
paths = vcat(paths, path_sum_113(node.right, target - node.val))
53+
for path in paths
54+
pushfirst!(path, node.val)
55+
end
56+
return paths
57+
end
58+
4759
## @lc code=end

src/unresolved/1137.n-th-tribonacci-number.jl renamed to src/problems/1137.n-th-tribonacci-number.jl

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1137. N-th Tribonacci Number
33
# id: problem1137
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-10
66
# difficulty: Easy
77
# categories: Recursion
88
# link: <https://leetcode.com/problems/n-th-tribonacci-number/description/>
@@ -47,5 +47,12 @@
4747
## @lc code=start
4848
using LeetCode
4949

50-
## add your code here:
50+
function tribonacci_1137(n::Int)::Int
51+
n <= 1 && return n
52+
a, b, c = 0, 1, 1
53+
for _ in 1:(n - 2)
54+
a, b, c = b, c, a + b + c
55+
end
56+
return c
57+
end
5158
## @lc code=end

src/unresolved/118.pascals-triangle.jl renamed to src/problems/118.pascals-triangle.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 118. Pascal's Triangle
33
# id: problem118
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-01-17
66
# difficulty: Easy
77
# categories: Array
88
# link: <https://leetcode.com/problems/pascals-triangle/description/>
@@ -35,5 +35,5 @@
3535
## @lc code=start
3636
using LeetCode
3737

38-
## add your code here:
38+
pascals_triangle(numRows::Int) = [[binomial(k, i) for i in 0:k] for k in 0:(numRows - 1)]
3939
## @lc code=end

src/unresolved/1189.maximum-number-of-balloons.jl renamed to src/problems/1189.maximum-number-of-balloons.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1189. Maximum Number of Balloons
33
# id: problem1189
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-13
66
# difficulty: Easy
77
# categories: Hash Table, String
88
# link: <https://leetcode.com/problems/maximum-number-of-balloons/description/>
@@ -56,5 +56,13 @@
5656
## @lc code=start
5757
using LeetCode
5858

59-
## add your code here:
59+
function max_num_of_ballons(text::String)::Int
60+
words = Dict{Char,Int}(s => 0 for s in "balon")
61+
for s in text
62+
if haskey(words, s)
63+
words[s] += 1
64+
end
65+
end
66+
return min(words['b'], words['a'], words['l'] ÷ 2, words['o'] ÷ 2, words['n'])
67+
end
6068
## @lc code=end

src/unresolved/121.best-time-to-buy-and-sell-stock.jl renamed to src/problems/121.best-time-to-buy-and-sell-stock.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 121. Best Time to Buy and Sell Stock
33
# id: problem121
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-01-17
66
# difficulty: Easy
77
# categories: Array, Dynamic Programming
88
# link: <https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/>
@@ -41,5 +41,13 @@
4141
## @lc code=start
4242
using LeetCode
4343

44-
## add your code here:
44+
function max_profit_121(prices::Vector{Int})::Int
45+
min_cost, profit = prices[1], 0
46+
for price in @view prices[2:end]
47+
profit = max(profit, price - min_cost)
48+
min_cost = min(min_cost, price)
49+
end
50+
return profit
51+
end
52+
4553
## @lc code=end

src/unresolved/1220.count-vowels-permutation.jl renamed to src/problems/1220.count-vowels-permutation.jl

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1220. Count Vowels Permutation
33
# id: problem1220
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-01-22
66
# difficulty: Hard
77
# categories: Dynamic Programming
88
# link: <https://leetcode.com/problems/count-vowels-permutation/description/>
@@ -58,5 +58,20 @@
5858
## @lc code=start
5959
using LeetCode
6060

61-
## add your code here:
61+
function count_vowel_permutation(n::Int)::Int
62+
table = Dict{Char,Int}(i => 1 for i in "aeiou")
63+
quotient = 10^9 + 7
64+
for _ in 1:(n - 1)
65+
a = table['e'] + table['i'] + table['u']
66+
e = table['a'] + table['i']
67+
i = table['e'] + table['o']
68+
o = table['i']
69+
u = table['i'] + table['o']
70+
table['a'], table['e'], table['i'], table['o'], table['u'] = map(
71+
x -> mod(x, quotient), [a, e, i, o, u]
72+
)
73+
end
74+
return mod(sum(values(table)), quotient)
75+
end
76+
6277
## @lc code=end

src/unresolved/1332.remove-palindromic-subsequences.jl renamed to src/problems/1332.remove-palindromic-subsequences.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# title: 1332. Remove Palindromic Subsequences
33
# id: problem1332
44
# author: Tian Jun
5-
# date: 2020-10-31
5+
# date: 2022-01-22
66
# difficulty: Easy
77
# categories: String
88
# link: <https://leetcode.com/problems/remove-palindromic-subsequences/description/>
@@ -70,5 +70,6 @@
7070
## @lc code=start
7171
using LeetCode
7272

73-
## add your code here:
73+
remove_palindromic_subsequences(s::String) = (s == reverse(s)) ? 1 : 2
74+
7475
## @lc code=end

src/unresolved/1345.jump-game-iv.jl renamed to src/problems/1345.jump-game-iv.jl

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1345. Jump Game IV
33
# id: problem1345
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-01-22
66
# difficulty: Hard
77
# categories: Breadth-first Search
88
# link: <https://leetcode.com/problems/jump-game-iv/description/>
@@ -78,5 +78,35 @@
7878
## @lc code=start
7979
using LeetCode
8080

81-
## add your code here:
81+
function min_jumps_1345(arr::Vector{Int})::Int
82+
## data initialize
83+
n, step = length(arr), 0
84+
nums = Dict{Int,Set}(x => Set{Int}() for x in Set{Int}(arr))
85+
for (i, val) in enumerate(arr)
86+
push!(nums[val], i)
87+
end
88+
## BFS Search
89+
up_nums, up_inds, inds = Set{Int}([arr[1]]), Set{Int}([1]), Set{Int}([0, 1, n + 1])
90+
while !(n in up_inds)
91+
down_inds = Set{Int}()
92+
## indexs by numbers
93+
for num in up_nums
94+
union!(down_inds, nums[num])
95+
end
96+
## nearby indexs
97+
for i in up_inds
98+
push!(down_inds, i - 1)
99+
push!(down_inds, i + 1)
100+
end
101+
## remove extra indexs
102+
for i in down_inds
103+
i in inds && delete!(down_inds, i)
104+
end
105+
up_inds, up_nums = down_inds, Set{Int}(arr[i] for i in down_inds)
106+
union!(inds, up_inds)
107+
step += 1
108+
end
109+
return step
110+
end
111+
82112
## @lc code=end

src/unresolved/136.single-number.jl renamed to src/problems/136.single-number.jl

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 136. Single Number
33
# id: problem136
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-15
66
# difficulty: Easy
77
# categories: Hash Table, Bit Manipulation
88
# link: <https://leetcode.com/problems/single-number/description/>
@@ -53,5 +53,11 @@
5353
## @lc code=start
5454
using LeetCode
5555

56-
## add your code here:
56+
function single_number_136(nums::Vector{Int})::Int
57+
a = 0
58+
for i in nums
59+
a ⊻= i
60+
end
61+
return a
62+
end
5763
## @lc code=end

src/unresolved/1380.lucky-numbers-in-a-matrix.jl renamed to src/problems/1380.lucky-numbers-in-a-matrix.jl

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1380. Lucky Numbers in a Matrix
33
# id: problem1380
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-15
66
# difficulty: Easy
77
# categories: Array
88
# link: <https://leetcode.com/problems/lucky-numbers-in-a-matrix/description/>
@@ -56,6 +56,21 @@
5656
#
5757
## @lc code=start
5858
using LeetCode
59-
60-
## add your code here:
59+
function lucky_numbers(matrix::Vector{Vector{Int}})::Vector{Int}
60+
m, n = length(matrix), length(matrix[1])
61+
row_min = Dict{Int,Tuple{Int,Int}}(i => (matrix[i][1], 1) for i in 1:m)
62+
col_max = Dict{Int,Tuple{Int,Int}}(j => (0, 0) for j in 1:n)
63+
for i in 1:m
64+
for j in 1:n
65+
c = matrix[i][j]
66+
if c < row_min[i][1]
67+
row_min[i] = (c, j)
68+
end
69+
if c > col_max[j][1]
70+
col_max[j] = (c, i)
71+
end
72+
end
73+
end
74+
return [row_min[i][1] for i in 1:m if col_max[row_min[i][2]][2] == i]
75+
end
6176
## @lc code=end

src/unresolved/141.linked-list-cycle.jl renamed to src/problems/141.linked-list-cycle.jl

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 141. Linked List Cycle
33
# id: problem141
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-15
66
# difficulty: Easy
77
# categories: Linked List, Two Pointers
88
# link: <https://leetcode.com/problems/linked-list-cycle/description/>
@@ -70,6 +70,13 @@
7070
#
7171
## @lc code=start
7272
using LeetCode
73-
74-
## add your code here:
73+
function has_cycle(head::Union{ListNode,Nothing})::Bool
74+
isnothing(head) && return false
75+
swift = slow = head
76+
while !isnothing(swift.next) && !isnothing(swift.next.next)
77+
slow, swift = slow.next, swift.next.next
78+
slow === swift && return true
79+
end
80+
return false
81+
end
7582
## @lc code=end

src/unresolved/1447.simplified-fractions.jl renamed to src/problems/1447.simplified-fractions.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1447. Simplified Fractions
33
# id: problem1447
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: zhwang
5+
# date: 2022-02-10
66
# difficulty: Medium
77
# categories: Math
88
# link: <https://leetcode.com/problems/simplified-fractions/description/>
@@ -57,5 +57,5 @@
5757
## @lc code=start
5858
using LeetCode
5959

60-
## add your code here:
60+
simplified_fractions(n::Int) = ["$p/$q" for q in 2:n for p in 1:(q - 1) if gcd(p, q) == 1]
6161
## @lc code=end

0 commit comments

Comments
 (0)