make purely pure functions
This commit is contained in:
parent
d7d9603bbc
commit
019f20a2c7
2 changed files with 31 additions and 45 deletions
|
|
@ -1,50 +1,38 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
//
|
enum LeetCode {
|
||||||
// LeetCode.swift
|
static func hammingWeight(_ n: Int) -> Int {
|
||||||
// m323
|
n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1)
|
||||||
//
|
|
||||||
// Created by cediackermann on 18.05.2026.
|
|
||||||
//
|
|
||||||
|
|
||||||
class LeetCode {
|
|
||||||
func hammingWeight(_ n: Int) -> Int {
|
|
||||||
return n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func singleNumber(_ nums: [Int]) -> Int {
|
static func singleNumber(_ nums: [Int]) -> Int {
|
||||||
var result = 0
|
nums.reduce(0, ^)
|
||||||
for num in nums {
|
|
||||||
result ^= num
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsDuplicate(_ nums: [Int]) -> Bool {
|
static func containsDuplicate(_ nums: [Int]) -> Bool {
|
||||||
return nums.count != Set(nums).count
|
nums.count != Set(nums).count
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortedSquares(_ nums: [Int]) -> [Int] {
|
static func sortedSquares(_ nums: [Int]) -> [Int] {
|
||||||
return nums.map { $0 * $0 }.sorted()
|
nums.map { $0 * $0 }.sorted()
|
||||||
}
|
}
|
||||||
|
|
||||||
func productExceptSelf(_ nums: [Int]) -> [Int] {
|
static func productExceptSelf(_ nums: [Int]) -> [Int] {
|
||||||
var result = [Int](repeating: 1, count: nums.count)
|
let n = nums.count
|
||||||
var prefix = [Int](repeating: 1, count: nums.count)
|
var result = [Int](repeating: 1, count: n)
|
||||||
var suffix = [Int](repeating: 1, count: nums.count)
|
|
||||||
var runningProduct = 1
|
var prefix = 1
|
||||||
for i in 0..<nums.count {
|
for i in 0..<n {
|
||||||
prefix[i] = runningProduct
|
result[i] = prefix
|
||||||
runningProduct *= nums[i]
|
prefix *= nums[i]
|
||||||
}
|
}
|
||||||
runningProduct = 1
|
|
||||||
for i in stride(from: nums.count - 1, through: 0, by: -1) {
|
var suffix = 1
|
||||||
suffix[i] = runningProduct
|
for i in stride(from: n - 1, through: 0, by: -1) {
|
||||||
runningProduct *= nums[i]
|
result[i] *= suffix
|
||||||
result[i] = prefix[i] * suffix[i]
|
suffix *= nums[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,7 @@
|
||||||
// Created by cediackermann on 18.05.2026.
|
// Created by cediackermann on 18.05.2026.
|
||||||
//
|
//
|
||||||
|
|
||||||
let code = LeetCode()
|
// print(LeetCode.hammingWeight(2_147_483_645))
|
||||||
|
// print(LeetCode.singleNumber([4, 1, 2, 1, 2]))
|
||||||
// print(code.hammingWeight(2_147_483_645))
|
// print(LeetCode.containsDuplicate([1, 2, 3, 4]))
|
||||||
// print(code.singleNumber([4, 1, 2, 1, 2]))
|
// print(LeetCode.productExceptSelf([-1, 1, 0, -3, 3]))
|
||||||
// print(code.containsDuplicate([1, 2, 3, 4]))
|
|
||||||
// print(code.productExceptSelf([-1, 1, 0, -3, 3]))
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue