We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 37266a5 commit b383b21Copy full SHA for b383b21
swift/0300-longest-increasing-subsequence.swift
@@ -0,0 +1,20 @@
1
+/**
2
+ * 300. Longest Increasing Subsequence
3
+ * https://leetcode.com/problems/longest-increasing-subsequence/
4
+ **/
5
+
6
+class Solution {
7
+ func lengthOfLIS(_ nums: [Int]) -> Int {
8
+ var dp: [Int] = Array(repeating: 1, count: nums.count)
9
10
+ for i in 0 ..< nums.count {
11
+ for j in 0 ..< i {
12
+ if nums[i] > nums[j] {
13
+ dp[i] = max(dp[i], dp[j] + 1)
14
+ }
15
16
17
18
+ return dp.max()!
19
20
+}
swift/0367-valid-perfect-square.swift
@@ -0,0 +1,13 @@
+ * 367. Valid Perfect Square
+ * https://leetcode.com/problems/valid-perfect-square/
+ func isPerfectSquare(_ num: Int) -> Bool {
+ for i in 1 ... num where i * i >= num {
+ return i * i == num
+ return false
swift/0383-ransom-note.swift
@@ -0,0 +1,24 @@
+ * 383. Ransom Note
+ * https://leetcode.com/problems/ransom-note/
+ func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
+ var dict: [Character: (note: Int, magazine: Int)] = [:]
+ for ch in ransomNote {
+ dict[ch, default: (0, 0)].note += 1
+ for ch in magazine {
+ dict[ch, default: (0, 0)].magazine += 1
+ for (note, magaz) in dict.values where note > magaz {
21
22
+ return true
23
24
swift/0567-permutation-in-string.swift
@@ -0,0 +1,36 @@
+ * 567. Permutation in String
+ * https://leetcode.com/problems/permutation-in-string/
+ func checkInclusion(_ s1: String, _ s2: String) -> Bool {
+ guard s1.count <= s2.count else { return false }
+ let s2arr = Array(s2)
+ let s1count = s1.count, s2count = s2.count
+ var dict: [Character: Int] = [:]
+ for ch in s1 {
+ dict[ch, default: 0] += 1
+ var l = 0, r = -1
+ while l + r + 1 < s1count {
+ r += 1
+ dict[s2arr[r], default: 0] -= 1
+ if dict[s2arr[r]] == 0 { dict[s2arr[r]] = nil }
25
+ while !dict.isEmpty && r < s2count - 1 {
26
27
28
29
+ dict[s2arr[l], default: 0] += 1
30
+ if dict[s2arr[l]] == 0 { dict[s2arr[l]] = nil }
31
+ l += 1
32
33
34
+ return dict.isEmpty
35
36
swift/1512-number-of-good-pairs.swift
@@ -13,4 +13,24 @@ class Solution {
return prev.filter{0ドル.value > 1}.map{0ドル.value * (0ドル.value-1) / 2}.reduce(0, +)
}
+class Solution2 {
+ func numIdenticalPairs(_ nums: [Int]) -> Int {
+ var dict: [Int: Int] = [:], ans = 0
+ for (i, v) in nums.enumerated() {
+ dict[v, default: 0] += 1
+ for pairCount in dict.values where pairCount > 1 {
+ var temp = pairCount
+ while temp > 1 {
+ ans += temp - 1
+ temp -= 1
+ return ans
swift/1952-three-divisors.swift
@@ -0,0 +1,17 @@
+ * 1952. Three Divisors
+ * https://leetcode.com/problems/three-divisors/
+ func isThree(_ n: Int) -> Bool {
+ var divisors = 0
+ for i in 1 ... n where n % i == 0 {
+ divisors += 1
+ if divisors > 3 { return false }
+ return divisors == 3
swift/2200-find-all-k-distant-indices-in-an-array.swift
@@ -0,0 +1,18 @@
+ * 2200. Find All K-Distant Indices in an Array
+ * https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
+ func findKDistantIndices(_ nums: [Int], _ key: Int, _ k: Int) -> [Int] {
+ var ans: [Int] = []
+ var from = 0
+ for i in 0 ..< nums.count where nums[i] == key {
+ for j in max(from, i - k) ... i + k where j < nums.count {
+ ans.append(j)
+ from = i + k + 1
swift/2367-number-of-arithmetic-triplets.swift
+ * 2367. Number of Arithmetic Triplets
+ * https://leetcode.com/problems/number-of-arithmetic-triplets/
+ func arithmeticTriplets(_ nums: [Int], _ diff: Int) -> Int {
+ var ans = 0
+ for (i, iv) in nums.enumerated() {
+ for (j, jv) in nums.enumerated() where j > i && jv - iv == diff {
+ for (k, kv) in nums.enumerated() where k > j && kv - jv == diff {
+ ans += 1
swift/2706-buy-two-chocolates.swift
@@ -0,0 +1,11 @@
+ * 2706. Buy Two Chocolates
+ * https://leetcode.com/problems/buy-two-chocolates/
+ func buyChoco(_ prices: [Int], _ money: Int) -> Int {
+ let prices = prices.sorted()
+ return prices[0] + prices[1] <= money ? money - prices[0] - prices[1] : money
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments