Skip to content

wangguangfeng/LeetCode_Swift

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode by Swift

[LeetCode Online Judge] (https://leetcode.com/) is a website containing many algorithm questions. Most of them are real interview questions of Google, Facebook, LinkedIn, Apple, etc. This repo shows my solutions by Swift and the code style strictly follows the RayWenderlich Swift Style Guide. Please feel free to reference and STAR to support this repo, thank you!

Leetcode

Data Structures

Companies

Array

Title Solution Difficulty Time Space
Summary Ranges Swift Medium O(n) O(n)
Intersection of Two Arrays Swift Easy O(n) O(n)
Intersection of Two Arrays II Swift Easy O(nlogn) O(n)
Contains Duplicate Swift Easy O(n) O(n)
Contains Duplicate II Swift Easy O(n) O(n)
Remove Duplicates from Sorted Array Swift Easy O(n) O(1)
Remove Duplicates from Sorted Array II Swift Medium O(n) O(1)
Move Zeroes Swift Easy O(n) O(1)
Remove Element Swift Easy O(n) O(1)
Two Sum Swift Easy O(n) O(n)
3Sum Swift Medium O(n^2) O(n)
4Sum Swift Medium O(n^3) O(n)
Maximum Size Subarray Sum Equals k Swift Easy O(n) O(n)
Product of Array Except Self Swift Medium O(n) O(n)
Rotate Array Swift Easy O(n) O(1)
Rotate Image Swift Medium O(n^2) O(1)
Spiral Matrix Swift Medium O(n^2) O(1)
Spiral Matrix II Swift Medium O(n^2) O(1)
Valid Sudoku Swift Easy O(n^2) O(n)
Sliding Window Maximum Swift Hard O(n) O(n)

String

Title Solution Difficulty Time Space
Count and Say Swift Easy O(n^2) O(n)
Flip Game Swift Easy O(n) O(n)
Implement strStr() Swift Easy O(nm) O(n)
Isomorphic Strings Swift Easy O(n) O(n)
Reverse String Swift Easy O(n) O(n)
Reverse Vowels of a String Swift Easy O(n) O(n)
Length of Last Word Swift Easy O(n) O(n)
Palindrome Permutation Swift Easy O(n) O(n)
Valid Anagram Swift Easy O(nlogn) O(1)
Group Anagrams Swift Medium O(nmlogm + nlogn) O(n)
Longest Common Prefix Swift Easy O(nm) O(m)
Longest Substring Without Repeating Characters Swift Medium O(n) O(n)
One Edit Distance Swift Medium O(n) O(n)
Word Pattern Swift Easy O(n) O(n)
Text Justification Swift Hard O(n^2) O(n)

Linked List

Title Solution Difficulty Time Space
Reverse Linked List Swift Easy O(n) O(1)
Palindrome Linked List Swift Easy O(n) O(1)
Swap Nodes in Pairs Swift Easy O(n) O(1)
Remove Linked List Elements Swift Easy O(n) O(1)
Remove Duplicates from Sorted List Swift Easy O(n) O(1)
Remove Duplicates from Sorted List II Swift Medium O(n) O(1)
Remove Nth Node From End of List Swift Easy O(n) O(1)
Rotate List Swift Medium O(n) O(1)
Reorder List Swift Medium O(n) O(1)
Merge Two Sorted Lists Swift Easy O(n) O(1)
Merge k Sorted Lists Swift Hard O(mlogn) O(1)
Partition List Swift Medium O(n) O(1)

Stack

Title Solution Difficulty Time Space
Valid Parentheses Swift Easy O(n) O(n)
Evaluate Reverse Polish Notation Swift Medium O(n) O(n)
Simplify Path Swift Medium O(n) O(n)
Binary Tree Preorder Traversal Swift Medium O(n) O(n)
Binary Tree Inorder Traversal Swift Medium O(n) O(n)
Binary Tree Postorder Traversal Swift Hard O(n) O(n)

Tree

Title Solution Difficulty Time Space
Same Tree Swift Easy O(n) O(1)
Symmetric Tree Swift Easy O(n) O(1)
Invert Binary Tree Swift Easy O(n) O(1)
Minimum Depth of Binary Tree Swift Easy O(n) O(1)
Maximum Depth of Binary Tree Swift Easy O(n) O(1)
Balanced Binary Tree Swift Easy O(n) O(1)
Validate Binary Search Tree Swift Medium O(n) O(1)
Binary Tree Level Order Traversal Swift Easy O(n) O(n)
Binary Tree Level Order Traversal II Swift Easy O(n) O(n)
Binary Tree Zigzag Level Order Traversal Swift Medium O(n) O(n)
Construct Binary Tree from Preorder and Inorder Traversal Swift Medium O(n) O(1)
Construct Binary Tree from Inorder and Postorder Traversal Swift Medium O(n) O(1)
Path Sum Swift Easy O(n) O(1)
Path Sum II Swift Medium O(n) O(1)

Dynamic programming

Title Solution Difficulty Time Space
Nested List Weight Sum Swift Easy O(n) O(1)
Climbing Stairs Swift Easy O(n) O(1)
Unique Paths Swift Medium O(mn) O(mn)
Unique Paths II Swift Medium O(mn) O(mn)
Generate Parentheses Swift Medium O(n^2) O(n)
Best Time to Buy and Sell Stock Swift Easy O(n) O(1)
Coin Change Swift Medium O(n^2) O(n)
Longest Increasing Subsequence Swift Medium O(n^2) O(n)
Longest Palindromic Substring Swift Medium O(n^2) O(n^2)
Perfect Squares Swift Medium O(n^2) O(n)
House Robber Swift Easy O(n) O(1)
House Robber II Swift Medium O(n) O(1)
Maximum Subarray Swift Medium O(n) O(1)
Maximum Product Subarray Swift Medium O(n) O(1)
Maximal Square Swift Medium O(mn) O(mn)
Edit Distance Swift Hard O(mn) O(mn)

Depth-first search

Title Solution Difficulty Time Space
Permutations Swift Medium O(n!) O(n)
Permutations II Swift Medium O(n!) O(n)
Subsets Swift Medium O(n!) O(n)
Subsets II Swift Medium O(n!) O(n)
Combinations Swift Medium O(n!) O(n)
Combination Sum Swift Medium O(n^n) O(2^n - 1)
Combination Sum II Swift Medium O(n!) O(2^n - 2)
Combination Sum III Swift Medium O(n!) O(nCk)
Letter Combinations of a Phone Number Swift Medium O(mn) O(n)
Number of Islands Swift Medium O(mn) O(1)
Word Search Swift Medium O((n^2)!) O(n^2)

Math

Title Solution Difficulty Time Space
Add Binary Swift Easy O(n) O(n)
Add Two Numbers Swift Medium O(n) O(1)
Plus One Swift Easy O(n) O(1)
Integer Break Swift Medium O(logn) O(1)
Happy Number Swift Easy O(n) O(n)
Single Number Swift Medium O(n) O(1)
Ugly Number Swift Easy O(logn) O(1)
Ugly Number II Swift Medium O(n) O(n)
Super Ugly Number Swift Medium O(n^2) O(n)
String to Integer (atoi) Swift Easy O(n) O(1)
Pow(x, n) Swift Medium O(logn) O(1)
Power of Two Swift Easy O(1) O(1)
Power of Three Swift Easy O(1) O(1)
Sum of Two Integers Swift Easy O(n) O(1)
Excel Sheet Column Number Swift Easy O(n) O(1)
Roman to Integer Swift Easy O(n) O(n)

Search

Title Solution Difficulty Time Space
Closest Binary Search Tree Value Swift Easy O(logn) O(1)
Search in Rotated Sorted Array Swift Hard O(logn) O(1)
Search in Rotated Sorted Array II Swift Medium O(logn) O(1)
Find Minimum in Rotated Sorted Array Swift Medium O(logn) O(1)
Find Minimum in Rotated Sorted Array II Swift Hard O(logn) O(1)
Search for a Range Swift Medium O(logn) O(1)
Search Insert Position Swift Medium O(logn) O(1)
Find Peak Element Swift Medium O(logn) O(1)
Sqrt(x) Swift Medium O(logn) O(1)

Sort

Title Solution Difficulty Time Space
Sort Colors Swift Medium O(n) O(1)
Wiggle Sort Swift Medium O(n) O(1)
Wiggle Sort II Swift Medium O(nlogn) O(n)
Top K Frequent Elements Swift Medium O(nlogn) O(n)
Meeting Rooms Swift Easy O(nlogn) O(1)
Merge Intervals Swift Hard O(nlogn) O(n)

Google

Title Solution Difficulty Frequency
Plus One Swift Easy ★★★★★★
Number of Islands Swift Medium ★★★★
Summary Ranges Swift Medium ★★★★
Perfect Squares Swift Medium ★★★★
Merge Intervals Swift Hard ★★★
Valid Parentheses Swift Easy ★★★
Merge k Sorted Lists Swift Hard ★★
Find Peak Element Swift Medium ★★
Spiral Matrix Swift Medium ★★
Sliding Window Maximum Swift Hard ★★
Power of Two Swift Easy ★★

Facebook

Title Solution Difficulty Frequency
3Sum Swift Medium ★★★★★★
Move Zeroes Swift Easy ★★★★★★
Add Binary Swift Easy ★★★★★
Two Sum Swift Easy ★★★★★
Letter Combinations of a Phone Number Swift Medium ★★★★
Merge k Sorted Lists Swift Hard ★★★★
Reverse Linked List Swift Easy ★★★
Merge Intervals Swift Hard ★★★
Subsets Swift Medium ★★★

LinkedIn

Title Solution Difficulty Frequency
Maximum Subarray Swift Medium ★★★★★★
Pow(x, n) Swift Medium ★★★★★★
Merge Intervals Swift Hard ★★★★★★
Isomorphic Strings Swift Easy ★★★★★★
Search in Rotated Sorted Array Swift Hard ★★★★★
Search for a Range Swift Medium ★★★★★
Two Sum Swift Easy ★★★★
Binary Tree Level Order Traversal Swift Easy ★★★★
Evaluate Reverse Polish Notation Swift Medium ★★★
Maximum Product Subarray Swift Medium ★★★
Product of Array Except Self Swift Medium ★★★

Amazon

Title Solution Difficulty Frequency
Two Sum Swift Easy ★★★★★★
Number of Islands Swift Medium ★★
Add Two Numbers Swift Medium ★★
Reverse Linked List Swift Easy ★★
Valid Parentheses Swift Easy ★★
Longest Palindromic Substring Swift Medium ★★
Longest Substring Without Repeating Characters Swift Medium ★★
Letter Combinations of a Phone Number Swift Medium ★★
Valid Anagram Swift Easy ★★
Rotate Image Swift Medium ★★
Best Time to Buy and Sell Stock Swift Easy ★★
3Sum Swift Medium ★★
Sliding Window Maximum Swift Hard ★★

Microsoft

Title Solution Difficulty Frequency
Reverse Linked List Swift Easy ★★★★★★
Two Sum Swift Easy ★★★★★
String to Integer (atoi) Swift Easy ★★★★
Add Two Numbers Swift Medium ★★★★
Excel Sheet Column Number Swift Easy ★★★★
Validate Binary Search Tree Swift Medium ★★★
Merge Two Sorted Lists Swift Easy ★★★

About

Solutions coded by Swift to LeetCode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%