From 2b135fe46bb7deaa5989a39ab5dbaea83bb43388 Mon Sep 17 00:00:00 2001 From: CRGT_Ghcoder Date: Sat, 11 May 2019 15:36:56 +0800 Subject: [PATCH 1/4] add leetcode 720 --- Week_04/id_31/LeetCode_720_031.swift | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week_04/id_31/LeetCode_720_031.swift diff --git a/Week_04/id_31/LeetCode_720_031.swift b/Week_04/id_31/LeetCode_720_031.swift new file mode 100644 index 00000000..32217224 --- /dev/null +++ b/Week_04/id_31/LeetCode_720_031.swift @@ -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 = [] + for key in sortedWords { + if (errorDatas.contains(key)) { continue } + for i in 1...key.count { + let subString = String(key[key.startIndex.. Date: Sun, 12 May 2019 11:08:06 +0800 Subject: [PATCH 2/4] add leetcode 169 --- Week_04/id_31/LeetCode_169_031.swift | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Week_04/id_31/LeetCode_169_031.swift diff --git a/Week_04/id_31/LeetCode_169_031.swift b/Week_04/id_31/LeetCode_169_031.swift new file mode 100644 index 00000000..11f74d7a --- /dev/null +++ b/Week_04/id_31/LeetCode_169_031.swift @@ -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 + } +} From 8188f35bed9588032e122ac22e2473cf983378fc Mon Sep 17 00:00:00 2001 From: CRGT_Ghcoder Date: Sun, 12 May 2019 12:06:02 +0800 Subject: [PATCH 3/4] add leetcode 455 --- Week_04/id_31/LeetCode_455_031.swift | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week_04/id_31/LeetCode_455_031.swift diff --git a/Week_04/id_31/LeetCode_455_031.swift b/Week_04/id_31/LeetCode_455_031.swift new file mode 100644 index 00000000..c9cccdc4 --- /dev/null +++ b/Week_04/id_31/LeetCode_455_031.swift @@ -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..= gg) { + // 找到满足这个孩子胃口的食物了,并且在食物列表中移除它 + sortedS.remove(at: sIndex) + // 记录一下满足孩子的个数 + res += 1 + // 退出此次循环 + break + } + sIndex += 1 + } + } + return res + } +} From e271ec4c39f55517cd2696b159d1a1bbe9803297 Mon Sep 17 00:00:00 2001 From: CRGT_Ghcoder Date: Sun, 12 May 2019 21:50:53 +0800 Subject: [PATCH 4/4] add leetcode 784 --- Week_04/id_31/LeetCode_784_031.swift | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Week_04/id_31/LeetCode_784_031.swift diff --git a/Week_04/id_31/LeetCode_784_031.swift b/Week_04/id_31/LeetCode_784_031.swift new file mode 100644 index 00000000..96ea6f79 --- /dev/null +++ b/Week_04/id_31/LeetCode_784_031.swift @@ -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 = [] + 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..