mirror of https://github.com/doocs/leetcode.git
feat: update lc problems (#2988)
This commit is contained in:
parent
92c9e06c99
commit
4b72ba0295
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ tags:
|
|||
|
||||
<p>你从某个城市开始,穿越各种道路,最终从 <strong>任何一个 </strong>城市买 <strong>一个 </strong>苹果。在你买了那个苹果之后,你必须回到你 <strong>开始的 </strong>城市,但现在所有道路的成本将 <strong>乘以 </strong>一个给定的因子 <code>k</code>。</p>
|
||||
|
||||
<p>给定整数 <code>k</code>,返回<em>一个大小为 <code>n</code> 的数组 <code>answer</code>,其中 <code>answer[i]</code> 是从城市 <code>i</code> 开始购买一个苹果的 <strong>最小 </strong>总成本。</em></p>
|
||||
<p>给定整数 <code>k</code>,返回<em>一个大小为 <code>n</code> 的从 1 开始的数组 <code>answer</code>,其中 <code>answer[i]</code> 是从城市 <code>i</code> 开始购买一个苹果的 <strong>最小 </strong>总成本。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
|
|
|
|||
|
|
@ -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 小时。</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> 输出表以 car_id 升序排序。</p>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue