Skip to content

Commit 4eaff48

Browse files
authored
feat: add swift implementation to lcof problem: No.49 (#2934)
1 parent a72988e commit 4eaff48

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

lcof/面试题49. 丑数/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,92 @@ public class Solution {
237237
}
238238
```
239239

240+
#### Swift
241+
242+
```swift
243+
class Solution {
244+
func nthUglyNumber(_ n: Int) -> Int {
245+
var vis = Set<Int64>()
246+
var pq = PriorityQueue<Int64>()
247+
let factors: [Int64] = [2, 3, 5]
248+
249+
pq.push(1)
250+
vis.insert(1)
251+
var ans: Int64 = 0
252+
253+
for _ in 0..<n {
254+
ans = pq.pop()!
255+
for factor in factors {
256+
let next = ans * factor
257+
if vis.insert(next).inserted {
258+
pq.push(next)
259+
}
260+
}
261+
}
262+
263+
return Int(ans)
264+
}
265+
}
266+
267+
struct PriorityQueue<T: Comparable> {
268+
private var heap: [T] = []
269+
270+
var isEmpty: Bool {
271+
return heap.isEmpty
272+
}
273+
274+
mutating func push(_ element: T) {
275+
heap.append(element)
276+
heapifyUp(from: heap.count - 1)
277+
}
278+
279+
mutating func pop() -> T? {
280+
guard !heap.isEmpty else {
281+
return nil
282+
}
283+
if heap.count == 1 {
284+
return heap.removeLast()
285+
}
286+
let value = heap[0]
287+
heap[0] = heap.removeLast()
288+
heapifyDown(from: 0)
289+
return value
290+
}
291+
292+
private mutating func heapifyUp(from index: Int) {
293+
var index = index
294+
let element = heap[index]
295+
while index > 0 {
296+
let parentIndex = (index - 1) / 2
297+
if element >= heap[parentIndex] {
298+
break
299+
}
300+
heap[index] = heap[parentIndex]
301+
index = parentIndex
302+
}
303+
heap[index] = element
304+
}
305+
306+
private mutating func heapifyDown(from index: Int) {
307+
var index = index
308+
let element = heap[index]
309+
let count = heap.count
310+
while index < count / 2 {
311+
var childIndex = index * 2 + 1
312+
if childIndex + 1 < count && heap[childIndex + 1] < heap[childIndex] {
313+
childIndex += 1
314+
}
315+
if element <= heap[childIndex] {
316+
break
317+
}
318+
heap[index] = heap[childIndex]
319+
index = childIndex
320+
}
321+
heap[index] = element
322+
}
323+
}
324+
```
325+
240326
<!-- tabs:end -->
241327

242328
<!-- solution:end -->
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class Solution {
2+
func nthUglyNumber(_ n: Int) -> Int {
3+
var vis = Set<Int64>()
4+
var pq = PriorityQueue<Int64>()
5+
let factors: [Int64] = [2, 3, 5]
6+
7+
pq.push(1)
8+
vis.insert(1)
9+
var ans: Int64 = 0
10+
11+
for _ in 0..<n {
12+
ans = pq.pop()!
13+
for factor in factors {
14+
let next = ans * factor
15+
if vis.insert(next).inserted {
16+
pq.push(next)
17+
}
18+
}
19+
}
20+
21+
return Int(ans)
22+
}
23+
}
24+
25+
struct PriorityQueue<T: Comparable> {
26+
private var heap: [T] = []
27+
28+
var isEmpty: Bool {
29+
return heap.isEmpty
30+
}
31+
32+
mutating func push(_ element: T) {
33+
heap.append(element)
34+
heapifyUp(from: heap.count - 1)
35+
}
36+
37+
mutating func pop() -> T? {
38+
guard !heap.isEmpty else {
39+
return nil
40+
}
41+
if heap.count == 1 {
42+
return heap.removeLast()
43+
}
44+
let value = heap[0]
45+
heap[0] = heap.removeLast()
46+
heapifyDown(from: 0)
47+
return value
48+
}
49+
50+
private mutating func heapifyUp(from index: Int) {
51+
var index = index
52+
let element = heap[index]
53+
while index > 0 {
54+
let parentIndex = (index - 1) / 2
55+
if element >= heap[parentIndex] {
56+
break
57+
}
58+
heap[index] = heap[parentIndex]
59+
index = parentIndex
60+
}
61+
heap[index] = element
62+
}
63+
64+
private mutating func heapifyDown(from index: Int) {
65+
var index = index
66+
let element = heap[index]
67+
let count = heap.count
68+
while index < count / 2 {
69+
var childIndex = index * 2 + 1
70+
if childIndex + 1 < count && heap[childIndex + 1] < heap[childIndex] {
71+
childIndex += 1
72+
}
73+
if element <= heap[childIndex] {
74+
break
75+
}
76+
heap[index] = heap[childIndex]
77+
index = childIndex
78+
}
79+
heap[index] = element
80+
}
81+
}

0 commit comments

Comments
 (0)