This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func permute(_ nums: [Int]) -> [[Int]] { | |
| for num in nums { | |
| let index = nums.index(of: num)! | |
| var newNums = nums | |
| newNums.remove(at: index) | |
| dfs(a: [num], b: newNums) | |
| } | |
| return [[1]] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func reverse(_ x: Int) -> Int { | |
| let num = abs(x) | |
| let str = String(String(num).reversed()) | |
| if let int = Int32(str) { | |
| return x > 0 ? Int(int) : 0 - Int(int) | |
| } | |
| return 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func wordPattern(_ pattern: String, _ str: String) -> Bool { | |
| let strArray = Array(str.split(separator: " ")) | |
| let patternArray = Array(pattern) | |
| guard strArray.count == patternArray.count && | |
| Set(pattern).count == Set(strArray).count else { return false } | |
| var mappingDict = [Character: Substring]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func rotate(_ nums: inout [Int], _ k: Int) { | |
| var stepCount = k % nums.count | |
| while stepCount > 0 && !nums.isEmpty { | |
| nums.insert(nums.last!, at: 0) | |
| nums.removeLast() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func rotate(_ nums: inout [Int], _ k: Int) { | |
| var newNums = nums | |
| for index in 0 ..< newNums.count { | |
| newNums[(index + k) % newNums.count] = nums[index] | |
| } | |
| nums = newNums | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func rotate(_ nums: inout [Int], _ k: Int) { | |
| var stepCount = k | |
| while stepCount > 0 && !nums.isEmpty { | |
| nums.insert(nums.last!, at: 0) | |
| nums.removeLast() | |
| stepCount -= 1 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func isPerfectSquare(_ num: Int) -> Bool { | |
| var base = 1 | |
| var tmp = 1 | |
| repeat { | |
| tmp = base * base | |
| base += 1 | |
| }while (tmp < num) | |
| return tmp == num | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func rob(_ nums: [Int]) -> Int { | |
| if (nums.count == 1) | |
| { | |
| return nums[0] | |
| } | |
| var now = 0 | |
| var last = 0 | |
| for i in 0..<nums.count | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func detectCapitalUse(_ word: String) -> Bool { | |
| let isLowerCase = word.lowercased() == word | |
| let isUpperCase = word.uppercased() == word | |
| if isLowerCase || isUpperCase { return true } | |
| var words = Array(word) | |
| let isFirstCapital = String((words.first!)).uppercased() == String(words.first!) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func detectCapitalUse(_ word: String) -> Bool { | |
| var words = Array(word) | |
| let isFirstCapital = String((words.first!)).uppercased() == String(words.first!) | |
| let isLowerCase = word.lowercased() == word | |
| let isUpperCase = word.uppercased() == word | |
| if isLowerCase || isUpperCase { return true } | |