mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 12:20:57 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* File: space_complexity.swift
|
||||
* Created Time: 2023-01-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Function */
|
||||
@discardableResult
|
||||
func function() -> Int {
|
||||
// Perform some operations
|
||||
return 0
|
||||
}
|
||||
|
||||
/* Constant order */
|
||||
func constant(n: Int) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
let a = 0
|
||||
var b = 0
|
||||
let nums = Array(repeating: 0, count: 10000)
|
||||
let node = ListNode(x: 0)
|
||||
// Variables in the loop occupy O(1) space
|
||||
for _ in 0 ..< n {
|
||||
let c = 0
|
||||
}
|
||||
// Functions in the loop occupy O(1) space
|
||||
for _ in 0 ..< n {
|
||||
function()
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear order */
|
||||
func linear(n: Int) {
|
||||
// Array of length n uses O(n) space
|
||||
let nums = Array(repeating: 0, count: n)
|
||||
// A list of length n occupies O(n) space
|
||||
let nodes = (0 ..< n).map { ListNode(x: $0) }
|
||||
// A hash table of length n occupies O(n) space
|
||||
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
|
||||
}
|
||||
|
||||
/* Linear order (recursive implementation) */
|
||||
func linearRecur(n: Int) {
|
||||
print("Recursion n = \(n)")
|
||||
if n == 1 {
|
||||
return
|
||||
}
|
||||
linearRecur(n: n - 1)
|
||||
}
|
||||
|
||||
/* Exponential order */
|
||||
func quadratic(n: Int) {
|
||||
// 2D list uses O(n^2) space
|
||||
let numList = Array(repeating: Array(repeating: 0, count: n), count: n)
|
||||
}
|
||||
|
||||
/* Quadratic order (recursive implementation) */
|
||||
@discardableResult
|
||||
func quadraticRecur(n: Int) -> Int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
// Array nums has length n, n-1, ..., 2, 1
|
||||
let nums = Array(repeating: 0, count: n)
|
||||
print("In recursion n = \(n), nums length = \(nums.count)")
|
||||
return quadraticRecur(n: n - 1)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func buildTree(n: Int) -> TreeNode? {
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
let root = TreeNode(x: 0)
|
||||
root.left = buildTree(n: n - 1)
|
||||
root.right = buildTree(n: n - 1)
|
||||
return root
|
||||
}
|
||||
|
||||
@main
|
||||
enum SpaceComplexity {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let n = 5
|
||||
// Constant order
|
||||
constant(n: n)
|
||||
// Linear order
|
||||
linear(n: n)
|
||||
linearRecur(n: n)
|
||||
// Exponential order
|
||||
quadratic(n: n)
|
||||
quadraticRecur(n: n)
|
||||
// Exponential order
|
||||
let root = buildTree(n: n)
|
||||
PrintUtil.printTree(root: root)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user