Swift.希尔排序

import Foundation class ShellSortSolution { func sort(_ n: [Int]) -> [Int] { var s = n var gap = n.count/2 while gap > 0 { var i = gap while i < n.count { var j = i ...

LeetCode 2015-05-11 PM 373℃ 0条

Swift.圆圈中最后剩下的数字

题目描述:有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数….这样下去….直到剩下最后一个小朋友,求最后一个小朋友的编号import Foundation class For45Solution { func deleteNum(_ n: Int,_ m: Int) -> Int { var nums = [Int]() for i in 0...n { ...

LeetCode 2015-05-06 PM 366℃ 0条

Swift.二维数组中的查找

题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样一个二维数组和整数,判断数组中是否含有该整数。class Solution { func find(_ n: [[Int]],_ i: Int) -> Bool { guard n.count > 0 else { return false } var row = 0 var col = n.first!.count - 1 ...

LeetCode 2015-04-22 PM 703℃ 0条

Swift.替换空格

题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入"We are happy.",则输出"We%20are%20happy."。class Solution { func replace(_ s: String) -> String { guard s.count > 0 else { return s } var nullNum = 0 for c in s { if c == " "...

LeetCode 2015-04-15 PM 526℃ 0条

Swift.冒泡排序

import Foundation class BubbleSortSolution { func sort(_ n: [Int]) -> [Int] { var nums = n for i in 0..<n.count { for j in 0..<n.count-i-1 { if nums[j] > nums[j+1] { let temp = nums[j+1...

LeetCode 2015-04-15 PM 491℃ 0条