Where it all begins: https://github.com/apple/swift.
- 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. Amortized O(1) time random access could be achieved if we manually slice the string and convert it into an array.
- Two Sum - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N), s=O(N) - Inspired by @naveed.zafar
- Add Two Numbers - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N), s=O(1) - Inspired by @potpie
- Longest Substring Without Repeating Characters - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N), s=O(1) - Inspired by @heiyanbin
- Median of Two Sorted Arrays - Hard - Swift Solution - ObjC Solution - Test Cases - t=O(log(min(m,n))), s=O(1) - Inspired by @MissMary - Be sure to read @MissMary's full explanation on how she achieved logarithmic time complexity with a few nice mathematic tricks.
- Longest Palindromic Substring - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N^2), s=O(1) - Inspired by @hh1985
- ZigZag Conversion - Easy - Swift Solution - Test Cases - t=O(N), s=O(N) - Inspired by @dylan_yu
- Reverse Integer - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @wshaoxuan
- String to Integer (atoi) - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @yuruofeifei
- Palindrome Number - Easy - Swift Solution - Test Cases, t=O(N), s=O(1) - Inspired by @hln9319
- Regular Expression Matching - Hard - Swift Solution - Test Cases - DP, t=O(M*N), s=O(M*N) - Inspired by @xiaohui7
- Container With Most Water - Medium - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @franticguy
- Integer To Roman - Medium - Swift Solution - Test Cases - t=O(1), s=O(1) - Inspired by @flytosky
- Roman To Integer - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @makeittrue
- Longest Common Prefix - Easy - Swift Solution - Test Cases - t=O(N*M), s=O(1) - Inspired by @garysui
- 3Sum - Medium - Swift Solution - Test Cases - t=O(N^2), s=O(N) - Inspired by @peerlessbloom
- 3Sum Closest - Medium - Swift Solution - Test Cases - t=O(N^2), s=O(N) - Inspired by @vaibhavatul47
- Letter Combinations of A Phone Number - Medium - Swift Solution - ObjC Solution - Test Cases - t=(3^N), s=O(3^N) - Inspired by @lirensun
- 4Sum - Medium - Swift Solution - Test Cases - t=O(N^3), s=O(N) - Inspired by @zhaohaoshu
- Remove Nth Node From End of List - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @i
- Valid Parentheses - Easy - Swift Solution - Test Cases - t=O(N), s=O(N) - Inspired by @exodia
- Merge Two Sorted Lists - Easy - Swift Solution - Test Cases - t=O(max(M,N)), s=O(1) - Inspired by @xiaohui7
- Generate Parentheses - Medium - Swift Solution - Test Cases - t=O(N^2), s=O(N^2) - Inspired by @klyc0k
- Merge K Sorted Lists - Hard - Swift Solution - Test Cases - 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
- Swap Nodes in Pairs - Medium - Swift Solution - Test Cases, t=O(N), s=O(1) - Inspired by @mike3
- Reverse Nodes in k-Group - Hard - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @sean hyuntaek and @shpolsky
- Remove Duplicates from Sorted Array - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @liyangguang1988
- Remove Element- Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @daxianji007
- Implement strStr() - Easy - Swift Solution - Test Cases - KMP: t=O(M+N), s=O(N). Brute-force: t=O(M*N), s=O(1) - Brute-force inspired by @shichaotan, KMP inspired by @zhenhai.
- Divide Two Integers - Medium - Swift Solution - Test Cases - t=O((logN)^2), s=O(1) - Inspired by @lucastan & @ngcl
- Substring with Concatenation of All Words - Hard - Swift Solution - Test Cases - 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
- Next Permutation - Medium - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @yuyibestman
- Longest Valid Parentheses - Hard - Swift Solution - Test Cases - One pass, t=O(N), s=O(N) - Inspired by @jerryrcwong
- Search in Rotated Sorted Array - Hard - Swift Solution - Test Cases - t=O(logN), s=O(1) - Inspired by @sean hyuntaek
- Search for a Range - Medium - Swift Solution - Test Cases - t=O(logN), s=O(1)
- Search Insert Position - Medium - Swift Solution - Test Cases - t=O(logN), s=O(1)
- Valid Sudoku - Medium - Swift Solution - Test Cases - t=O(1), s=O(1)
- Sudoku Solver - Hard - Swift Solution - Test Cases - DFS
- Count and Say - Easy - Swift Solution - Test Cases - t=(2^N), s=O(2^N)
- Combination Sum - Medium - Swift Solution - Test Cases - t=O(N*M), s=O(M), where N is the length of the candidates array, M is the value of the target integer.
- Combination Sum II - Medium - Swift Solution - Test Cases - t=O(N*M), s=O(M), where N is the length of the candidates array, M is the value of the target integer.
- First Missing Positive - Hard - Swift Solution - Test Cases - t=O(N), s=O(1)
- Trapping Rain Water - Hard - Swift Solution - Test Cases - t=O(N), s=O(1)
- Multiply Strings - Medium - Swift Solution - Test Cases - t=O(N*M), s=O(N+M)
- Wildcard Matching - Hard - Swift Solution - Test Cases - t=O(N), s=O(1)
- Jump Game II - Hard - Swift Solution - Test Cases - t=O(N), s=O(1)
- Permutations - Medium - Swift Solution - Test Cases - t=O(N!), s=O(N!)
- Permutations II - Hard - Swift Solution - Test Cases - t=O(N!), s=O(N!)
- Rotate Image - Medium - Swift Solution - Test Cases - t=O(N*N), s=O(1)
- Anagrams - Medium - Swift Solution - Test Cases - t=O(N), s=O(N)
- Pow(x, n) - Medium - Swift Solution - Test Cases - t=O(logN), s=O(logN)
- N-Queens - Hard - Swift Solution - Test Cases - t=O(N^2), s=O(N^2)
- N-Queens II - Hard - Swift Solution - Test Cases - t= well it's complicated...
- Maximum Subarray - Medium - Swift Solution - Test Cases - t=O(N), s=O(1)
- Spiral Matrix - Medium - Swift Solution - Test Cases - t=O(N), s=O(N)
- Jump Game - Medium - Swift Solution - Test Cases - t=O(N), s=O(1)
- Merge Intervals - Hard - Swift Solution - Test Cases - t=O(N*logN), s=O(N)
- Insert Interval - Hard - Swift Solution - Test Cases - t=O(N), s=O(N)
- Length of Last Word - Easy - Swift Solution - Test Cases - t=O(N), s=O(1)
- Spiral Matrix II - Medium - Swift Solution - Test Cases - t=O(N^2), s=O(N^2)
- Permutation Sequence - Medium - Swift Solution - Test Cases - t=O(N^2), s=O(N)
- Rotate List - Medium - Swift Solution - Test Cases - t=O(N), s=O(1)
- Unique Paths - Medium - Swift Solution - Test Cases - t=O(min(M, N)), s=O(1)
- Unique Paths II - Medium - Swift Solution - Test Cases - t=O(M*N), s=O(1)
- Minimum Path Sum - Medium - Swift Solution - Test Cases - t=O(M*N), s=O(1)
- Valid Number - Hard - Swift Solution - Test Cases - t=O(N), s=O(1)
- Plus One - Easy - Swift Solution - Test Cases - t=O(N), s=O(1)
- Add Binary - Easy - Swift Solution - Test Cases - t=O(max(M,N)), s=O(max(M,N))
- Text Justification - Hard - Swift Solution - Test Cases - t<=O(N*L), s<=O(N*L), where N is the length of the input words array, L is the required justified length
- Sqrt(x) - Medium - Swift Solution - Test Cases - t=O(logN), s=O(1)
- Climbing Stairs - Easy - Swift Solution - Test Cases - t=O(N), s=O(1)
- Simplify Path - Medium - Swift Solution - Test Cases - t=O(N), s=O(N)
- Edit Distance - Hard - Swift Solution - Test Cases - t=O(M*N), s=O(min(M,N))
- Set Matrix Zeroes - Medium - Swift Solution - Test Cases - t=O(M*N), s=O(1)
- Search a 2D Matrix - Medium - Swift Solution - Test Cases - t=O(logN), s=O(1)
- Sort Colors - Medium - Swift Solution - Test Cases - t=O(N), s=O(1), one pass
- Minimum Window Substring - Hard - Swift Solution - Test Cases - t=O(N), s=O(N)
- Combinations - Medium - Swift Solution - Test Cases - t=O(Combination(n, k)+2^(k-1)), s is complicated.
- Subsets - Medium - Swift Solution - Test Cases - t=O(N*2^N), s=O(n*2^N)
- Word Search - Medium - Swift Solution - Test Cases - t=O(M*N*L), s=O(L), where L is the length of the word.
- Remove Duplicates from Sorted Array II - Medium - Swift Solution - Test Cases - t=O(N), s=O(1)
- Search in Rotated Sorted Array II - Medium - Swift Solution - Test Cases - average t=O(logN), worst case t=O(N), s=O(1)
- Remove Duplicates from Sorted List II - Medium - Swift Solution - Test Cases - t=O(N), s=O(1)
- Remove Duplicates from Sorted List - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @Tao2014
- Largest Rectangle in Histogram - Hard - Swift Solution - Test Cases - t=O(N), s=O(N) - Inspired by geeksforgeeks
- Maximal Rectangle - Hard - Swift Solution - Test Cases - t=O(M*N), s=O(min(M,N)) - Inspired by @morrischen2008
- Partition List - Medium - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @shichaotan
- Scramble String - Hard - Swift Solution - Test Cases - time and space complexity need further investigation - Inspired by @Charles_p and @dtx0
- Merge Sorted Array - Easy - Swift Solution - Test Cases - t=O(N), s=O(1) - Inspired by @leetchunhui
- Gray Code - Medium - Swift Solution - Test Cases - t=O(2^N), s=O(2^N) - Inspired by @jinrf
- Subsets II - Medium - Swift Solution - Test Cases - t=O(2^N), s=O(2^N) - Inspired by @mathsam
- Decode Ways - Medium - Swift Solution - Test Cases - t=O(N), s=O(N) - Inspired by @manky
- Reverse Linked List II - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N), s=O(1), One Pass - Inspired by @ardyadipta
- Restore IP Addresses - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(1), s=O(1) - Inspired by @fiona_mao
- Binary Tree Inorder Traversal - Medium - Swift Solution - ObjC Solution - Test Cases - 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 and @blue_y
- Unique Binary Search Trees II - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N^(N-1)), s=I've no idea - Inspired by @Jayanta
- Unique Binary Search Trees - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N^2), s=O(N) - Inspired by @liaison
- Interleaving String - Hard - Swift Solution - ObjC Solution - Test Cases - t=O(N*M), s=O(N*M) - Inspired by @sherryxmhe
- Validate Binary Search Tree - Medium - Swift Solution - ObjC Solution - Test Cases - t=O(N), average s=O(logN), worst s=O(N) - Inspired by @jakwings
- Recover Binary Search Tree - Hard - Swift Solution - ObjC Solution - Test Cases - t=O(N), s=(1) - Inspired by @siyang3
- Same Tree - Easy - Swift Solution - ObjC Solution - Test Cases - t=O(N), average s=O(logN), worst s=O(N) - Inspired by @JohnWeiGitHub and @scott
- Symmetric Tree - Easy - Swift Solution - ObjC Solution - Test Cases - t=O(N), best s=O(logN), worst s=O(N) - Inspired by @xuanaux
- Binary Tree Level Order Traversal - Swift Solution - Test Cases - t=O(N), best s=O(logN), worst s=O(N) - Inspired by @d40a
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..