题记
最近在用Swift刷LeetCode,顺便熟悉一下Swift基本语法.故以此文已记之.
- 1.基本数据类型转
var a:Int = 10
var b = Double(a)
var str = String(a)
print(b,str)
- 2.For循环
- 2.1 带下标
for i in 0..<100{ print(i) }
- 2.2 不带下标
for _ in 0..<100{ print("hello") }
- 2.1 带下标
- 3.字符串遍历
/// LeetCode 第9题(回文数)
/// - Parameter x: 入参
func isPalindrome(_ x: Int) -> Bool {
if(x<0) {
return false
}
let str = String(x)
let length = str.count
for offset in 0..<length/2{
let preChar = str[str.index(str.startIndex, offsetBy: offset)]
let subCHar = str[str.index(str.startIndex, offsetBy: length-1-offset)]
if(preChar != subCHar){
return false
}
}
return true
}
网友评论