-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contents.swift
51 lines (42 loc) · 1.17 KB
/
Contents.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
// LeetCode: https://leetcode.com/problems/reverse-integer/description/
import Foundation
import XCTest
class Solution {
func reverse(_ x: Int) -> Int {
var temp = abs(x)
var output = 0
while temp > 0 {
output = output * 10 + temp % 10
temp = temp / 10
}
guard output < Int32.max, output > Int32.min else { return 0 }
return output * x.signum()
}
func mySqrt(_ x: Int) -> Int {
return Int(sqrt(Double(x)))
}
}
class Tests: XCTestCase {
let solution = Solution()
func testSample1() {
let input = -123
let expected = -321
XCTAssertEqual(solution.reverse(input), expected)
}
func testSample2() {
let input = 123
let expected = 321
XCTAssertEqual(solution.reverse(input), expected)
}
func testSample3() {
let input = 120
let expected = 21
XCTAssertEqual(solution.reverse(input), expected)
}
func testSample4() {
let input = 1534236469
let expected = 0
XCTAssertEqual(solution.reverse(input), expected)
}
}
Tests.defaultTestSuite.run()