feat: update lc problems (#2988)

This commit is contained in:
Libin YANG 2024-06-01 00:05:27 +08:00 committed by GitHub
parent 92c9e06c99
commit 4b72ba0295
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 26 additions and 26 deletions

View File

@ -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 {
@ -203,7 +203,7 @@ class Solution {
}
}
}
return maxProduct
}
}

View File

@ -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 {
@ -194,7 +194,7 @@ class Solution {
i += 1
}
}
return ans == inf ? 0 : ans
}
}

View File

@ -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 {
@ -168,7 +168,7 @@ class Solution {
}
ans += right - left + 1
}
return ans
}
}

View File

@ -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
}
}

View File

@ -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] {
@ -178,7 +178,7 @@ class Solution {
d[s] = i
}
}
return ans
}
}

View File

@ -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 {

View File

@ -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]

View File

@ -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
}
}

View File

@ -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>

View File

@ -83,13 +83,13 @@ tags:
<li>从 2023-06-01 10:45:00 到 2023-06-01 12:00:00 在停车场 21.25 小时,费用 6.00</li>
<li>从 2023-06-03 07:00:00 到 2023-06-03 09:00:00 在停车场 32 小时,费用 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 在停车场 22.5 小时,费用 4.00</li>
<li>从 2023-06-02 12:00:00 到 2023-06-02 14:00:00 在停车场 32 小时,费用 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>

View File

@ -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>

View File

@ -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>