Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Week_04/id_31/LeetCode_169_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// LeetCode_169_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/12.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

// 哈希Map
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
guard !nums.isEmpty else { return -1 }
var res: [Int: Int] = [:]
for v in nums {
if var data = res[v] {
data += 1
res[v] = data
} else {
res[v] = 1
}
if res[v]! > nums.count / 2 { return v }
}
return -1
}
}

// 排序
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
guard !nums.isEmpty else { return -1 }
let sorted = nums.sorted{ $0 < $1 }
return sorted[(0+sorted.count - 1) / 2]
}
}

// 分治算法
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
return major(nums, 0, nums.count - 1).value
}

private func major(_ nums: [Int], _ from: Int, _ to: Int) -> (value: Int, count: Int) {
guard from < to else { return (value: nums[from], 1)}
let mid = from + (to - from) / 2
let leftData = major(nums, from, mid)
let rightData = major(nums, mid+1, to)
if leftData.value == rightData.value {
return (value: leftData.value, count: leftData.count + rightData.count)
}


if (leftData.count > rightData.count) {
return (value: leftData.value, count: leftData.count + findNum(leftData.value, Array(nums[mid+1...to])))
} else {
return (value: rightData.value, count: rightData.count + findNum(rightData.value, Array(nums[from...mid])))
}
}

private func findNum(_ value: Int, _ nums: [Int]) -> Int {
var res: Int = 0
_ = nums.map { if ($0 == value) { res += 1 }}
return res
}
}
36 changes: 36 additions & 0 deletions Week_04/id_31/LeetCode_455_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// LeetCode_455_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/12.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

class Solution {
func findContentChildren(_ g: [Int], _ s: [Int]) -> Int {
var res: Int = 0
var sortedG = g.sorted { $0 < $1 }
var sortedS = s.sorted { $0 < $1 }
// 先满足胃口最小的孩子
for i in 0..<sortedG.count {
let gg = sortedG[i]

var sIndex = 0
while sIndex <= sortedS.count - 1 {
let ss = sortedS[sIndex]
if (ss >= gg) {
// 找到满足这个孩子胃口的食物了,并且在食物列表中移除它
sortedS.remove(at: sIndex)
// 记录一下满足孩子的个数
res += 1
// 退出此次循环
break
}
sIndex += 1
}
}
return res
}
}
36 changes: 36 additions & 0 deletions Week_04/id_31/LeetCode_720_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// LeetCode_720_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/11.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

class Solution {
func longestWord(_ words: [String]) -> String {
let sortedWords = Set(words).sorted {
if $0.count == $1.count {
return $0 < $1
} else {
return $0.count > $1.count
}
}
var errorDatas: Set<String> = []
for key in sortedWords {
if (errorDatas.contains(key)) { continue }
for i in 1...key.count {
let subString = String(key[key.startIndex..<key.index(key.startIndex, offsetBy: i)])
if (sortedWords.contains(subString)) {
if i == key.count { return key }
continue
} else {
errorDatas.insert(key)
break
}
}
}
return ""
}
}
47 changes: 47 additions & 0 deletions Week_04/id_31/LeetCode_784_031.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// LeetCode_784_031.swift
// TestCoding
//
// Created by 龚欢 on 2019/5/12.
// Copyright © 2019 龚欢. All rights reserved.
//

import Foundation

class Solution {
var m: [String] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var n: [String] = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var res: Set<String> = []
func letterCasePermutation(_ S: String) -> [String] {
var s = S
_ = find(&s, 0)
return Array(res)
}

private func find(_ S: inout String, _ from: Int) {
if from == S.count {
return
}
guard let offSetStart = S.index(S.startIndex, offsetBy: from, limitedBy: S.endIndex), let offSetEnd = S.index(offSetStart, offsetBy: 1, limitedBy: S.endIndex) else { return }
let findRange = offSetStart..<offSetEnd
let findWord = String(S[findRange])
if m.contains(findWord) || n.contains(findWord) {
// 可以切换大小写
// 1. 不切换大小写
res.insert(S)
find(&S, from + 1)

// 2. 切换大小写
if (m.contains(findWord)) {
S = S.replacingOccurrences(of: findWord, with: n[m.index(of: findWord)!], options:[], range: findRange)
} else if (n.contains(findWord)) {
S = S.replacingOccurrences(of: findWord, with: m[n.index(of: findWord)!], options:[], range: findRange)
}
res.insert(S)
find(&S, from + 1)
} else {
res.insert(S)
find(&S, from + 1)
}
}
}