Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

robocar2018/leetcode-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

187 Commits

Repository files navigation

leetcode

Personal notes for leetcode.com solutions with support for local building and unit testing.

Stats

Top interviewed: 76 / 145 52%

In total: 114 / 2143 5%

99.00%+ in running time: 79 / 114 69%

Solutions

  1. Two Sum
  • Hash table (unorderd_map): 4ms (top 99.54%)
  1. Add Two Numbers
  • Linked list: 16ms (top 100.00%)
  1. Longest Substring Without Repeating Characters
  • Hash table (vector): 0ms (top 100.00%)
  1. Median of Two Sorted Arrays
  • Non-recursive: 35 ms (top 63.25%)
  • Recursion: 16 ms (top 100.00%)
  1. Longest Palindromic Substring
  • Brutal force: 9ms (top 94.73%)
  • Manacher's algorithm: 0ms (top 100.00%)
  1. ZigZag Conversion
  • Brutal force: 40ms (top 55.26%)
  • Fast for-loop: 8ms (top 100.00%)
  1. Reverse Integer
  • 0ms (top 100.00%)
  1. String to Integer (atoi)
  • 0ms (top 100.00%)
  1. Palindrome Number
  • Lookup table: 52ms (top 100.00%)
  1. Regular Expression Matching
  • Dynamic programming: 4ms (top 100.00%)
  1. Container With Most Water
  • Two pointers: 12ms (top 100.00%)
  1. Integer to Roman
  • Two arrays: 28ms (top 99.20%)
  • Lookup table: 28ms (top 99.20%)
  1. Roman to Integer
  • 10ms (top 67.12%)
  • Map: 44ms (top 97.72%)
  1. Longest Common Prefix
  • 4ms (top 100.00%)
  1. 3Sum
  • Two pointers: 112ms (top 99.88%)
  1. 3Sum Closest
  • Two pointers: 8ms (top 100.00%)
  1. Letter Combinations of a Phone Number
  • Deque: 4ms (top 100.00%)
  • DFS: 0ms (top 100.00%)
  1. 4Sum
  • Set: 40ms (top 82.57%)
  • Fast for-loop: 20ms (top 90.04%)
  1. Remove Nth Node From End of List
  • 0ms (top 100.00%)
  1. Valid Parentheses
  • Stack: 4ms (top 100.00%)
  1. Merge Two Sorted Lists
  • Two swaps: 12ms (top 100.00%)
  1. Generate Parentheses
  1. Merge k Sorted Lists
  • Use 021: 16ms (top 98.02%)
  • Priority queue: 32ms (top 98.10%)
  1. Swap Nodes in Pairs
  • 0ms (top 100.00%)
  1. Reverse Nodes in k-Group
  1. Remove Duplicates from Sorted Array
  1. Remove Element
  • 0ms (top 100.00%)
  1. Implement strStr()
  • 8ms (top 98.99%)
  • KMP: 4ms (top 91.65%)
  1. Divide Two Integers
  • 0ms (top 100.00%)
  1. Substring with Concatenation of All Words
  1. Next Permutation
  • 4ms (top 96.34%)
  1. Longest Valid Parentheses
  • 4ms (top 96.65%)
  1. Search in Rotated Sorted Array
  • 0ms (top 100.00%)
  1. Find First and Last Position of Element in Sorted Array
  • 8ms (top 98.52%)
  1. Search Insert Position
  • 4ms (top 98.31%)
  1. Valid Sudoku
  • 8ms (top 99.96%)
  1. Sudoku Solver
  • 4ms (top 95.52%)
  1. Count and Say
  • 4ms (top 97.84%)
  1. Combination Sum
  • 8ms (top 95.15%)
  1. Combination Sum II
  • 4ms (top 98.92%)
  1. First Missing Positive
  • 4ms (top 96.34%)
  1. Trapping Rain Water
  • 4ms (top 99.71%)
  1. Multiply Strings
  • C++ interface: 8ms (top 72.26%)
  • C interface: 4ms (top 97.53%)
  1. Wildcard Matching
  • 4ms (top 99.84%)
  1. Jump Game II
  • 8ms (top 95.32%)
  1. Permutations
  • Recursive: 20ms (top 22.83%)
  • Better loop: 12ms (top 96.86%)
  • Backtracking: 4ms (top 96.41%)
  1. Permutations II
  • 8ms (top 96.44%)
  1. Rotate Image
  • Direct rotation: 4ms (top 98.21%)
  • Flip & swap: 4ms (top 98.21%)
  1. Group Anagrams
  1. Pow(x, n)
  • 4ms (top 96.07%)
  1. N-Queens
  • 4ms (top 99.62%)
  1. Maximum Subarray
  • Divide and conquer: 8ms (top 96.32%)
  • Kadane: 4ms (top 99.88%)
  • DP: 0ms (top 100.00%)
  1. Spiral Matrix
  • 0ms (top 100.00%)
  1. Jump Game
  1. Merge Intervals
  • 8ms (top 100.00%)
  1. Permutation Sequence
  • 0ms (top 100.00%)
  1. Unique Paths
  • 0ms (top 100.00%)
  1. Unique Paths II
  • 0ms (top 100.00%)
  1. Minimum Path Sum
  • 4ms (top 99.39%)
  1. Plus One
  • 0ms (top 100.00%)
  1. Sqrt(x)
  • 0ms (top 100.00%)
  1. Climbing Stairs
  • 0ms (top 100.00%)
  1. Set Matrix Zeroes
  1. Search a 2D Matrix
  • 4ms (top 99.56%)
  1. Sort Colors
  • 0ms (top 100.00%)
  • three pointer: 0ms (top 100.00%)
  1. Minimum Window Substring
  • sliding window: 32ms (top 38.25%)
  • optimized sliding window: 44ms (top 19.33%)
  • hash: 4ms (top 99.92%)
  1. Subsets
  • 8ms (top 90.16%)
  • backtrack: 4ms (top 98.41%)
  • bitwise: 0ms (top 100.00%)
  1. Word Search
  1. Largest Rectangle in Histogram
  • recursion: 32ms (top 14.84%)
  • two vector: 4ms (top 100.00%)
  • one vector: 122ms (top 75.97%)
  1. Merge Sorted Array
  • additional space: 4ms (top 96.53%)
  • 3 pointers: 0ms (top 100.00%)
  1. Decode Ways
  • 0ms (top 100.00%)
  1. Binary Tree Inorder Traversal
  • Recursion - list: 4ms (top 87.58%)
  • Recursion - vector: 4ms (top 43.93%)
  • Stack: 0ms (top 100.00%)
  • Stack: 0ms (top 100.00%)
  1. Validate Binary Search Tree
  • Two pointers: 4ms (top 99.99%)
  • 12ms (top 74.23%)
  1. Symmetric Tree
  • Recursive: 0ms (top 100.00%)
  • Queue: 4ms (top 81.64%)
  1. Binary Tree Level Order Traversal
  • 4ms (top 76.73%)
  1. Binary Tree Zigzag Level Order Traversal
  • 4ms (top 63.24%)
  1. Maximum Depth of Binary Tree
  • 8ms (top 75.97%)
  1. Construct Binary Tree from Preorder and Inorder Traversal
  • 40ms (top 19.30%)
  • hash: 16ms (top 89.76%)
  • Stack: 11ms (top 94.01%)
  1. Convert Sorted Array to Binary Search Tree
  • 4ms (top 99.83%)
  1. Minimum Depth of Binary Tree
  • 4ms (top 99.83%)
  1. Populating Next Right Pointers in Each Node
  1. Pascal's Triangle
  • swap: 4ms (top 51.26%)
  • resize: 0ms (top 100.00%)
  • 0ms (top 100.00%)
  1. Triangle
  • 0ms (top 100.00%)
  1. Best Time to Buy and Sell Stock
  • priority queue: 12ms (top 17.37%)
  • 4ms (top 97.69%)
  • 76ms (top 99.79%)
  1. Best Time to Buy and Sell Stock II
  • 0ms (top 100.00%)
  1. Best Time to Buy and Sell Stock III
  1. Binary Tree Maximum Path Sum
  1. Valid Palindrome
  • 4ms (top 99.16%)
  1. Word Ladder
  • 28ms (top 99.56%)
  • Graph + BFS: 138ms (top 67.54%)
  • Graph + BiDir BFS: 131ms (top 69.50%)
  • BiDir BFS: 560ms (top 34.48%)
  1. Longest Consecutive Sequence
  • hash: 97ms (top 63.61%)
  • sort: 34ms (top 96.44%)
  • dp: 87ms (top 67.80%)
  1. Surrounded Regions
  • 4ms (top 99.88%)
  1. Palindrome Partitioning
  • backtrack: 159ms (top 63.39%)
  • backtrack + DP: 150ms (top 67.07%)
  1. Gas Station
  • 0ms (top 100.00%)
  1. Single Number
  • 4ms (top 99.88%)
  1. Binary Tree Preorder Traversal
  • Recursion: 4ms (top 43.36%)
  • Stack: 0ms (top 100.00%)
  1. Binary Tree Postorder Traversal
  • Recursion: 4ms (top 44.24%)
  • Stack: 0ms (top 100.00%)
  1. Evaluate Reverse Polish Notation
  1. Find Minimum in Rotated Sorted Array
  • 0ms (top 100.00%)
  1. Repeated DNA Sequences
  • 3-bit hash: 52ms (top 81.78%)
  • 2-bit hash: 4ms (top 99.71%)
  1. House Robber
  • 0ms (top 100.00%)
  1. House Robber II
  • 0ms (top 100.00%)
  1. Kth Largest Element in an Array
  • 4ms (top 99.81%)
  1. Maximal Square
  • 8ms (top 99.64%)
  1. Search a 2D Matrix II
  1. First Bad Version
  • 0ms (top 100.00%)
  1. Perfect Squares
  • 0ms (top 100.00%)
  • Tree search: 92ms (top 79.03%)
  1. Find the Duplicate Number
  • Binary search: 8ms (top 94.55%)
  • Two pointers: 4ms (top 99.76%)
  1. Longest Increasing Subsequence
  • 0ms (top 100.00%)
  1. Remove Invalid Parentheses
  1. Intersection of Two Arrays
  • Set: 4ms (top 99.21%)
  • Map: 4ms (top 99.21%)
  1. Intersection of Two Arrays II
  • 4ms (top 99.54%)
  1. Single Element in a Sorted Array
  • recursion: 19ms (top 96.25%)
  • iteration: 12ms (top 99.87%)
  1. Find the Closest Palindrome
  • 0ms (top 100.00%)
  1. Shortest Path to Get All Keys

Problem categories

Category Problems
Sort/search 026, 031, 036, 041, 048, 054, 055, 056, 073, 074, 075, 118, 121, 122, 123, 128, 134, 136, 153, 215, 240, 278
String 005, 006, 008, 010, 014, 028, 038, 043, 044, 127
Numerical 007, 009, 029, 050, 066, 069, 279, 564
Hash/Lookup 001, 003, 012, 013, 020, 030, 049, 076, 187, 349, 350
Stack/Queue/Deque 023, 032, 094, 102, 103, 116, 150
Linked list 002, 019, 021, 024
Two pointers 011, 015, 016, 018, 027, 042, 084, 088, 125, 287
Recursion 004, 025, 033, 034, 035, 045, 079, 098, 101, 104, 105, 108, 111, 124, 130, 144, 145, 301, 540
Backtracking 017, 022, 037, 039, 040, 046, 047, 051, 060, 078
DP 053, 062, 063, 064, 070, 091, 120, 131, 198, 213, 221, 300, 864

Note that binary tree problems are mostly included in the Recursion category.


Environment

sudo snap install --classic code
sudo apt-get install pylint
  • clang (used by default. You can use other tools as long as your .vscode/c_cpp_properties.json file is modified correctly)
wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt-get update
sudo apt-get install clang-8 lldb-8
  • CMake
  • Boost (for unit testing)
wget -O boost_1_70_0.tar.gz https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz
tar xzvf boost_1_70_0.tar.gz
cd boost_1_70_0/
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev
./bootstrap.sh --prefix=/usr/
./b2
sudo ./b2 install

Usage

Each folder contains 3 files: solution.hpp, solution.cpp and solution_test.cpp. The first folder 001-Two-Sum contains an additional main.cpp file, in case you don't feel like using unit testing and you can test your own cases in main.cpp.

Add following lines to your settings.json to configure the building and linting:

{
 "clang.executable": "clang++",
 "clang.cflags": ["c11"],
 "clang.cxxflags": ["-std=c++17"],
 "lldb.executable": "lldb"
}

Steps to build a solution:

  1. Change the line set(PROBLEM_NAME {Problem_Folder}) in CMakeLists.txt to choose the problem you want to solve, in which {Problem_Folder} is the folder name of the problem. For example, set(PROBLEM_NAME "001-Two-Sum").

  2. Press Ctrl + Shift + P to bring up the Command Palette of VSCode. Type in CMake, and look for a CMake: Configure command, select it. It will configure the cache files and makefile which are located in build folder by default.

  3. Type in or look for a CMake: Build command in the Command Palette and execute it. It will compile the source codes of the solution that you previously chose.

  4. Debug Press Ctrl + Shift + D or click the bug icon on the left. The .vscode/launch.json for debugging is already set up for you, and both GDB and LLDB is supported.

    gdb: Choose (gdb) Launch and you're good to go.

    lldb: Choose (lldb) Launch. You may need to disable and re-enable ms-vscode.cpptools and mitaki28.vscode-clang extensions in case the variables won't properly display in "Watch" Panel. Data visualization is supported (https://github.com/vadimcn/vscode-lldb/wiki/Data-visualization).

  5. Unit testing Type in or look for a CMake: Run tests command in the Command Palette and execute it. It will run all test cases in solution_test.cpp. Feel free to add your own test cases.

Clear build folder:

  1. Press Ctrl + Shift + P to bring up the Command Palette. Type in Task: Run Task and press enter. Choose clean will delete everything in the build folder. Bear in mind that you need to configure the project after cleaning the folder or switching to another solution, by either executing Task: Run Task -> configure or CMake: Configure.

About

leetcode.com solutions with unit testing (VSCode+CMake+Boost.Test)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 99.6%
  • CMake 0.4%

AltStyle によって変換されたページ (->オリジナル) /