Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/diwu/LeetCode-Solutions-in-Swift

LeetCode Solutions in Swift 5
https://github.com/diwu/LeetCode-Solutions-in-Swift

Last synced: 3 months ago
JSON representation

LeetCode Solutions in Swift 5

Awesome Lists containing this project

README

        


![logo](https://github.com/diwu/ui-markdown-store/blob/master/swift_sample.png?raw=true)

Where it all begins: [https://github.com/apple/swift](https://github.com/apple/swift).

#### LeetCode Solutions in Swift 5
* Designed for your next job interview in Swift.
* Optimal solutions handpicked from the LeetCode community.
* Best time/space complexity guaranteed. (Think about **O(1) space** in Binary Tree Inorder Traversal. It's that good.)
* Written with Swift-specific language features in mind. (**guard**/**defer**, **map**/**filter**/**reduce**, **optionality**, **inout/@noescape**, **characters as extended grapheme clusters**, etc)
* Comprehensive test cases guarding against wrong answers and timeouts.
* A work in progress. Currently at **102** with **665** test cases.

Please note: Subscript-based O(1) time random access to characters inside a string is not supported by Swift, due to [the way Swift strings are stored](http://oleb.net/blog/2014/07/swift-strings/). Amortized O(1) time random access could be achieved if we manually slice the string and convert it into an array.

##### To run all test cases, launch the project and hit ⌘ + U.

##### Requires Xcode 12.4 (12D4e) or later.

1. [Two Sum](https://oj.leetcode.com/problems/two-sum/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_001_Two_Sum.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_001_Two_Sum.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_001_Two_Sum_Test.swift) - t=O(N), s=O(N) - Inspired by [@naveed.zafar](https://leetcode.com/discuss/10947/accepted-c-o-n-solution)
2. [Add Two Numbers](https://oj.leetcode.com/problems/add-two-numbers/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_002_Add_Two_Numbers.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_002_Add_Two_Numbers.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_002_Add_Two_Numbers_Test.swift) - t=O(N), s=O(1) - Inspired by [@potpie](https://leetcode.com/discuss/2308/is-this-algorithm-optimal-or-what)
3. [Longest Substring Without Repeating Characters ](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_003_Longest_Substring_Without_Repeating_Characters.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_003_Longest_Substring_Without_Repeating_Characters.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_003_Longest_Substring_Without_Repeating_Characters_Test.swift) - t=O(N), s=O(1) - Inspired by [@heiyanbin](https://leetcode.com/discuss/6168/my-o-n-solution)
4. [Median of Two Sorted Arrays](https://oj.leetcode.com/problems/median-of-two-sorted-arrays/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_004_Median_Of_Two_Sorted_Arrays.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Hard/ObjC_Hard_004_Median_Of_Two_Sorted_Arrays.m) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_004_Median_Of_Two_Sorted_Arrays_Test.swift) - t=O(log(min(m,n))), s=O(1) - Inspired by [@MissMary](https://leetcode.com/discuss/15790/share-my-o-log-min-m-n-solution-with-explanation) - Be sure to read [@MissMary](https://leetcode.com/discuss/15790/share-my-o-log-min-m-n-solution-with-explanation)'s [full explanation](https://leetcode.com/discuss/15790/share-my-o-log-min-m-n-solution-with-explanation) on how she achieved logarithmic time complexity with a few nice mathematic tricks.
5. [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_005_Longest_Palindromic_Substring.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_005_Longest_Palindromic_Substring.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_005_Longest_Palindromic_Substring_Test.swift) - t=O(N^2), s=O(1) - Inspired by [@hh1985](https://leetcode.com/discuss/32204/simple-c-solution-8ms-13-lines)
6. [ZigZag Conversion](https://oj.leetcode.com/problems/zigzag-conversion/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_006_ZigZag_Conversion.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_006_ZigZag_Conversion_Test.swift) - t=O(N), s=O(N) - Inspired by [@dylan_yu](https://leetcode.com/discuss/10493/easy-to-understand-java-solution)
7. [Reverse Integer ](https://oj.leetcode.com/problems/reverse-integer/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_007_Reverse_Integer.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_007_Reverse_Integer_Test.swift) - t=O(N), s=O(1) - Inspired by [@wshaoxuan](https://oj.leetcode.com/discuss/120/how-do-we-handle-the-overflow-case)
8. [String to Integer (atoi)](https://oj.leetcode.com/problems/string-to-integer-atoi/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_008_String_to_Integer_atoi.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_008_String_to_Integer_atoi_Test.swift) - t=O(N), s=O(1) - Inspired by [@yuruofeifei](https://leetcode.com/discuss/8886/my-simple-solution)
9. [Palindrome Number](https://oj.leetcode.com/problems/palindrome-number/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_009_Palindrome_Number.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_009_Palindrome_Number_Test.swift), t=O(N), s=O(1) - Inspired by [@hln9319](https://leetcode.com/discuss/12693/neat-ac-java-code-o-n-time-complexity)
10. [Regular Expression Matching](https://oj.leetcode.com/problems/regular-expression-matching/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_010_Regular_Expression_Matching.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_010_Regular_Expression_Matching_Test.swift) - DP, t=O(M\*N), s=O(M\*N) - Inspired by [@xiaohui7](https://leetcode.com/discuss/18970/concise-recursive-and-dp-solutions-with-full-explanation-in)
11. [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_011_Container_With_Most_Water.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_011_Container_With_Most_Water_Test.swift) - t=O(N), s=O(1) - Inspired by [@franticguy](https://leetcode.com/discuss/14610/very-simple-o-n-solution)
12. [Integer To Roman](https://leetcode.com/problems/integer-to-roman/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_012_Integer_To_Roman.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_012_Integer_To_Roman_Test.swift) - t=O(1), s=O(1) - Inspired by [@flytosky](https://leetcode.com/discuss/1208/how-to-improve-code)
13. [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_013_Roman_To_Integer.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_013_Roman_To_Integer_Test.swift) - t=O(N), s=O(1) - Inspired by [@makeittrue](https://leetcode.com/discuss/2369/solution-for-this-question-but-dont-know-there-any-easier-way)
14. [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_014_Longest_Common_Prefix.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_014_Longest_Common_Prefix_Test.swift) - t=O(N\*M), s=O(1) - Inspired by [@garysui](https://leetcode.com/discuss/15755/optimal-solution)
15. [3Sum](https://leetcode.com/problems/3sum/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_015_3Sum.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_015_3Sum_Test.swift) - t=O(N^2), s=O(N) - Inspired by [@peerlessbloom](https://leetcode.com/discuss/10756/my-accepted-o-n-2-solution-without-hashmap)
16. [3Sum Closest](https://leetcode.com/problems/3sum-closest/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_016_3Sum_Closest.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_016_3Sum_Closest_Test.swift) - t=O(N^2), s=O(N) - Inspired by [@vaibhavatul47](https://leetcode.com/discuss/6438/a-n-2-solution-can-we-do-better)
17. [Letter Combinations of A Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_017_Letter_Combinations_Of_A_Phone_Number.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_017_Letter_Combinations_Of_A_Phone_Number.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_017_Letter_Combinations_Of_A_Phone_Number_Test.swift) - t=(3^N), s=O(3^N) - Inspired by [@lirensun](https://leetcode.com/discuss/24431/my-java-solution-with-fifo-queue)
18. [4Sum](https://leetcode.com/problems/4sum/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_018_4Sum.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_018_4Sum_Test.swift) - t=O(N^3), s=O(N) - Inspired by [@zhaohaoshu](https://leetcode.com/discuss/9141/any-better-solution-than-o-n-3)
19. [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_019_Remove_Nth_Node_From_End_Of_List.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_019_Remove_Nth_Node_From_End_Of_List_Test.swift) - t=O(N), s=O(1) - Inspired by [@i](https://leetcode.com/discuss/1656/is-there-a-solution-with-one-pass-and-o-1-space)
20. [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_020_Valid_Parentheses.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_020_Valid_Parentheses_Test.swift) - t=O(N), s=O(N) - Inspired by [@exodia](https://leetcode.com/discuss/21440/sharing-my-simple-cpp-code-with-2ms)
21. [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_021_Merge_Two_Sorted_Lists.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_021_Merge_Two_Sorted_Lists_Test.swift) - t=O(max(M,N)), s=O(1) - Inspired by [@xiaohui7](https://leetcode.com/discuss/18986/14-line-clean-c-solution)
22. [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_022_Generate_Parentheses.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_022_Generate_Parentheses_Test.swift) - t=O(N^2), s=O(N^2) - Inspired by [@klyc0k](https://leetcode.com/discuss/14436/concise-recursive-c-solution)
23. [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_023_Merge_K_Sorted_Lists.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_023_Merge_K_Sorted_Lists_Test.swift) - t=O(N\*logK), s=O(logK), where where N is the total number of elements, K is the number of lists - Inspired by [@wksora](https://leetcode.com/discuss/9279/a-java-solution-based-on-priority-queue)
24. [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_024_Swap_Nodes_In_Pairs.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_024_Swap_Nodes_In_Pairs_Test.swift), t=O(N), s=O(1) - Inspired by [@mike3](https://leetcode.com/discuss/3608/seeking-for-a-better-solution)
25. [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_025_Reverse_Nodes_In_K_Group.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_025_Reverse_Nodes_In_K_Group_Test.swift) - t=O(N), s=O(1) - Inspired by [@sean hyuntaek](https://leetcode.com/discuss/6113/my-solution-accepted-in-java) and [@shpolsky](https://leetcode.com/discuss/21301/short-but-recursive-java-code-with-comments)
26. [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_026_Remove_Duplicates_from_Sorted_Array.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_026_Remove_Duplicates_from_Sorted_Array_Test.swift) - t=O(N), s=O(1) - Inspired by [@liyangguang1988](https://leetcode.com/discuss/10314/my-solution-time-o-n-space-o-1)
27. [Remove Element](https://leetcode.com/problems/remove-element/)- Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_027_Remove_Element.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_027_Remove_Element_Test.swift) - t=O(N), s=O(1) - Inspired by [@daxianji007](https://leetcode.com/discuss/3753/my-solution-for-your-reference)
28. [Implement strStr()](https://leetcode.com/problems/implement-strstr/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_028_Implement_StrStr.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_028_Implement_StrStr_Test.swift) - KMP: t=O(M+N), s=O(N). Brute-force: t=O(M\*N), s=O(1) - Brute-force inspired by [@shichaotan](https://leetcode.com/discuss/19962/a-very-clean-solution-brute-force), KMP inspired by [@zhenhai](https://leetcode.com/discuss/11814/accepted-kmp-solution-in-java-for-reference).
29. [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_029_Divide_Two_Integers.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_029_Divide_Two_Integers_Test.swift) - t=O((logN)^2), s=O(1) - Inspired by [@lucastan & @ngcl](https://leetcode.com/discuss/11358/simple-o-log-n-2-c-solution)
30. [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_030_Substring_With_Concatenation_Of_All_Words.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_030_Substring_With_Concatenation_Of_All_Words_Test.swift) - t=O(N\*W), s=O(L\*W), where N is the length of the string, W is the length of a word, L is the length of the words list. - Inspired by [@ramakanthd92](https://leetcode.com/discuss/366/better-solution-than-brute-force)
31. [Next Permutation](https://leetcode.com/problems/next-permutation/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_031_Next_Permutation.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_031_Next_Permutation_Test.swift) - t=O(N), s=O(1) - Inspired by [@yuyibestman](https://leetcode.com/discuss/8472/share-my-o-n-time-solution)
32. [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_032_Longest_Valid_Parentheses.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_032_Longest_Valid_Parentheses_Test.swift) - One pass, t=O(N), s=O(N) - Inspired by [@jerryrcwong](https://leetcode.com/discuss/8092/my-dp-o-n-solution-without-using-stack)
33. [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_033_Search_In_Rotated_Sorted_Array.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_033_Search_In_Rotated_Sorted_Array_Test.swift) - t=O(logN), s=O(1) - Inspired by [@sean hyuntaek](https://leetcode.com/discuss/5707/algorithm-using-binary-search-accepted-some-suggestions-else)
34. [Search for a Range](https://leetcode.com/problems/search-for-a-range/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_034_Search_For_A_Range.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_034_Search_For_A_Range_Test.swift) - t=O(logN), s=O(1)
35. [Search Insert Position](https://leetcode.com/problems/search-insert-position/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_035_Search_Insert_Position.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_035_Search_Insert_Position_Test.swift) - t=O(logN), s=O(1)
36. [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) - Medium - [Swift Solution](./Solutions/Solutions/Easy/Easy_036_Valid_Sudoku.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_036_Valid_Sudoku_Test.swift) - t=O(1), s=O(1)
37. [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_037_Sudoku_Solver.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_037_Sudoku_Solver_Test.swift) - DFS
38. [Count and Say](https://leetcode.com/problems/count-and-say/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_038_Count_And_Say.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_038_Count_And_Say_Test.swift) - t=(2^N), s=O(2^N)
39. [Combination Sum](https://leetcode.com/problems/combination-sum/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_039_Combination_Sum.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_039_Combination_Sum_Test.swift) - t=O(N*M), s=O(M), where N is the length of the candidates array, M is the value of the target integer.
40. [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_040_Combination_Sum_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_040_Combination_Sum_II_Test.swift) - t=O(N*M), s=O(M), where N is the length of the candidates array, M is the value of the target integer.
41. [First Missing Positive](https://leetcode.com/problems/first-missing-positive/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_041_First_Missing_Positive.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_041_First_Missing_Positive_Test.swift) - t=O(N), s=O(1)
42. [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_042_Trapping_Rain_Water.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_042_Trapping_Rain_Water_Test.swift) - t=O(N), s=O(1)
43. [Multiply Strings](https://leetcode.com/problems/multiply-strings/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_043_Multiply_Strings.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_043_Multiply_Strings_Test.swift) - t=O(N\*M), s=O(N+M)
44. [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_044_Wildcard_Matching.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_044_Wildcard_Matching_Test.swift) - t=O(N), s=O(1)
45. [Jump Game II](https://leetcode.com/problems/jump-game-ii/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_045_Jump_Game_II.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_045_Jump_Game_II_Test.swift) - t=O(N), s=O(1)
46. [Permutations](https://leetcode.com/problems/permutations/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_046_Permutations.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_046_Permutations_Test.swift) - t=O(N!), s=O(N!)
47. [Permutations II](https://leetcode.com/problems/permutations-ii/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_047_Permutations_II.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_047_Permutations_II_Test.swift) - t=O(N!), s=O(N!)
48. [Rotate Image](https://leetcode.com/problems/rotate-image/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_048_Rotate_Image.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_048_Rotate_Image_Test.swift) - t=O(N\*N), s=O(1)
49. [Anagrams](https://leetcode.com/problems/anagrams/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_049_Anagrams.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_049_Anagrams_Test.swift) - t=O(N), s=O(N)
50. [Pow(x, n)](https://leetcode.com/problems/powx-n/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_050_Pow_X_N.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_050_Pow_X_N_Test.swift) - t=O(logN), s=O(logN)
51. [N-Queens](https://leetcode.com/problems/n-queens/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_051_N_Queens.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_051_N_Queens_Test.swift) - t=O(N^2), s=O(N^2)
52. [N-Queens II](https://leetcode.com/problems/n-queens-ii/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_052_N_Queens_II.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_052_N_Queens_II_Test.swift) - t= well it's complicated...
53. [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_053_Maximum_Subarray.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_053_Maximum_Subarray_Test.swift) - t=O(N), s=O(1)
54. [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_054_Spiral_Matrix.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_054_Spiral_Matrix_Test.swift) - t=O(N), s=O(N)
55. [Jump Game](https://leetcode.com/problems/jump-game/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_055_Jump_Game.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_055_Jump_Game_Test.swift) - t=O(N), s=O(1)
56. [Merge Intervals](https://leetcode.com/problems/merge-intervals/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_056_Merge_Intervals.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_056_Merge_Intervals_Test.swift) - t=O(N\*logN), s=O(N)
57. [Insert Interval](https://leetcode.com/discuss/3971/in-place-solution-ask-for-suggestion) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_057_Insert_Interval.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_057_Insert_Interval_Test.swift) - t=O(N), s=O(N)
58. [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_058_Length_Of_Last_Word.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_058_Length_Of_Last_Word_Test.swift) - t=O(N), s=O(1)
59. [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_059_Spiral_Matrix_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_059_Spiral_Matrix_II_Test.swift) - t=O(N^2), s=O(N^2)
60. [Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_060_Permutation_Sequence.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_060_Permutation_Sequence_Test.swift) - t=O(N^2), s=O(N)
61. [Rotate List](https://leetcode.com/problems/rotate-list/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_061_Rotate_List.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_061_Rotate_List_Test.swift) - t=O(N), s=O(1)
62. [Unique Paths](https://leetcode.com/problems/unique-paths/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_062_Unique_Paths.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_062_Unique_Paths_Test.swift) - t=O(min(M, N)), s=O(1)
63. [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_063_Unique_Paths_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_063_Unique_Paths_II_Test.swift) - t=O(M\*N), s=O(1)
64. [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_064_Minimum_Path_Sum.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_064_Minimum_Path_Sum_Test.swift) - t=O(M\*N), s=O(1)
65. [Valid Number](https://leetcode.com/problems/valid-number/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_065_Valid_Number.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_065_Valid_Number_Test.swift) - t=O(N), s=O(1)
66. [Plus One](https://leetcode.com/problems/plus-one/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_066_Plus_One.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_066_Plus_One_Test.swift) - t=O(N), s=O(1)
67. [Add Binary](https://leetcode.com/problems/add-binary/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_067_Add_Binary.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_067_Add_Binary_Test.swift) - t=O(max(M,N)), s=O(max(M,N))
68. [Text Justification](https://leetcode.com/problems/text-justification/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_068_Text_Justification.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_068_Text_Justification_Test.swift) - t<=O(N\*L), s<=O(N\*L), where N is the length of the input words array, L is the required justified length
69. [Sqrt(x)](https://leetcode.com/problems/sqrtx/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_069_Sqrt_X.swift) - [Test Cases](https://github.com/diwu/LeetCode-Solutions-in-Swift/blob/master/Solutions/SolutionsTests/Medium/Medium_069_Sqrt_X_Test.swift) - t=O(logN), s=O(1)
70. [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_070_Climbing_Stairs.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_070_Climbing_Stairs_Test.swift) - t=O(N), s=O(1)
71. [Simplify Path](https://leetcode.com/problems/simplify-path/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_071_Simplify_Path.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_071_Simplify_Path_Test.swift) - t=O(N), s=O(N)
72. [Edit Distance](https://leetcode.com/problems/edit-distance/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_072_Edit_Distance.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_072_Edit_Distance_Test.swift) - t=O(M\*N), s=O(min(M,N))
73. [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_073_Set_Matrix_Zeroes.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_073_Set_Matrix_Zeroes_Test.swift) - t=O(M\*N), s=O(1)
74. [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_074_Search_A_2D_Matrix.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_074_Search_A_2D_Matrix_Test.swift) - t=O(logN), s=O(1)
75. [Sort Colors](https://leetcode.com/problems/sort-colors/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_075_Sort_Colors.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_075_Sort_Colors_Test.swift) - t=O(N), s=O(1), one pass
76. [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_076_Minimum_Window_Substring.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_076_Minimum_Window_Substring_Test.swift) - t=O(N), s=O(N)
77. [Combinations](https://leetcode.com/problems/combinations/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_077_Combinations.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_077_Combinations_Test.swift) - t=O(Combination(n, k)+2^(k-1)), s is complicated.
78. [Subsets](https://leetcode.com/problems/subsets/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_078_Subsets.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_078_Subsets_Test.swift) - t=O(N\*2^N), s=O(n\*2^N)
79. [Word Search](https://leetcode.com/problems/word-search/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_079_Word_Search.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_079_Word_Search_Test.swift) - t=O(M\*N\*L), s=O(L), where L is the length of the word.
80. [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_080_Remove_Duplicates_From_Sorted_Array_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_080_Remove_Duplicates_From_Sorted_Array_II_Test.swift) - t=O(N), s=O(1)
81. [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_081_Search_In_Rotated_Sorted_Array_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_081_Search_In_Rotated_Sorted_Array_II_Test.swift) - average t=O(logN), worst case t=O(N), s=O(1)
82. [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_082_Remove_Duplicates_From_Sorted_List_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_082_Remove_Duplicates_From_Sorted_List_II_Test.swift) - t=O(N), s=O(1)
83. [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_083_Remove_Duplicates_From_Sorted_List.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_083_Remove_Duplicates_From_Sorted_List_Test.swift) - t=O(N), s=O(1) - Inspired by [@Tao2014](https://leetcode.com/discuss/7188/concise-solution-and-memory-freeing)
84. [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_084_Largest_Rectangle_In_Histogram.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_084_Largest_Rectangle_In_Histogram_Test.swift) - t=O(N), s=O(N) - Inspired by [geeksforgeeks](http://www.geeksforgeeks.org/largest-rectangle-under-histogram/)
85. [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_085_Maximal_Rectangle.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_085_Maximal_Rectangle_Test.swift) - t=O(M\*N), s=O(min(M,N)) - Inspired by [@morrischen2008](https://leetcode.com/discuss/20240/share-my-dp-solution)
86. [Partition List](https://leetcode.com/problems/partition-list/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_086_Partition_List.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_086_Partition_List_Test.swift) - t=O(N), s=O(1) - Inspired by [@shichaotan](https://leetcode.com/discuss/21032/very-concise-one-pass-solution)
87. [Scramble String](https://leetcode.com/problems/scramble-string/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_087_Scramble_String.swift) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_087_Scramble_String_Test.swift) - time and space complexity need further investigation - Inspired by [@Charles_p](https://leetcode.com/discuss/3632/any-better-solution) and [@dtx0](https://leetcode.com/discuss/3632/any-better-solution)
88. [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_088_Merge_Sorted_Array.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_088_Merge_Sorted_Array_Test.swift) - t=O(N), s=O(1) - Inspired by [@leetchunhui](https://leetcode.com/discuss/8233/this-is-my-ac-code-may-help-you)
89. [Gray Code](https://leetcode.com/problems/gray-code/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_089_Gray_Code.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_089_Gray_Code_Test.swift) - t=O(2^N), s=O(2^N) - Inspired by [@jinrf](https://discuss.leetcode.com/topic/8557/an-accepted-three-line-solution-in-java)
90. [Subsets II](https://leetcode.com/problems/subsets-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_090_Subsets_II.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_090_Subsets_II_Test.swift) - t=O(2^N), s=O(2^N) - Inspired by [@mathsam](https://leetcode.com/discuss/14902/c-solution-and-explanation)
91. [Decode Ways](https://leetcode.com/problems/decode-ways/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_091_Decode_Ways.swift) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_091_Decode_Ways_Test.swift) - t=O(N), s=O(N) - Inspired by [@manky](https://leetcode.com/discuss/8527/dp-solution-java-for-reference)
92. [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_092_Reverse_Linked_List_II.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_092_Reverse_Linked_List_II.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_092_Reverse_Linked_List_II_Test.swift) - t=O(N), s=O(1), One Pass - Inspired by [@ardyadipta](https://leetcode.com/discuss/25580/simple-java-solution-with-clear-explanation)
93. [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_093_Restore_IP_Addresses.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_093_Restore_IP_Addresses.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_093_Restore_IP_Addresses_Test.swift) - t=O(1), s=O(1) - Inspired by [@fiona_mao](https://leetcode.com/discuss/12790/my-code-in-java)
94. [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_094_Binary_Tree_Inorder_Traversal.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_094_Binary_Tree_Inorder_Traversal.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_094_Binary_Tree_Inorder_Traversal_Test.swift) - Recursion and Iteration t=O(N), average s=O(log(N)), worst s=O(N) - Morris t=O(N), both average & worst s=O(1) - The way we achieved s=O(1) is pretty impressive. Be sure to read the Morris algorithm - Inspired by [@lvlolitte](https://leetcode.com/discuss/19765/iterative-solution-in-java-simple-and-readable) and [@blue_y](https://leetcode.com/discuss/11295/morris-traversal-no-recursion-no-stack)
95. [Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_095_Unique_Binary_Search_Trees_II.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_095_Unique_Binary_Search_Trees_II.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_095_Unique_Binary_Search_Trees_II_Test.swift) - t=O(N^(N-1)), s=I've no idea - Inspired by [@Jayanta](https://leetcode.com/discuss/10254/a-simple-recursive-solution)
96. [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_096_Unique_Binary_Search_Trees.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_096_Unique_Binary_Search_Trees.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_096_Unique_Binary_Search_Trees_Test.swift) - t=O(N^2), s=O(N) - Inspired by [@liaison](https://leetcode.com/discuss/24282/dp-solution-in-6-lines-with-explanation-f-i-n-g-i-1-g-n-i)
97. [Interleaving String](https://leetcode.com/problems/interleaving-string/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_097_Interleaving_String.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Hard/ObjC_Hard_097_Interleaving_String.m) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_097_Interleaving_String_Test.swift) - t=O(N\*M), s=O(N\*M) - Inspired by [@sherryxmhe](https://leetcode.com/discuss/11694/my-dp-solution-in-c)
98. [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) - Medium - [Swift Solution](./Solutions/Solutions/Medium/Medium_098_Validate_Binary_Search_Tree.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Medium/ObjC_Medium_098_Validate_Binary_Search_Tree.m) - [Test Cases](./Solutions/SolutionsTests/Medium/Medium_098_Validate_Binary_Search_Tree_Test.swift) - t=O(N), average s=O(logN), worst s=O(N) - Inspired by [@jakwings](https://leetcode.com/discuss/14886/order-traversal-please-rely-buggy-int_max-int_min-solutions)
99. [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/) - Hard - [Swift Solution](./Solutions/Solutions/Hard/Hard_099_Recover_Binary_Search_Tree.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Hard/ObjC_Hard_099_Recover_Binary_Search_Tree.m) - [Test Cases](./Solutions/SolutionsTests/Hard/Hard_099_Recover_Binary_Search_Tree_Test.swift) - t=O(N), s=(1) - Inspired by [@siyang3](https://leetcode.com/discuss/26310/detail-explain-about-morris-traversal-finds-incorrect-pointer)
100. [Same Tree](https://leetcode.com/problems/same-tree/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_100_Same_Tree.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Easy/ObjC_Easy_100_Same_Tree.m) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_100_Same_Tree_Test.swift) - t=O(N), average s=O(logN), worst s=O(N) - Inspired by [@JohnWeiGitHub](https://leetcode.com/discuss/3470/seeking-for-better-solution) and [@scott](https://leetcode.com/discuss/22197/my-non-recursive-method)
101. [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) - Easy - [Swift Solution](./Solutions/Solutions/Easy/Easy_101_Symmetric_Tree.swift) - [ObjC Solution](./Solutions/Solutions_ObjC/Easy/ObjC_Easy_101_Symmetric_Tree.m) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_101_Symmetric_Tree_Test.swift) - t=O(N), best s=O(logN), worst s=O(N) - Inspired by [@xuanaux](https://leetcode.com/discuss/456/recusive-solution-symmetric-optimal-solution-inordertraversal)
102. [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) - [Swift Solution](./Solutions/Solutions/Easy/Easy_102_Binary_Tree_Level_Order_Traversal.swift) - [Test Cases](./Solutions/SolutionsTests/Easy/Easy_102_Binary_Tree_Level_Order_Traversal_Test.swift) - t=O(N), best s=O(logN), worst s=O(N) - Inspired by [@d40a](https://discuss.leetcode.com/topic/2399/it-is-my-ac-solution)

Optional chaining, closure, subscript, enumeration, generic, extension, access control, automatic reference counting, string, character, nested type, type casting, protocol, xctestcase, xctest, online judge, oj, xcode, cocoa, cocoa touch, foundation, ios, 面试, 算法, 递归, 迭代, 找工作, 手机, 苹果, wwdc, POJ, PKU Online Judge, OJ, data structure, algorithm, algorithms, data structures, 数据结构, 算法与数据结构, interview, interviews, interviewer, interviewers, facebook, google, linkedin, apple, flag, cracking the coding interview, job, recruit, recruiter, recruiting..