mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 12:16:27 +08:00
build
This commit is contained in:
parent
7156a5f832
commit
30ed83e5b1
10 changed files with 130 additions and 49 deletions
|
@ -1517,6 +1517,15 @@ comments: true
|
|||
// 更新列表容量
|
||||
_capacity = nums.count
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
func toArray() -> [Int] {
|
||||
var nums = Array(repeating: 0, count: _size)
|
||||
for i in 0 ..< _size {
|
||||
nums[i] = get(index: i)
|
||||
}
|
||||
return nums
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1342,12 +1342,14 @@ $$
|
|||
|
||||
```swift title="space_complexity.swift"
|
||||
/* 平方阶(递归实现) */
|
||||
@discardableResult
|
||||
func quadraticRecur(n: Int) -> Int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
let nums = Array(repeating: 0, count: n)
|
||||
print("递归 n = \(n) 中的 nums 长度 = \(nums.count)")
|
||||
return quadraticRecur(n: n - 1)
|
||||
}
|
||||
```
|
||||
|
|
|
@ -161,6 +161,7 @@ comments: true
|
|||
=== "Swift"
|
||||
|
||||
```swift title="leetcode_two_sum.swift"
|
||||
/* 方法一:暴力枚举 */
|
||||
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for i in nums.indices.dropLast() {
|
||||
|
@ -344,6 +345,7 @@ comments: true
|
|||
=== "Swift"
|
||||
|
||||
```swift title="leetcode_two_sum.swift"
|
||||
/* 方法二:辅助哈希表 */
|
||||
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
var dic: [Int: Int] = [:]
|
||||
|
|
|
@ -903,7 +903,7 @@ $$
|
|||
/* 常数阶 */
|
||||
func constant(n: Int) -> Int {
|
||||
var count = 0
|
||||
let size = 100000
|
||||
let size = 100_000
|
||||
for _ in 0 ..< size {
|
||||
count += 1
|
||||
}
|
||||
|
|
|
@ -1020,6 +1020,46 @@ $$
|
|||
// 置为 nil ,代表删除
|
||||
bucket[index] = nil
|
||||
}
|
||||
|
||||
/* 获取所有键值对 */
|
||||
func entrySet() -> [Entry] {
|
||||
var entrySet: [Entry] = []
|
||||
for pair in bucket {
|
||||
if let pair = pair {
|
||||
entrySet.append(pair)
|
||||
}
|
||||
}
|
||||
return entrySet
|
||||
}
|
||||
|
||||
/* 获取所有键 */
|
||||
func keySet() -> [Int] {
|
||||
var keySet: [Int] = []
|
||||
for pair in bucket {
|
||||
if let pair = pair {
|
||||
keySet.append(pair.key)
|
||||
}
|
||||
}
|
||||
return keySet
|
||||
}
|
||||
|
||||
/* 获取所有值 */
|
||||
func valueSet() -> [String] {
|
||||
var valueSet: [String] = []
|
||||
for pair in bucket {
|
||||
if let pair = pair {
|
||||
valueSet.append(pair.val)
|
||||
}
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
|
||||
/* 打印哈希表 */
|
||||
func print() {
|
||||
for entry in entrySet() {
|
||||
Swift.print("\(entry.key) -> \(entry.val)")
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -386,13 +386,6 @@ comments: true
|
|||
=== "Swift"
|
||||
|
||||
```swift title="my_heap.swift"
|
||||
var maxHeap: [Int]
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
init() {
|
||||
maxHeap = []
|
||||
}
|
||||
|
||||
/* 获取左子结点索引 */
|
||||
func left(i: Int) -> Int {
|
||||
2 * i + 1
|
||||
|
|
|
@ -141,14 +141,14 @@ comments: true
|
|||
|
||||
```javascript title="quick_sort.js"
|
||||
/* 元素交换 */
|
||||
function swap(nums, i, j) {
|
||||
swap(nums, i, j) {
|
||||
let tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
function partition(nums, left, right) {
|
||||
partition(nums, left, right) {
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
while (i < j) {
|
||||
|
@ -159,9 +159,9 @@ comments: true
|
|||
i += 1; // 从左向右找首个大于基准数的元素
|
||||
}
|
||||
// 元素交换
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
this.swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
```
|
||||
|
@ -170,14 +170,14 @@ comments: true
|
|||
|
||||
```typescript title="quick_sort.ts"
|
||||
/* 元素交换 */
|
||||
function swap(nums: number[], i: number, j: number): void {
|
||||
swap(nums: number[], i: number, j: number): void {
|
||||
let tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* 哨兵划分 */
|
||||
function partition(nums: number[], left: number, right: number): number {
|
||||
partition(nums: number[], left: number, right: number): number {
|
||||
// 以 nums[left] 作为基准数
|
||||
let i = left, j = right;
|
||||
while (i < j) {
|
||||
|
@ -188,9 +188,9 @@ comments: true
|
|||
i += 1; // 从左向右找首个大于基准数的元素
|
||||
}
|
||||
// 元素交换
|
||||
swap(nums, i, j); // 交换这两个元素
|
||||
this.swap(nums, i, j); // 交换这两个元素
|
||||
}
|
||||
swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
this.swap(nums, i, left); // 将基准数交换至两子数组的分界线
|
||||
return i; // 返回基准数的索引
|
||||
}
|
||||
```
|
||||
|
@ -698,13 +698,12 @@ comments: true
|
|||
}
|
||||
|
||||
/* 哨兵划分(三数取中值) */
|
||||
func partition(nums: inout [Int], left: Int, right: Int) -> Int {
|
||||
func partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {
|
||||
// 选取三个候选元素的中位数
|
||||
let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)
|
||||
// 将中位数交换至数组最左端
|
||||
swap(nums: &nums, i: left, j: med)
|
||||
// 以 nums[left] 作为基准数
|
||||
// 下同省略...
|
||||
return partition(nums: &nums, left: left, right: right)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -879,7 +878,7 @@ comments: true
|
|||
|
||||
```swift title="quick_sort.swift"
|
||||
/* 快速排序(尾递归优化) */
|
||||
func quickSort(nums: inout [Int], left: Int, right: Int) {
|
||||
func quickSortTailCall(nums: inout [Int], left: Int, right: Int) {
|
||||
var left = left
|
||||
var right = right
|
||||
// 子数组长度为 1 时终止
|
||||
|
@ -888,10 +887,10 @@ comments: true
|
|||
let pivot = partition(nums: &nums, left: left, right: right)
|
||||
// 对两个子数组中较短的那个执行快排
|
||||
if (pivot - left) < (right - pivot) {
|
||||
quickSort(nums: &nums, left: left, right: pivot - 1) // 递归排序左子数组
|
||||
quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // 递归排序左子数组
|
||||
left = pivot + 1 // 剩余待排序区间为 [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums: &nums, left: pivot + 1, right: right) // 递归排序右子数组
|
||||
quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // 递归排序右子数组
|
||||
right = pivot - 1 // 剩余待排序区间为 [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -798,6 +798,17 @@ comments: true
|
|||
}
|
||||
return front!.val
|
||||
}
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
func toArray() -> [Int] {
|
||||
var node = front
|
||||
var res = Array(repeating: 0, count: size())
|
||||
for i in res.indices {
|
||||
res[i] = node!.val
|
||||
node = node?.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1380,6 +1391,16 @@ comments: true
|
|||
}
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
func toArray() -> [Int] {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
var res = Array(repeating: 0, count: queSize)
|
||||
for (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {
|
||||
res[i] = nums[j % capacity()]
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -724,6 +724,17 @@ comments: true
|
|||
}
|
||||
return _peek!.val
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
func toArray() -> [Int] {
|
||||
var node = _peek
|
||||
var res = Array(repeating: 0, count: _size)
|
||||
for i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {
|
||||
res[i] = node!.val
|
||||
node = node?.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1113,6 +1124,11 @@ comments: true
|
|||
}
|
||||
return stack.last!
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
func toArray() -> [Int] {
|
||||
stack
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -773,7 +773,6 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
|||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
|
Loading…
Reference in a new issue