-
Notifications
You must be signed in to change notification settings - Fork 41
/
189.swift
63 lines (48 loc) · 1.28 KB
/
189.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// 189.swift
// LeetCode
//
// Created by Lex on 12/30/15.
// Copyright © 2015 Lex Tang. All rights reserved.
//
/*
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
*/
import Foundation
import XCTest
extension Array {
mutating func rotate(_ k: Int) {
let start = self.count - k
if start < 1 {
return
}
var extra = Array<Element>(self[start..<(start + k)])
extra.append(contentsOf: self[0..<(count - k)])
self = extra
}
}
class RotateArray: XCTestCase {
func testRotateArray() {
var a = [1, 2, 3, 4, 5, 6, 7]
a.rotate(3)
XCTAssertEqual(a, [5, 6, 7, 1, 2, 3, 4])
a = [1, 2, 3, 4, 5, 6, 7]
a.rotate(1)
XCTAssertEqual(a, [7, 1, 2, 3, 4, 5, 6])
a = [1]
a.rotate(1)
XCTAssertEqual(a, [1])
a = []
a.rotate(1)
XCTAssertEqual(a, [])
a = [1, 2, 3]
a.rotate(3)
XCTAssertEqual(a, [1, 2, 3])
}
}