/LeetCode

www.leetcode.com

Primary LanguageC++

C++11/14/17 Template

#include <iostream> // std::cout; std::cin
#include <fstream> // std::fstream::open; std::fstream::close; 
#include <cstdlib> // rand
#include <cassert> // assert
#include <cctype> // isalnum; isalpha; isdigit; islower; isupper; isspace; tolower; toupper
#include <cmath> // pow; sqrt; round; fabs; abs; log
#include <climits> // INT_MIN; INT_MAX; LLONG_MIN; LLONG_MAX; ULLONG_MAX
#include <cfloat> // DBL_EPSILON; LDBL_EPSILON
#include <cstring> // std::memset
#include <algorithm> // std::swap; std::max; std::min; std::min_element; std::max_element; std::minmax_element; std::next_permutation; std::prev_permutation; std::nth_element; std::sort; std::lower_bound; std::upper_bound; std::reverse
#include <limits> // std::numeric_limits<int>::min; std::numeric_limits<int>::max; std::numeric_limits<double>::epsilon; std::numeric_limits<long double>::epsilon;
#include <numeric> // std::accumulate; std::iota
#include <string> // std::to_string; std::string::npos; std::stoul; std::stoull; std::stoi; std::stol; std::stoll; std::stof; std::stod; std::stold; 
#include <list> // std::list::merge; std::list::splice; std::list::merge; std::list::unique; std::list::sort
#include <bitset>
#include <vector>
#include <deque>
#include <stack> // std::stack::top; std::stack::pop; std::stack::push
#include <queue> // std::queue::front; std::queue::back; std::queue::pop; std::queue::push
#include <set> // std::set::count; std::set::find; std::set::equal_range; std::set::lower_bound; std::set::upper_bound
#include <map> // std::map::count; std::map::find; std::map::equal_range; std::map::lower_bound; std::map::upper_bound
#include <unordered_set>
#include <unordered_map>
#include <utility> // std::pair; std::make_pair
#include <iterator>
#include <functional> // std::less<int>; std::greater<int>
using namespace std;

class Solution {
public:
	void helloworld() {
		cout << "\nHello World\n";
	}
};

int main(void) {
	Solution solution;
	solution.helloworld();
	cout << "\nPassed All\n";
	return 0;
}

Algorithms

# Title Solution Difficulty
1 Two Sum C++ Easy
2 Add Two Numbers C++ Medium
3 Longest Substring Without Repeating Characters C++ Medium
4 Median of Two Sorted Arrays C++ Hard
5 Longest Palindromic Substring C++ Medium
7 Reverse Integer C++ Easy
9 Palindrome Number C++ Easy
20 Valid Parentheses C++ Easy
21 Merge Two Sorted Lists C++ Easy
23 Merge k Sorted Lists C++ Hard
25 Reverse Nodes in k-Group C++ Hard
29 Divide Two Integers C++ Medium
30 Substring with Concatenation of All Words C++ Hard
32 Longest Valid Parentheses C++ Hard
34 Search for a Range C++ Medium
35 Search Insert Position C++ Medium
39 Combination Sum C++ Medium
40 Combination Sum II C++ Medium
41 First Missing Positive C++ Hard
42 Trapping Rain Water C++ Hard
43 Multiply Strings C++ Medium
44 Wildcard Matching C++ Hard
45 Jump Game II C++ Hard
51 N-Queens C++ Hard
52 N-Queens II C++ Hard
54 Spiral Matrix C++ Medium
55 Jump Game C++ Medium
56 Merge Intervals C++ Hard
57 Insert Interval C++ Hard
59 Spiral Matrix II C++ Medium
62 Unique Paths C++ Medium
63 Unique Paths II C++ Medium
64 Minimum Path Sum C++ Medium
65 Valid Number C++ Hard
66 Plus One C++ Easy
68 Text Justification C++ Hard
69 Sqrt(x) C++ Medium
71 Simplify Path C++ Medium
72 Edit Distance C++ Hard
76 Minimum Window Substring C++ Hard
79 Word Search C++ Medium
82 Remove Duplicates from Sorted List II C++ Medium
83 Remove Duplicates from Sorted List C++ Easy
84 Largest Rectangle in Histogram C++ Hard
85 Maximal Rectangle C++ Hard
90 Subsets II C++ Medium
91 Decode Ways C++ Medium
92 Reverse Linked List II C++ Medium
93 Restore IP Addresses C++ Medium
94 Binary Tree Inorder Traversal C++ Medium
95 Unique Binary Search Trees II C++ Medium
96 Unique Binary Search Trees C++ Medium
97 Interleaving String C++ Hard
98 Validate Binary Search Tree C++ Medium
99 Recover Binary Search Tree C++ Hard
100 Same Tree C++ Easy
101 Symmetric Tree C++ Easy
102 Binary Tree Level Order Traversal C++ Medium
103 Binary Tree Zigzag Level Order Traversal C++ Medium
105 Construct Binary Tree from Preorder and Inorder Traversal C++ Medium
106 Construct Binary Tree from Inorder and Postorder Traversal C++ Medium
108 Convert Sorted Array to Binary Search Tree C++ Medium
109 Convert Sorted List to Binary Search Tree C++ Medium
112 Path Sum C++ Easy
113 Path Sum II C++ Medium
114 Flatten Binary Tree to Linked List C++ Medium
115 Distinct Subsequences C++ Hard
121 Best Time to Buy and Sell Stock C++ Easy
122 Best Time to Buy and Sell Stock II C++ Medium
123 Best Time to Buy and Sell Stock III C++ Hard
124 Binary Tree Maximum Path Sum C++ Hard
125 Valid Palindrome C++ Easy
126 Word Ladder II C++ Hard
128 Longest Consecutive Sequence C++ Hard
129 Sum Root to Leaf Numbers C++ Medium
130 Surrounded Regions C++ Medium
131 Palindrome Partitioning C++ Medium
132 Palindrome Partitioning II C++ Hard
133 Clone Graph C++ Medium
134 Gas Station C++ Medium
135 Candy C++ Hard
136 Single Number C++ Easy
138 Copy List with Random Pointer C++ Hard
141 Linked List Cycle C++ Easy
142 Linked List Cycle II C++ Medium
143 Reorder List C++ Medium
144 Binary Tree Preorder Traversal C++ Medium
145 Binary Tree Postorder Traversal C++ Hard
146 LRU Cache C++ Hard
147 Insertion Sort List C++ Medium
148 Sort List C++ Medium
149 Max Points on a Line C++ Hard
150 Evaluate Reverse Polish Notation C++ Medium
151 Reverse Words in a String C++ Medium
152 Maximum Product Subarray C++ Medium
153 Find Minimum in Rotated Sorted Array C++ Medium
154 Find Minimum in Rotated Sorted Array II C++ Hard
155 Min Stack C++ Easy
156 Binary Tree Upside Down C++ Medium
157 Read N Characters Given Read4 C++ Easy
158 Read N Characters Given Read4 II - Call multiple times C++ Hard
159 Longest Substring with At Most Two Distinct Characters C++ Hard
162 Find Peak Element C++ Medium
163 Missing Ranges C++ Medium
164 Maximum Gap C++ Hard
166 Fraction to Recurring Decimal C++ Medium
167 Two Sum II - Input array is sorted C++ Medium
173 Binary Search Tree Iterator C++ Medium
174 Dungeon Game C++ Hard
187 Repeated DNA Sequences C++ Medium
188 Best Time to Buy and Sell Stock IV C++ Hard
191 Number of 1 Bits C++ Easy
198 House Robber C++ Easy
199 Binary Tree Right Side View C++ Medium
200 Number of Islands C++ Medium
201 Bitwise AND of Numbers Range C++ Medium
206 Reverse Linked List C++ Easy
207 Course Schedule C++ Medium
208 Implement Trie (Prefix Tree) C++ Medium
209 Minimum Size Subarray Sum C++ Medium
210 Course Schedule II C++ Medium
211 Add and Search Word - Data structure design C++ Medium
212 Word Search II C++ Hard
213 House Robber II C++ Medium
214 Shortest Palindrome C++ Hard
215 Kth Largest Element in an Array C++ Medium
216 Combination Sum III C++ Medium
217 Contains Duplicate C++ Easy
218 The Skyline Problem C++ Hard
219 Contains Duplicate II C++ Easy
220 Contains Duplicate III C++ Medium
221 Maximal Square C++ Medium
222 Count Complete Tree Nodes C++ Medium
224 Basic Calculator C++ Hard
227 Basic Calculator II C++ Medium
230 Kth Smallest Element in a BST C++ Medium
231 Power of Two C++ Easy
233 Number of Digit One C++ Hard
235 Lowest Common Ancestor of a Binary Search Tree C++ Easy
236 Lowest Common Ancestor of a Binary Tree C++ Medium
238 Product of Array Except Self C++ Medium
239 Sliding Window Maximum C++ Hard
241 Different Ways to Add Parentheses C++ Medium
242 Valid Anagram C++ Easy
246 Strobogrammatic Number C++ Medium
247 Strobogrammatic Number II C++ Medium
248 Strobogrammatic Number III C++ Hard
250 Count Univalue Subtrees C++ Medium
251 Flatten 2D Vector C++ Medium
253 Meeting Rooms II C++ Medium
254 Factor Combinations C++ Medium
255 Verify Preorder Sequence in Binary Search Tree C++ Medium
256 Paint House C++ Medium
261 Graph Valid Tree C++ Medium
258 Add Digits C++ Easy
263 Ugly Number C++ Easy
264 Ugly Number II C++ Medium
265 Paint House II C++ Hard
266 Palindrome Permutation C++ Easy
268 Missing Number C++ Medium
269 Alien Dictionary C++ Hard
270 Closest Binary Search Tree Value C++ Easy
271 Encode and Decode Strings C++ Medium
272 Closest Binary Search Tree Value II C++ Hard
273 Integer to English Words C++ Hard
274 H-Index C++ Medium
275 H-Index II C++ Medium
277 Find the Celebrity C++ Medium
279 Perfect Squares C++ Medium
280 Wiggle Sort C++ Medium
281 Zigzag Iterator C++ Medium
282 Expression Add Operators C++ Hard
283 Move Zeroes C++ Easy
284 Peeking Iterator C++ Medium
285 Inorder Successor in BST C++ Medium
286 Walls and Gates C++ Medium
287 Find the Duplicate Number C++ Hard
289 Game of Life C++ Medium
290 Word Pattern C++ Easy
291 Word Pattern II C++ Hard
292 Nim Game C++ Easy
293 Flip Game C++ Easy
294 Flip Game II C++ Medium
295 Find Median from Data Stream C++ Hard
296 Best Meeting Point C++ Hard
297 Serialize and Deserialize Binary Tree C++ Hard
298 Binary Tree Longest Consecutive Sequence C++ Medium
300 Longest Increasing Subsequence C++ Medium
301 Remove Invalid Parentheses C++ Hard
302 Smallest Rectangle Enclosing Black Pixels C++ Hard
305 Number of Islands II C++ Hard
306 Additive Number C++ Medium
307 Range Sum Query - Mutable C++ Medium
308 Range Sum Query 2D - Mutable C++ Hard
309 Best Time to Buy and Sell Stock with Cooldown C++ Medium
310 Minimum Height Trees C++ Medium
312 Burst Balloons C++ Hard
313 Super Ugly Number C++ Medium
314 Binary Tree Vertical Order Traversal C++ Medium
315 Count of Smaller Numbers After Self C++ Hard
316 Remove Duplicate Letters C++ Hard
317 Shortest Distance from All Buildings C++ Hard
318 Maximum Product of Word Lengths C++ Medium
319 Bulb Switcher C++ Medium
320 Generalized Abbreviation C++ Medium
321 Create Maximum Number C++ Hard
322 Coin Change C++ Medium
323 Number of Connected Components in an Undirected Graph C++ Medium
324 Wiggle Sort II C++ Medium
327 Count of Range Sum C++ Hard
329 Longest Increasing Path in a Matrix C++ Hard
330 Patching Array C++ Hard
331 Verify Preorder Serialization of a Binary Tree C++ Medium
332 Reconstruct Itinerary C++ Medium
333 Largest BST Subtree C++ Medium
334 Increasing Triplet Subsequence C++ Medium
335 Self Crossing C++ Hard
336 Palindrome Pairs C++ Hard
339 Nested List Weight Sum C++ Easy
340 Longest Substring with At Most K Distinct Characters C++ Hard
341 Flatten Nested List Iterator C++ Medium
342 Power of Four C++ Easy
343 Integer Break C++ Medium
346 Moving Average from Data Stream C++ Easy
347 Top K Frequent Elements C++ Medium
348 Design Tic-Tac-Toe C++ Medium
349 Intersection of Two Arrays C++ Easy
350 Intersection of Two Arrays II C++ Easy
351 Android Unlock Patterns C++ Medium
352 Data Stream as Disjoint Intervals C++ Hard
353 Design Snake Game C++ Medium
354 Russian Doll Envelopes C++ Hard
355 Design Twitter C++ Medium
356 Line Reflection C++ Medium
357 Count Numbers with Unique Digits C++ Medium
358 Rearrange String k Distance Apart C++ Hard
359 Logger Rate Limiter C++ Easy
360 Sort Transformed Array C++ Medium
361 Bomb Enemy C++ Medium
362 Design Hit Counter C++ Medium
363 Max Sum of Rectangle No Larger Than K C++ Hard
364 Nested List Weight Sum II C++ Medium
365 Water and Jug Problem C++ Medium
366 Find Leaves of Binary Tree C++ Medium
367 Valid Perfect Square C++ Medium
368 Largest Divisible Subset C++ Medium
369 Plus One Linked List C++ Medium
370 Range Addition C++ Medium
371 Sum of Two Integers C++ Easy
372 Super Pow C++ Medium
373 Find K Pairs with Smallest Sums C++ Medium
374 Guess Number Higher or Lower C++ Easy
375 Guess Number Higher or Lower II C++ Medium
376 Wiggle Subsequence C++ Medium
377 Combination Sum IV C++ Medium
378 Kth Smallest Element in a Sorted Matrix C++ Medium
379 Design Phone Directory C++ Medium
380 Insert Delete GetRandom O(1) C++ Hard
381 Insert Delete GetRandom O(1) - Duplicates allowed C++ Hard
382 Linked List Random Node C++ Medium
383 Ransom Note C++ Easy
384 Shuffle an Array C++ Medium
385 Mini Parser C++ Medium
386 Lexicographical Numbers C++ Medium
387 First Unique Character in a String C++ Easy
388 Longest Absolute File Path C++ Medium
389 Find the Difference C++ Easy
390 Elimination Game C++ Medium
391 Perfect Rectangle C++ Hard
392 Is Subsequence C++ Medium
393 UTF-8 Validation C++ Medium
394 Decode String C++ Medium
395 Longest Substring with At Least K Repeating Characters C++ Medium
396 Rotate Function C++ Easy
397 Integer Replacement C++ Easy
398 Random Pick Index C++ Medium
399 Evaluate Division C++ Medium
400 Nth Digit C++ Easy
401 Binary Watch C++ Easy
402 Remove K Digits C++ Medium
403 Frog Jump C++ Hard
404 Sum of Left Leaves C++ Easy
405 Convert a Number to Hexadecimal C++ Easy
406 Queue Reconstruction by Height C++ Medium
407 Trapping Rain Water II C++ Hard
408 Valid Word Abbreviation C++ Easy
409 Longest Palindrome C++ Easy
410 Split Array Largest Sum C++ Hard
411 Minimum Unique Word Abbreviation C++ Hard
412 Fizz Buzz C++ Easy
413 Arithmetic Slices C++ Medium
414 Third Maximum Number C++ Easy
415 Add Strings C++ Easy
416 Partition Equal Subset Sum C++ Medium
417 Pacific Atlantic Water Flow C++ Medium
418 Sentence Screen Fitting C++ Medium
419 Battleships in a Board C++ Medium
420 Strong Password Checker C++ Hard
421 Maximum XOR of Two Numbers in an Array C++ Medium
422 Valid Word Square C++ Easy
423 Reconstruct Original Digits from English C++ Medium
424 Longest Repeating Character Replacement C++ Medium
425 Word Squares C++ Hard
432 All O`one Data Structure C++ Hard
433 Minimum Genetic Mutation C++ Medium
434 Number of Segments in a String C++ Easy
435 Non-overlapping Intervals C++ Medium
436 Find Right Interval C++ Medium
437 Path Sum III C++ Easy
438 Find All Anagrams in a String C++ Easy
439 Ternary Expression Parser C++ Medium
440 K-th Smallest in Lexicographical Order C++ Hard
441 Arranging Coins C++ Easy
442 Find All Duplicates in an Array C++ Medium
444 Sequence Reconstruction C++ Medium
445 Add Two Numbers II C++ Medium
446 Arithmetic Slices II - Subsequence C++ Hard
447 Number of Boomerangs C++ Easy
448 Find All Numbers Disappeared in an Array C++ Easy
449 Serialize and Deserialize BST C++ Medium
450 Delete Node in a BST C++ Medium
451 Sort Characters By Frequency C++ Medium
452 Minimum Number of Arrows to Burst Balloons C++ Medium
453 Minimum Moves to Equal Array Elements C++ Easy
454 4Sum II C++ Medium
455 Assign Cookies C++ Easy
456 132 Pattern C++ Medium
458 Poor Pigs C++ Easy
459 Repeated Substring Pattern C++ Easy
460 LFU Cache C++ Hard
461 Hamming Distance C++ Easy
462 Minimum Moves to Equal Array Elements II C++ Medium
463 Island Perimeter C++ Easy
464 Can I Win C++ Medium
465 Optimal Account Balancing C++ Hard
466 Count The Repetitions C++ Hard
467 Unique Substrings in Wraparound String C++ Medium
468 Validate IP Address C++ Medium
469 Convex Polygon C++ Medium
471 Encode String with Shortest Length C++ Hard
472 Concatenated Words C++ Hard
473 Matchsticks to Square C++ Medium
474 Ones and Zeroes C++ Medium
475 Heaters C++ Easy
476 Number Complement C++ Easy
477 Total Hamming Distance C++ Medium
480 Sliding Window Median C++ Hard
481 Magical String C++ Medium
482 License Key Formatting C++ Medium
483 Smallest Good Base C++ Hard
484 Find Permutation C++ Medium
485 Max Consecutive Ones C++ Easy
486 Predict the Winner C++ Medium
487 Max Consecutive Ones II C++ Medium
488 Zuma Game C++ Hard
490 The Maze C++ Medium
491 Increasing Subsequences C++ Medium
492 Construct the rectangle C++ Easy
493 Reverse Pairs C++ Hard
494 Target Sum C++ Medium
495 Teemo Attacking C++ Medium
496 Next Greater Element I C++ Easy
498 Diagonal Traverse C++ Hard
499 The Maze III C++ Hard
500 Keyboard Row C++ Easy
501 Find Mode in Binary Search Tree C++ Easy
502 IPO C++ Hard
503 Next Greater Element II C++ Medium
504 Base 7 C++ Easy
505 The Maze II C++ Medium
506 Relative Ranks C++ Easy
507 Perfect Number C++ Easy
508 Most Frequent Subtree Sum C++ Medium
513 Find Bottom Left Tree Value C++ Medium
514 Freedom Trail C++ Hard
515 Find Largest Value in Each Tree Row C++ Medium
516 Longest Palindromic Subsequence C++ Medium
517 Super Washing Machines C++ Hard
520 Detect Capital C++ Easy
523 Continuous Subarray Sum C++ Medium
524 Longest Word in Dictionary through Deleting C++ Medium
525 Contiguous Array C++ Medium
526 Beautiful Arrangement C++ Medium
527 Word Abbreviation C++ Hard
529 Minesweeper C++ Medium
530 Minimum Absolute Difference in BST C++ Easy
531 Lonely Pixel I C++ Medium
532 K-diff Pairs in an Array C++ Easy
533 Lonely Pixel II C++ Medium
535 Encode and Decode TinyURL C++ Medium
536 Construct Binary Tree from String C++ Medium
537 Complex Number Multiplication C++ Medium
538 Convert BST to Greater Tree C++ Medium
539 Minimum Time Difference C++ Medium
540 Single Element in a Sorted Array C++ Medium
541 Reverse String II C++ Easy
542 01 Matrix C++ Medium
543 Diameter of Binary Tree C++ Easy
544 Output Contest Matches C++ Medium
545 Boundary of Binary Tree C++ Medium
546 Remove Boxes C++ Hard
554 Brick Wall C++ Medium
557 Reverse Words in a String III C++ Easy
560 Subarray Sum Equals K C++ Medium
566 Reshape the Matrix C++ Easy
567 Permutation in String C++ Medium
568 Maximum Vacation Days C++ Hard