Skip to content

Commit dc3d90b

Browse files
Merge pull request algorithm001#660 from glve1027/master
Week_04 031
2 parents 6bc3b1e + e271ec4 commit dc3d90b

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

Week_04/id_31/LeetCode_169_031.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// LeetCode_169_031.swift
3+
// TestCoding
4+
//
5+
// Created by 龚欢 on 2019/5/12.
6+
// Copyright © 2019 龚欢. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
// 哈希Map
12+
class Solution {
13+
func majorityElement(_ nums: [Int]) -> Int {
14+
guard !nums.isEmpty else { return -1 }
15+
var res: [Int: Int] = [:]
16+
for v in nums {
17+
if var data = res[v] {
18+
data += 1
19+
res[v] = data
20+
} else {
21+
res[v] = 1
22+
}
23+
if res[v]! > nums.count / 2 { return v }
24+
}
25+
return -1
26+
}
27+
}
28+
29+
// 排序
30+
class Solution {
31+
func majorityElement(_ nums: [Int]) -> Int {
32+
guard !nums.isEmpty else { return -1 }
33+
let sorted = nums.sorted{ $0 < $1 }
34+
return sorted[(0+sorted.count - 1) / 2]
35+
}
36+
}
37+
38+
// 分治算法
39+
class Solution {
40+
func majorityElement(_ nums: [Int]) -> Int {
41+
return major(nums, 0, nums.count - 1).value
42+
}
43+
44+
private func major(_ nums: [Int], _ from: Int, _ to: Int) -> (value: Int, count: Int) {
45+
guard from < to else { return (value: nums[from], 1)}
46+
let mid = from + (to - from) / 2
47+
let leftData = major(nums, from, mid)
48+
let rightData = major(nums, mid+1, to)
49+
if leftData.value == rightData.value {
50+
return (value: leftData.value, count: leftData.count + rightData.count)
51+
}
52+
53+
54+
if (leftData.count > rightData.count) {
55+
return (value: leftData.value, count: leftData.count + findNum(leftData.value, Array(nums[mid+1...to])))
56+
} else {
57+
return (value: rightData.value, count: rightData.count + findNum(rightData.value, Array(nums[from...mid])))
58+
}
59+
}
60+
61+
private func findNum(_ value: Int, _ nums: [Int]) -> Int {
62+
var res: Int = 0
63+
_ = nums.map { if ($0 == value) { res += 1 }}
64+
return res
65+
}
66+
}

Week_04/id_31/LeetCode_455_031.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// LeetCode_455_031.swift
3+
// TestCoding
4+
//
5+
// Created by 龚欢 on 2019/5/12.
6+
// Copyright © 2019 龚欢. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class Solution {
12+
func findContentChildren(_ g: [Int], _ s: [Int]) -> Int {
13+
var res: Int = 0
14+
var sortedG = g.sorted { $0 < $1 }
15+
var sortedS = s.sorted { $0 < $1 }
16+
// 先满足胃口最小的孩子
17+
for i in 0..<sortedG.count {
18+
let gg = sortedG[i]
19+
20+
var sIndex = 0
21+
while sIndex <= sortedS.count - 1 {
22+
let ss = sortedS[sIndex]
23+
if (ss >= gg) {
24+
// 找到满足这个孩子胃口的食物了,并且在食物列表中移除它
25+
sortedS.remove(at: sIndex)
26+
// 记录一下满足孩子的个数
27+
res += 1
28+
// 退出此次循环
29+
break
30+
}
31+
sIndex += 1
32+
}
33+
}
34+
return res
35+
}
36+
}

Week_04/id_31/LeetCode_720_031.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// LeetCode_720_031.swift
3+
// TestCoding
4+
//
5+
// Created by 龚欢 on 2019/5/11.
6+
// Copyright © 2019 龚欢. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class Solution {
12+
func longestWord(_ words: [String]) -> String {
13+
let sortedWords = Set(words).sorted {
14+
if $0.count == $1.count {
15+
return $0 < $1
16+
} else {
17+
return $0.count > $1.count
18+
}
19+
}
20+
var errorDatas: Set<String> = []
21+
for key in sortedWords {
22+
if (errorDatas.contains(key)) { continue }
23+
for i in 1...key.count {
24+
let subString = String(key[key.startIndex..<key.index(key.startIndex, offsetBy: i)])
25+
if (sortedWords.contains(subString)) {
26+
if i == key.count { return key }
27+
continue
28+
} else {
29+
errorDatas.insert(key)
30+
break
31+
}
32+
}
33+
}
34+
return ""
35+
}
36+
}

Week_04/id_31/LeetCode_784_031.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// LeetCode_784_031.swift
3+
// TestCoding
4+
//
5+
// Created by 龚欢 on 2019/5/12.
6+
// Copyright © 2019 龚欢. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class Solution {
12+
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"]
13+
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"]
14+
var res: Set<String> = []
15+
func letterCasePermutation(_ S: String) -> [String] {
16+
var s = S
17+
_ = find(&s, 0)
18+
return Array(res)
19+
}
20+
21+
private func find(_ S: inout String, _ from: Int) {
22+
if from == S.count {
23+
return
24+
}
25+
guard let offSetStart = S.index(S.startIndex, offsetBy: from, limitedBy: S.endIndex), let offSetEnd = S.index(offSetStart, offsetBy: 1, limitedBy: S.endIndex) else { return }
26+
let findRange = offSetStart..<offSetEnd
27+
let findWord = String(S[findRange])
28+
if m.contains(findWord) || n.contains(findWord) {
29+
// 可以切换大小写
30+
// 1. 不切换大小写
31+
res.insert(S)
32+
find(&S, from + 1)
33+
34+
// 2. 切换大小写
35+
if (m.contains(findWord)) {
36+
S = S.replacingOccurrences(of: findWord, with: n[m.index(of: findWord)!], options:[], range: findRange)
37+
} else if (n.contains(findWord)) {
38+
S = S.replacingOccurrences(of: findWord, with: m[n.index(of: findWord)!], options:[], range: findRange)
39+
}
40+
res.insert(S)
41+
find(&S, from + 1)
42+
} else {
43+
res.insert(S)
44+
find(&S, from + 1)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)