Skip to content

Commit c253ed1

Browse files
authored
Merge branch 'doocs:main' into main
2 parents fbdbd9b + 4bd6a33 commit c253ed1

File tree

230 files changed

+36812
-27748
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+36812
-27748
lines changed

images/starcharts.svg

Lines changed: 25855 additions & 25748 deletions
Loading

lcof2/剑指 Offer II 065. 最短的单词编码/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,51 @@ func dfs(cur *trie, l int) int {
218218
}
219219
```
220220

221+
#### Swift
222+
223+
```swift
224+
class Trie {
225+
var children = [Trie?](repeating: nil, count: 26)
226+
}
227+
228+
class Solution {
229+
func minimumLengthEncoding(_ words: [String]) -> Int {
230+
let root = Trie()
231+
232+
for word in words {
233+
var current = root
234+
for char in word.reversed() {
235+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
236+
if current.children[index] == nil {
237+
current.children[index] = Trie()
238+
}
239+
current = current.children[index]!
240+
}
241+
}
242+
243+
return dfs(root, 1)
244+
}
245+
246+
private func dfs(_ current: Trie, _ length: Int) -> Int {
247+
var isLeaf = true
248+
var result = 0
249+
250+
for child in current.children {
251+
if let child = child {
252+
isLeaf = false
253+
result += dfs(child, length + 1)
254+
}
255+
}
256+
257+
if isLeaf {
258+
result += length
259+
}
260+
261+
return result
262+
}
263+
}
264+
```
265+
221266
<!-- tabs:end -->
222267

223268
<!-- solution:end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Trie {
2+
var children = [Trie?](repeating: nil, count: 26)
3+
}
4+
5+
class Solution {
6+
func minimumLengthEncoding(_ words: [String]) -> Int {
7+
let root = Trie()
8+
9+
for word in words {
10+
var current = root
11+
for char in word.reversed() {
12+
let index = Int(char.asciiValue! - Character("a").asciiValue!)
13+
if current.children[index] == nil {
14+
current.children[index] = Trie()
15+
}
16+
current = current.children[index]!
17+
}
18+
}
19+
20+
return dfs(root, 1)
21+
}
22+
23+
private func dfs(_ current: Trie, _ length: Int) -> Int {
24+
var isLeaf = true
25+
var result = 0
26+
27+
for child in current.children {
28+
if let child = child {
29+
isLeaf = false
30+
result += dfs(child, length + 1)
31+
}
32+
}
33+
34+
if isLeaf {
35+
result += length
36+
}
37+
38+
return result
39+
}
40+
}

lcof2/剑指 Offer II 066. 单词之和/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,34 @@ func (this *MapSum) Sum(prefix string) int {
196196
*/
197197
```
198198

199+
#### Swift
200+
201+
```swift
202+
class MapSum {
203+
private var data: [String: Int]
204+
private var t: [String: Int]
205+
206+
init() {
207+
data = [String: Int]()
208+
t = [String: Int]()
209+
}
210+
211+
func insert(_ key: String, _ val: Int) {
212+
let old = t[key] ?? 0
213+
t[key] = val
214+
for i in 1...key.count {
215+
let endIndex = key.index(key.startIndex, offsetBy: i)
216+
let k = String(key[key.startIndex..<endIndex])
217+
data[k, default: 0] += (val - old)
218+
}
219+
}
220+
221+
func sum(_ prefix: String) -> Int {
222+
return data[prefix] ?? 0
223+
}
224+
}
225+
```
226+
199227
<!-- tabs:end -->
200228

201229
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MapSum {
2+
private var data: [String: Int]
3+
private var t: [String: Int]
4+
5+
init() {
6+
data = [String: Int]()
7+
t = [String: Int]()
8+
}
9+
10+
func insert(_ key: String, _ val: Int) {
11+
let old = t[key] ?? 0
12+
t[key] = val
13+
for i in 1...key.count {
14+
let endIndex = key.index(key.startIndex, offsetBy: i)
15+
let k = String(key[key.startIndex..<endIndex])
16+
data[k, default: 0] += (val - old)
17+
}
18+
}
19+
20+
func sum(_ prefix: String) -> Int {
21+
return data[prefix] ?? 0
22+
}
23+
}

lcof2/剑指 Offer II 067. 最大的异或/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,34 @@ func findMaximumXOR(nums []int) int {
235235
}
236236
```
237237

238+
#### Swift
239+
240+
```swift
241+
class Solution {
242+
func findMaximumXOR(_ numbers: [Int]) -> Int {
243+
var max = 0
244+
var mask = 0
245+
246+
for i in stride(from: 30, through: 0, by: -1) {
247+
let current = 1 << i
248+
mask ^= current
249+
var set = Set<Int>()
250+
for num in numbers {
251+
set.insert(mask & num)
252+
}
253+
let flag = max | current
254+
for prefix in set {
255+
if set.contains(prefix ^ flag) {
256+
max = flag
257+
break
258+
}
259+
}
260+
}
261+
return max
262+
}
263+
}
264+
```
265+
238266
<!-- tabs:end -->
239267

240268
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func findMaximumXOR(_ numbers: [Int]) -> Int {
3+
var max = 0
4+
var mask = 0
5+
6+
for i in stride(from: 30, through: 0, by: -1) {
7+
let current = 1 << i
8+
mask ^= current
9+
var set = Set<Int>()
10+
for num in numbers {
11+
set.insert(mask & num)
12+
}
13+
let flag = max | current
14+
for prefix in set {
15+
if set.contains(prefix ^ flag) {
16+
max = flag
17+
break
18+
}
19+
}
20+
}
21+
return max
22+
}
23+
}

lcof2/剑指 Offer II 068. 查找插入位置/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ var searchInsert = function (nums, target) {
170170
};
171171
```
172172

173+
#### Swift
174+
175+
```swift
176+
class Solution {
177+
func searchInsert(_ nums: [Int], _ target: Int) -> Int {
178+
var left = 0
179+
var right = nums.count
180+
while left < right {
181+
let mid = (left + right) / 2
182+
if nums[mid] >= target {
183+
right = mid
184+
} else {
185+
left = mid + 1
186+
}
187+
}
188+
return left
189+
}
190+
}
191+
```
192+
173193
<!-- tabs:end -->
174194

175195
<!-- solution:end -->

lcof2/剑指 Offer II 068. 查找插入位置/Solution.swift

Whitespace-only changes.

0 commit comments

Comments
 (0)