Skip to content

Commit 64f93cf

Browse files
DeeDee
Dee
authored and
Dee
committed
make some conversion
1 parent 79c46da commit 64f93cf

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

Diff for: Kth Largest Element/kthLargest.playground/Contents.swift

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//: Playground - noun: a place where people can play
22

3-
func kthLargest(a: [Int], k: Int) -> Int? {
3+
func kthLargest(_ a: [Int], _ k: Int) -> Int? {
44
let len = a.count
55
if k > 0 && k <= len {
6-
let sorted = a.sort()
6+
let sorted = a.sorted()
77
return sorted[len - k]
88
} else {
99
return nil
@@ -12,23 +12,23 @@ func kthLargest(a: [Int], k: Int) -> Int? {
1212

1313
let a = [5, 1, 3, 2, 7, 6, 4]
1414

15-
kthLargest(a, k: 0)
16-
kthLargest(a, k: 1)
17-
kthLargest(a, k: 2)
18-
kthLargest(a, k: 3)
19-
kthLargest(a, k: 4)
20-
kthLargest(a, k: 5)
21-
kthLargest(a, k: 6)
22-
kthLargest(a, k: 7)
23-
kthLargest(a, k: 8)
15+
kthLargest(a, 0)
16+
kthLargest(a, 1)
17+
kthLargest(a, 2)
18+
kthLargest(a, 3)
19+
kthLargest(a, 4)
20+
kthLargest(a, 5)
21+
kthLargest(a, 6)
22+
kthLargest(a, 7)
23+
kthLargest(a, 8)
2424

2525

2626

2727

2828
import Foundation
2929

3030
/* Returns a random integer in the range min...max, inclusive. */
31-
public func random(min min: Int, max: Int) -> Int {
31+
public func random( min: Int, max: Int) -> Int {
3232
assert(min < max)
3333
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
3434
}
@@ -37,22 +37,22 @@ public func random(min min: Int, max: Int) -> Int {
3737
Swift's swap() doesn't like it if the items you're trying to swap refer to
3838
the same memory location. This little wrapper simply ignores such swaps.
3939
*/
40-
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
40+
public func swap<T>(_ a:inout [T], _ i: Int, _ j: Int) {
4141
if i != j {
4242
swap(&a[i], &a[j])
4343
}
4444
}
4545

46-
public func randomizedSelect<T: Comparable>(array: [T], order k: Int) -> T {
46+
public func randomizedSelect<T: Comparable>(_ array: [T], order k: Int) -> T {
4747
var a = array
4848

49-
func randomPivot<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> T {
49+
func randomPivot<T: Comparable>(_ a: inout[T], _ low: Int, _ high: Int) -> T {
5050
let pivotIndex = random(min: low, max: high)
5151
swap(&a, pivotIndex, high)
5252
return a[high]
5353
}
5454

55-
func randomizedPartition<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> Int {
55+
func randomizedPartition<T: Comparable>(_ a: inout[T], _ low: Int, _ high: Int) -> Int {
5656
let pivot = randomPivot(&a, low, high)
5757
var i = low
5858
for j in low..<high {
@@ -65,7 +65,7 @@ public func randomizedSelect<T: Comparable>(array: [T], order k: Int) -> T {
6565
return i
6666
}
6767

68-
func randomizedSelect<T: Comparable>(inout a: [T], _ low: Int, _ high: Int, _ k: Int) -> T {
68+
func randomizedSelect<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int, _ k: Int) -> T {
6969
if low < high {
7070
let p = randomizedPartition(&a, low, high)
7171
if k == p {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "CF309AABC690F91A443043D5C69EECB0069B5411",
3+
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {
4+
5+
},
6+
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
7+
"CF309AABC690F91A443043D5C69EECB0069B5411" : 9223372036854775807,
8+
"FA0506A44181383605977F2A9C8020B861F7CE04" : 9223372036854775807
9+
},
10+
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "6CCD70DA-EE44-4E31-98F0-6DA8B083772A",
11+
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
12+
"CF309AABC690F91A443043D5C69EECB0069B5411" : "swift-algorithm-club\/",
13+
"FA0506A44181383605977F2A9C8020B861F7CE04" : ""
14+
},
15+
"DVTSourceControlWorkspaceBlueprintNameKey" : "kthLargest",
16+
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
17+
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Kth Largest Element\/kthLargest.playground",
18+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
19+
{
20+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Deeer\/swift-algorithm-club.git",
21+
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
22+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "CF309AABC690F91A443043D5C69EECB0069B5411"
23+
},
24+
{
25+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Deeer\/CuteSticker.git",
26+
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
27+
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "FA0506A44181383605977F2A9C8020B861F7CE04"
28+
}
29+
]
30+
}

Diff for: Kth Largest Element/kthLargest.playground/timeline.xctimeline

-6
This file was deleted.

0 commit comments

Comments
 (0)