Skip to content

feat: update lc problems #2988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ class Solution {
func maxProduct(_ words: [String]) -> Int {
let n = words.count
var masks = [Int](repeating: 0, count: n)

for i in 0..<n {
for c in words[i] {
masks[i] |= 1 << (c.asciiValue! - Character("a").asciiValue!)
}
}

var maxProduct = 0
for i in 0..<n {
for j in i+1..<n {
Expand All @@ -203,7 +203,7 @@ class Solution {
}
}
}

return maxProduct
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Solution {
var ans = inf
var sum = 0
var i = 0

for j in 0..<nums.count {
sum += nums[j]
while sum >= target {
Expand All @@ -194,7 +194,7 @@ class Solution {
i += 1
}
}

return ans == inf ? 0 : ans
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ function numSubarrayProductLessThanK(nums: number[], k: number): number {
class Solution {
func numSubarrayProductLessThanK(_ nums: [Int], _ k: Int) -> Int {
if k <= 1 { return 0 }

var product: Int = 1
var ans: Int = 0
var left: Int = 0

for right in 0..<nums.count {
product *= nums[right]
while product >= k {
Expand All @@ -168,7 +168,7 @@ class Solution {
}
ans += right - left + 1
}

return ans
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 010. 和为 k 的子数组/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ class Solution {
var cnt: [Int: Int] = [0: 1]
var ans = 0
var s = 0

for x in nums {
s += x
ans += cnt[s - k, default: 0]
cnt[s, default: 0] += 1
}

return ans
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Solution {
var d: [Int: Int] = [0: -1]
var ans = 0
var s = 0

for i in 0..<nums.count {
s += nums[i] == 0 ? -1 : 1
if let prevIndex = d[s] {
Expand All @@ -178,7 +178,7 @@ class Solution {
d[s] = i
}
}

return ans
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Solution {
func pivotIndex(_ nums: [Int]) -> Int {
var leftSum = 0
var rightSum = nums.reduce(0, +)

for i in 0..<nums.count {
rightSum -= nums[i]
if leftSum == rightSum {
Expand Down
2 changes: 1 addition & 1 deletion lcof2/剑指 Offer II 013. 二维子矩阵的和/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class NumMatrix {
let m = matrix.count
let n = matrix[0].count
prefixSum = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)

for i in 1...m {
for j in 1...n {
prefixSum[i][j] = prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1] + matrix[i - 1][j - 1]
Expand Down
12 changes: 6 additions & 6 deletions lcof2/剑指 Offer II 014. 字符串中的变位词/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,29 +450,29 @@ class Solution {
if m > n {
return false
}

var cnt1 = [Int](repeating: 0, count: 26)
var cnt2 = [Int](repeating: 0, count: 26)
let aAscii = Character("a").asciiValue!

for i in 0..<m {
cnt1[Int(s1[s1.index(s1.startIndex, offsetBy: i)].asciiValue! - aAscii)] += 1
cnt2[Int(s2[s2.index(s2.startIndex, offsetBy: i)].asciiValue! - aAscii)] += 1
}

if cnt1 == cnt2 {
return true
}

for i in m..<n {
cnt2[Int(s2[s2.index(s2.startIndex, offsetBy: i)].asciiValue! - aAscii)] += 1
cnt2[Int(s2[s2.index(s2.startIndex, offsetBy: i - m)].asciiValue! - aAscii)] -= 1

if cnt1 == cnt2 {
return true
}
}

return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tags:

<p>你从某个城市开始,穿越各种道路,最终从&nbsp;<strong>任何一个&nbsp;</strong>城市买&nbsp;<strong>一个&nbsp;</strong>苹果。在你买了那个苹果之后,你必须回到你&nbsp;<strong>开始的&nbsp;</strong>城市,但现在所有道路的成本将&nbsp;<strong>乘以&nbsp;</strong>一个给定的因子 <code>k</code>。</p>

<p>给定整数 <code>k</code>,返回<em>一个大小为 <code>n</code> 的数组 <code>answer</code>,其中 <code>answer[i]</code>&nbsp;是从城市 <code>i</code> 开始购买一个苹果的&nbsp;<strong>最小&nbsp;</strong>总成本。</em></p>
<p>给定整数 <code>k</code>,返回<em>一个大小为 <code>n</code> 的从 1 开始的数组 <code>answer</code>,其中 <code>answer[i]</code>&nbsp;是从城市 <code>i</code> 开始购买一个苹果的&nbsp;<strong>最小&nbsp;</strong>总成本。</em></p>

<p>&nbsp;</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ tags:
<li>从 2023-06-01 10:45:00 到 2023-06-01 12:00:00 在停车场 2:1.25 小时,费用 6.00</li>
<li>从 2023-06-03 07:00:00 到 2023-06-03 09:00:00 在停车场 3:2 小时,费用 4.00</li>
</ul>
总共支付费用:18.00,总小时:7.5,每小时平均费用:2.40,停车场 1 总花费时间最长:2.5 小时。</li>
总共支付费用:18.00,总小时:7.5,每小时平均费用:2.40,停车场 1 总花费时间最长:4.25&nbsp;小时。</li>
<li>对于汽车 ID 1002:
<ul>
<li>从 2023-06-01 09:00:00 到 2023-06-01 11:30:00 在停车场 2:2.5 小时,费用 4.00</li>
<li>从 2023-06-02 12:00:00 到 2023-06-02 14:00:00 在停车场 3:2 小时,费用 2.00</li>
</ul>
总共支付费用:6.00,总小时:4.5,每小时平均费用:1.33,停车场 2 总花费时间最长:4.25 小时。</li>
总共支付费用:6.00,总小时:4.5,每小时平均费用:1.33,停车场 2 总花费时间最长:2.5 小时。</li>
</ul>

<p><b>注意:</b>&nbsp;输出表以 car_id 升序排序。</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Each row of this table contains the ID of the parking lot, the ID of the car, th
<li>From 2023-06-01 10:45:00 to 2023-06-01 12:00:00 in lot 2: 1.25 hours, fee 6.00</li>
<li>From 2023-06-03 07:00:00 to 2023-06-03 09:00:00 in lot 3: 2 hours, fee 4.00</li>
</ul>
Total fee paid: 18.00, total hours: 7.5, average hourly fee: 2.40, most time spent in lot 1: 2.5 hours.</li>
Total fee paid: 18.00, total hours: 7.5, average hourly fee: 2.40, most time spent in lot 1: 4.25 hours.</li>
<li>For car ID 1002:
<ul>
<li>From 2023-06-01 09:00:00 to 2023-06-01 11:30:00 in lot 2: 2.5 hours, fee 4.00</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3167.Be

<p><strong>解释:</strong></p>

<p>字符 "a" 和 "b" 在输入中只出现了一次,但 "c" 出现了两次,第一次出现了 9 次,另一次是 1 次。</p>
<p>字符 "a" 和 "b" 在输入中只出现了一次,但 "c" 出现了两次,第一次长度为 9,另一次是长度为 1。</p>

<p>因此,在结果字符串中,它应当出现 10。</p>
<p>因此,在结果字符串中,它应当长度为 10。</p>
</div>

<p><strong class="example">示例 2:</strong></p>
Expand Down