This commit is contained in:
游由 2021-09-30 11:16:18 +08:00
parent 7ff51b11ed
commit c3aac03f1a
6 changed files with 110 additions and 0 deletions

7
swift/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

22
swift/Package.swift Normal file
View File

@ -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"]),
]
)

3
swift/README.md Normal file
View File

@ -0,0 +1,3 @@
# my_leetcode
A description of this package.

View File

@ -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))
}
}

View File

@ -0,0 +1 @@
print("Hello, world!")

View File

@ -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
}
}