Skip to content

Commit 0daadc2

Browse files
committed
Solution to 1019
1 parent de8fe31 commit 0daadc2

4 files changed

+26
-6
lines changed

src/problems/1018.binary-prefix-divisible-by-5.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ using LeetCode
6363

6464
function prefixes_div_by5(nums::Vector{Int})
6565
len = length(nums)
66-
res = fill(false, len)
66+
res = fill(false, len)
6767
cur = 0
6868
for i in 1:len
6969
cur = cur * 2 + nums[i]
7070
cur % 5 == 0 && (res[i] = true)
7171
end
72-
res
72+
return res
7373
end
7474
## @lc code=end

src/unresolved/1019.next-greater-node-in-linked-list.jl renamed to src/problems/1019.next-greater-node-in-linked-list.jl

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1019. Next Greater Node In Linked List
33
# id: problem1019
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Indigo
5+
# date: 2022-03-19
66
# difficulty: Medium
77
# categories: Linked List, Stack
88
# link: <https://leetcode.com/problems/next-greater-node-in-linked-list/description/>
@@ -61,5 +61,18 @@
6161
## @lc code=start
6262
using LeetCode
6363

64-
## add your code here:
64+
function next_larger_nodes(head::ListNode{Int})
65+
head = reverse_list(head)
66+
stk = Int[]
67+
res = Int[]
68+
while !isnothing(head)
69+
while !isempty(stk) && stk[end] <= val(head)
70+
pop!(stk)
71+
end
72+
pushfirst!(res, isempty(stk) ? 0 : stk[end])
73+
push!(stk, val(head))
74+
head = next(head)
75+
end
76+
return res
77+
end
6578
## @lc code=end

src/problems/206.reverse-linked-list.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
## @lc code=start
2929
using LeetCode
3030

31-
function reverse_list(head::Union{ListNode,Nothing})::Union{ListNode,Nothing}
31+
reverse_list(head::Nothing) = head
32+
function reverse_list(head::ListNode)::ListNode
3233
new_head = pre_node = nothing
3334
while !isnothing(head)
3435
new_head = ListNode(head.val)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "1019.next-greater-node-in-linked-list.jl" begin
2+
@test next_larger_nodes(ListNode{Int}([1, 7, 5, 1, 9, 2, 5, 1])) ==
3+
[7, 9, 9, 9, 0, 5, 0, 0]
4+
@test next_larger_nodes(ListNode{Int}([2, 7, 4, 3, 5])) == [7, 0, 5, 5, 0]
5+
@test next_larger_nodes(ListNode{Int}([2, 1, 5])) == [5, 5, 0]
6+
end

0 commit comments

Comments
 (0)