Skip to content

Commit b60c754

Browse files
authored
Add 6 solutions and fix typo in#1023 (#174)
1 parent 2eabb4f commit b60c754

14 files changed

+179
-23
lines changed

src/problems/1023.camelcase-matching.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ function matches(query, pattern)
7474
end
7575
i += 1
7676
end
77-
return j > length(pattern) && all(!isuppercase, query[i:end])
77+
return j > length(pattern) && all(islowercase, @view(query[i:end]))
7878
end
7979

80-
function camelMatch(queries, pattern)
81-
return [matches(query, pattern) for query in queries]
82-
end
80+
camelMatch(queries, pattern) = matches.(queries, Ref(pattern))
8381

8482
## @lc code=end

src/unresolved/28.implement-strstr.jl src/problems/28.implement-strstr.jl

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 28. Implement strStr()
33
# id: problem28
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Pixia1234
5+
# date: 2024-07-13
66
# difficulty: Easy
77
# categories: Two Pointers, String
88
# link: <https://leetcode.com/problems/implement-strstr/description/>
@@ -61,5 +61,24 @@
6161
## @lc code=start
6262
using LeetCode
6363

64-
## add your code here:
64+
function strStr(haystack::String, needle::String)
65+
needle == "" && return 0
66+
for i in 1:(length(haystack) - length(needle) + 1)
67+
if @view(haystack[i:(i + length(needle) - 1)]) == needle
68+
return i - 1 # Notice that Julia is 1-indexed, and here we need 0-indexed so minus 1
69+
end
70+
end
71+
return -1
72+
end
73+
74+
function strStr2(haystack::AbstractString, needle::AbstractString)
75+
# border case
76+
isempty(needle) && return 0
77+
length(needle) > length(haystack) && return -1
78+
# match needle
79+
needle == @view(haystack[1:length(needle)]) && return 0
80+
# recursive search
81+
ind = @views strStr2(haystack[2:end], needle)
82+
return ind == -1 ? -1 : ind + 1
83+
end
6584
## @lc code=end

src/unresolved/29.divide-two-integers.jl src/problems/29.divide-two-integers.jl

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 29. Divide Two Integers
33
# id: problem29
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Pixia1234
5+
# date: 2024-07-17
66
# difficulty: Medium
77
# categories: Math, Binary Search
88
# link: <https://leetcode.com/problems/divide-two-integers/description/>
@@ -69,5 +69,18 @@
6969
## @lc code=start
7070
using LeetCode
7171

72-
## add your code here:
72+
function divide(dividend::Int, divisor::Int)::Int
73+
sign = (dividend < 0) (divisor < 0)
74+
dividend, divisor = abs(dividend), abs(divisor)
75+
result = 0
76+
while dividend >= divisor
77+
shift = 0
78+
while dividend >= (divisor << shift)
79+
shift += 1
80+
end
81+
dividend -= divisor << (shift - 1)
82+
result += 1 << (shift - 1)
83+
end
84+
return sign ? -result : result
85+
end
7386
## @lc code=end

src/unresolved/38.count-and-say.jl src/problems/38.count-and-say.jl

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 38. Count and Say
33
# id: problem38
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Pixia1234
5+
# date: 2024-07-17
66
# difficulty: Easy
77
# categories: String
88
# link: <https://leetcode.com/problems/count-and-say/description/>
@@ -62,5 +62,26 @@
6262
## @lc code=start
6363
using LeetCode
6464

65-
## add your code here:
65+
function countandsay(n::Int)
66+
# Base case
67+
n == 1 && return "1"
68+
# Get the previous term
69+
previous_term = countandsay(n - 1)
70+
# Generate the current term by "saying" the previous term
71+
current_term = ""
72+
count = 0
73+
current_char = previous_term[1]
74+
75+
for char in previous_term
76+
if char == current_char
77+
count += 1
78+
else
79+
current_term *= string(count) * current_char
80+
current_char = char
81+
count = 1
82+
end
83+
end
84+
# Append the last group
85+
current_term * string(count) * current_char
86+
end
6687
## @lc code=end

src/unresolved/40.combination-sum-ii.jl src/problems/40.combination-sum-ii.jl

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 40. Combination Sum II
33
# id: problem40
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Pixia1234
5+
# date: 2024-07-18
66
# difficulty: Medium
77
# categories: Array, Backtracking
88
# link: <https://leetcode.com/problems/combination-sum-ii/description/>
@@ -57,5 +57,29 @@
5757
## @lc code=start
5858
using LeetCode
5959

60-
## add your code here:
60+
function combinationSum(candidates::AbstractVector{Int}, target::Int)
61+
res = Vector{Vector{Int}}()
62+
return combinationSum!(sort(candidates), target, Int[], res)
63+
end
64+
65+
function combinationSum!(
66+
candidates::AbstractVector{Int},
67+
target::Int,
68+
path::AbstractVector{Int},
69+
res::Vector{Vector{Int}},
70+
)
71+
# if the target is 0, we find a solution
72+
target == 0 && return push!(res, copy(path))
73+
length(candidates) == 0 || target < first(candidates) && return res
74+
75+
# use @view to avoid copying the array
76+
for (i, candidate) in enumerate(candidates)
77+
i > 1 && candidate == candidates[i - 1] && continue
78+
candidate > target && break
79+
subcandidates = @view(candidates[1:length(candidate) .!= i]) # skip the current candidate
80+
combinationSum!(subcandidates, target - candidate, push!(path, candidate), res)
81+
end
82+
return res
83+
end
84+
6185
## @lc code=end

src/unresolved/46.permutations.jl src/problems/46.permutations.jl

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 46. Permutations
33
# id: problem46
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Pixia1234
5+
# date: 2024-07-19
66
# difficulty: Medium
77
# categories: Backtracking
88
# link: <https://leetcode.com/problems/permutations/description/>
@@ -50,5 +50,26 @@
5050
## @lc code=start
5151
using LeetCode
5252

53-
## add your code here:
53+
permutation(nums::Vector{Int}) = permutation!(copy(nums))
54+
function permutation!(nums::Vector{Int})
55+
sort!(nums)
56+
res = Vector{Vector{Int}}()
57+
n = length(nums)
58+
sizehint!(res, factorial(n))
59+
function dfs(nums::Vector{Int}, path::Vector{Int}, used::Vector{Bool}, len::Int)
60+
if len == n # alternatively, len = sum(used)
61+
push!(res, copy(path))
62+
return nothing
63+
end
64+
for i in 1:n
65+
used[i] && continue
66+
used[i] = true
67+
path[len + 1] = nums[i]
68+
dfs(nums, path, used, len + 1)
69+
used[i] = false
70+
end
71+
end
72+
dfs(nums, Vector{Int}(undef, n), fill(false, n), 0)
73+
return res
74+
end
5475
## @lc code=end

src/unresolved/47.permutations-ii.jl src/problems/47.permutations-ii.jl

+23-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,27 @@
4444
## @lc code=start
4545
using LeetCode
4646

47-
## add your code here:
47+
function permutationII(nums::Vector{Int})::Vector{Vector{Int}}
48+
res = Vector{Vector{Int}}()
49+
n = length(nums)
50+
function dfs(nums::Vector{Int}, path::Vector{Int}, used::Vector{Bool})
51+
if length(path) == n
52+
push!(res, copy(path))
53+
return nothing
54+
end
55+
@inbounds for i in 1:n
56+
if used[i] || (i > 1 && nums[i] == nums[i - 1] && !used[i - 1])
57+
continue
58+
end
59+
push!(path, nums[i])
60+
used[i] = true
61+
dfs(nums, path, used)
62+
pop!(path)
63+
used[i] = false
64+
end
65+
end
66+
sort!(nums)
67+
dfs(nums, Vector{Int}(), fill(false, n))
68+
return res
69+
end
4870
## @lc code=end
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "1023.camelcase-matching.jl" begin
2-
@test camelMatch(("FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"),"FB") == ("1,0,1,1,0")
3-
@test camelMatch(("FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"),"FoBa") == ("1,0,1,0,0")
4-
@test camelMatch(("FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"),"FoBaT") == ("0,1,0,0,0")
2+
@test camelMatch(["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"],"FB") == [true,false,true,true,false]
3+
@test camelMatch(["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"],"FoBa") == [true,false,true,false,false]
4+
@test camelMatch(["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"],"FoBaT") == [false,true,false,false,false]
55
end

test/problems/28.implement-strstr.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@testset "28.implement-strstr.jl" begin
2+
@test strStr("hello", "ll") == 2
3+
@test strStr("aaaaa", "bba") == -1
4+
@test strStr("sadbutsad", "sad") == 0
5+
@test strStr("leetcode", "leeto") == -1
6+
@test strStr2("hello", "ll") == 2
7+
@test strStr2("aaaaa", "bba") == -1
8+
@test strStr2("sadbutsad", "sad") == 0
9+
@test strStr2("leetcode", "leeto") == -1
10+
end
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "29.divide-two-integers.jl" begin
2+
@test divide(10, 3) == 3
3+
@test divide(7, -3) == -2
4+
@test divide(0, 1) == 0
5+
@test divide(1, 1) == 1
6+
end

test/problems/38.count-and-say.jl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "38.count-and-say.jl" begin
2+
@test countandsay(1) == "1"
3+
@test countandsay(4) == "1211"
4+
@test countandsay(5) == "111221"
5+
@test countandsay(6) == "312211"
6+
end
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@testset "40.combination-sum-ii.jl" begin
2+
@test combinationSum([10, 1, 2, 7, 6, 1, 5], 8) ==
3+
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
4+
@test combinationSum([2, 5, 2, 1, 2], 5) == [[1, 2, 2], [5]]
5+
end

test/problems/46.permutations.jl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@testset "46.permutations.jl" begin
2+
@test sort!(permutation([1, 3, 2])) ==
3+
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
4+
@test permutation([0, 1]) == [[0, 1], [1, 0]]
5+
@test permutation([1]) == [[1]]
6+
end

test/problems/47.permutations-ii.jl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@testset "47.permutations-ii.jl" begin
2+
@test permutationII([1, 1, 2]) == [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
3+
@test permutationII([1, 2, 3]) ==
4+
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
5+
end

0 commit comments

Comments
 (0)