-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSumOfSquareNumbers_633.swift
More file actions
48 lines (41 loc) · 981 Bytes
/
SumOfSquareNumbers_633.swift
File metadata and controls
48 lines (41 loc) · 981 Bytes
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
//
// SumOfSquareNumbers_633.swift
// LeetCode_Swift
//
// Created by Huni on 09/11/2017.
// Copyright © 2017 KnighhtJoker. All rights reserved.
//
import Foundation
class SumOfSquareNumbers_633 {
// Time Limit
// func judgeSquareSum(_ c: Int) -> Bool {
//
// if c == 1 {
// return true
// }
//
// for i in 0...(c/2) {
// for j in 0...(c/2) {
// if i * i + j * j == c {
// return true
// }
// }
// }
// return false
// }
func judgeSquareSum(_ c: Int) -> Bool {
var left = 0
var right = Int(sqrt(Double(c)))
while left <= right {
let cur = left * left + right * right
if cur < c {
left += 1
} else if cur > c {
right -= 1
} else {
return true
}
}
return false
}
}