diff --git a/swift/.gitignore b/swift/.gitignore new file mode 100644 index 0000000..bb460e7 --- /dev/null +++ b/swift/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/swift/Package.swift b/swift/Package.swift new file mode 100644 index 0000000..ff88a01 --- /dev/null +++ b/swift/Package.swift @@ -0,0 +1,22 @@ +// swift-tools-version:5.5 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "my_leetcode", + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages this package depends on. + .executableTarget( + name: "my_leetcode", + dependencies: []), + .testTarget( + name: "my_leetcodeTests", + dependencies: ["my_leetcode"]), + ] +) diff --git a/swift/README.md b/swift/README.md new file mode 100644 index 0000000..260e079 --- /dev/null +++ b/swift/README.md @@ -0,0 +1,3 @@ +# my_leetcode + +A description of this package. diff --git a/swift/Sources/my_leetcode/Solution.swift b/swift/Sources/my_leetcode/Solution.swift new file mode 100644 index 0000000..5ec30c1 --- /dev/null +++ b/swift/Sources/my_leetcode/Solution.swift @@ -0,0 +1,30 @@ +// +// Created by Huang on 2021/9/30. +// + +import Foundation + +class Solution { + + func mySqrt(_ x: Int) -> Int { + let x_half = 0.5 * Double(x) + var i: UInt64 = Double(x).bitPattern + i = 0x5FE6EC85E7DE30DA - (i >> 1) + var f: Double = Double.init(bitPattern: i) + f = f * (1.5 - x_half * f * f) + f = f * (1.5 - x_half * f * f) + f = f * (1.5 - x_half * f * f) + return Int(1.0 / f) + } + + /** + 1486. 数组异或操作 + - Parameters: + - n: 数量 + - start: 起点 + - Returns: 数组异或操作结果 + */ + func xorOperation(_ n: Int, _ start: Int) -> Int { + return (start & 3) < 2 ? ((n & 1) == 0) ? (n & 3) : (start + 2 * n - 3 + (n & 3)) : ((n & 1) == 0) ? ((start + (n - 1) * 2) ^ (start - 2 + (n & 3))) : (start + 1 - (n & 3)) + } +} diff --git a/swift/Sources/my_leetcode/main.swift b/swift/Sources/my_leetcode/main.swift new file mode 100644 index 0000000..f7cf60e --- /dev/null +++ b/swift/Sources/my_leetcode/main.swift @@ -0,0 +1 @@ +print("Hello, world!") diff --git a/swift/Tests/my_leetcodeTests/my_leetcodeTests.swift b/swift/Tests/my_leetcodeTests/my_leetcodeTests.swift new file mode 100644 index 0000000..c420f08 --- /dev/null +++ b/swift/Tests/my_leetcodeTests/my_leetcodeTests.swift @@ -0,0 +1,47 @@ +import XCTest +import class Foundation.Bundle + +final class my_leetcodeTests: XCTestCase { + func testExample() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct + // results. + + // Some of the APIs that we use below are available in macOS 10.13 and above. + guard #available(macOS 10.13, *) else { + return + } + + // Mac Catalyst won't have `Process`, but it is supported for executables. + #if !targetEnvironment(macCatalyst) + + let fooBinary = productsDirectory.appendingPathComponent("my_leetcode") + + let process = Process() + process.executableURL = fooBinary + + let pipe = Pipe() + process.standardOutput = pipe + + try process.run() + process.waitUntilExit() + + let data = pipe.fileHandleForReading.readDataToEndOfFile() + let output = String(data: data, encoding: .utf8) + + XCTAssertEqual(output, "Hello, world!\n") + #endif + } + + /// Returns path to the built products directory. + var productsDirectory: URL { + #if os(macOS) + for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { + return bundle.bundleURL.deletingLastPathComponent() + } + fatalError("couldn't find the products directory") + #else + return Bundle.main.bundleURL + #endif + } +}