├── 0001-two-sum ├── 0001-two-sum.cpp ├── 0001-two-sum.java └── README.md ├── 0002-add-two-numbers ├── 0002-add-two-numbers.cpp └── README.md ├── 0003-longest-substring-without-repeating-characters ├── 0003-longest-substring-without-repeating-characters.java └── README.md ├── 0007-reverse-integer ├── 0007-reverse-integer.java └── README.md ├── 0009-palindrome-number ├── 0009-palindrome-number.cpp └── README.md ├── 0013-roman-to-integer ├── 0013-roman-to-integer.cpp └── README.md ├── 0014-longest-common-prefix ├── 0014-longest-common-prefix.java └── README.md ├── 0015-3sum ├── 0015-3sum.java └── README.md ├── 0017-letter-combinations-of-a-phone-number ├── 0017-letter-combinations-of-a-phone-number.cpp └── README.md ├── 0018-4sum ├── 0018-4sum.java └── README.md ├── 0019-remove-nth-node-from-end-of-list ├── 0019-remove-nth-node-from-end-of-list.cpp └── README.md ├── 0020-valid-parentheses ├── 0020-valid-parentheses.cpp ├── 0020-valid-parentheses.java └── README.md ├── 0021-merge-two-sorted-lists ├── 0021-merge-two-sorted-lists.cpp └── README.md ├── 0024-swap-nodes-in-pairs ├── 0024-swap-nodes-in-pairs.cpp └── README.md ├── 0025-reverse-nodes-in-k-group ├── 0025-reverse-nodes-in-k-group.cpp └── README.md ├── 0026-remove-duplicates-from-sorted-array ├── 0026-remove-duplicates-from-sorted-array.cpp └── README.md ├── 0027-remove-element ├── 0027-remove-element.java └── README.md ├── 0028-find-the-index-of-the-first-occurrence-in-a-string ├── 0028-find-the-index-of-the-first-occurrence-in-a-string.java └── README.md ├── 0031-next-permutation ├── 0031-next-permutation.cpp └── README.md ├── 0033-search-in-rotated-sorted-array ├── 0033-search-in-rotated-sorted-array.cpp ├── 0033-search-in-rotated-sorted-array.java └── README.md ├── 0034-find-first-and-last-position-of-element-in-sorted-array ├── 0034-find-first-and-last-position-of-element-in-sorted-array.cpp └── README.md ├── 0035-search-insert-position ├── 0035-search-insert-position.cpp └── README.md ├── 0037-sudoku-solver ├── 0037-sudoku-solver.cpp └── README.md ├── 0041-first-missing-positive ├── 0041-first-missing-positive.cpp └── README.md ├── 0042-trapping-rain-water ├── 0042-trapping-rain-water.cpp └── README.md ├── 0046-permutations ├── 0046-permutations.cpp └── README.md ├── 0048-rotate-image ├── 0048-rotate-image.cpp └── README.md ├── 0049-group-anagrams ├── 0049-group-anagrams.cpp └── README.md ├── 0050-powx-n ├── 0050-powx-n.cpp ├── 0050-powx-n.java └── README.md ├── 0051-n-queens ├── 0051-n-queens.cpp └── README.md ├── 0053-maximum-subarray ├── 0053-maximum-subarray.cpp └── README.md ├── 0054-spiral-matrix ├── 0054-spiral-matrix.cpp └── README.md ├── 0056-merge-intervals ├── 0056-merge-intervals.cpp └── README.md ├── 0058-length-of-last-word ├── 0058-length-of-last-word.java └── README.md ├── 0061-rotate-list ├── 0061-rotate-list.cpp └── README.md ├── 0062-unique-paths ├── 0062-unique-paths.cpp └── README.md ├── 0070-climbing-stairs ├── 0070-climbing-stairs.cpp └── README.md ├── 0073-set-matrix-zeroes ├── 0073-set-matrix-zeroes.cpp └── README.md ├── 0074-search-a-2d-matrix ├── 0074-search-a-2d-matrix.cpp ├── 0074-search-a-2d-matrix.java └── README.md ├── 0075-sort-colors ├── 0075-sort-colors.cpp └── README.md ├── 0079-word-search ├── 0079-word-search.cpp ├── 0079-word-search.java └── README.md ├── 0081-search-in-rotated-sorted-array-ii ├── 0081-search-in-rotated-sorted-array-ii.cpp └── README.md ├── 0083-remove-duplicates-from-sorted-list ├── 0083-remove-duplicates-from-sorted-list.cpp └── README.md ├── 0088-merge-sorted-array ├── 0088-merge-sorted-array.cpp └── README.md ├── 0090-subsets-ii ├── 0090-subsets-ii.cpp └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.cpp └── README.md ├── 0096-unique-binary-search-trees ├── 0096-unique-binary-search-trees.cpp └── README.md ├── 0098-validate-binary-search-tree ├── 0098-validate-binary-search-tree.cpp └── README.md ├── 0100-same-tree ├── 0100-same-tree.cpp └── README.md ├── 0101-symmetric-tree ├── 0101-symmetric-tree.cpp └── README.md ├── 0102-binary-tree-level-order-traversal ├── 0102-binary-tree-level-order-traversal.cpp └── README.md ├── 0103-binary-tree-zigzag-level-order-traversal ├── 0103-binary-tree-zigzag-level-order-traversal.cpp └── README.md ├── 0104-maximum-depth-of-binary-tree ├── 0104-maximum-depth-of-binary-tree.cpp ├── 0104-maximum-depth-of-binary-tree.java └── README.md ├── 0108-convert-sorted-array-to-binary-search-tree ├── 0108-convert-sorted-array-to-binary-search-tree.cpp └── README.md ├── 0111-minimum-depth-of-binary-tree ├── 0111-minimum-depth-of-binary-tree.java └── README.md ├── 0112-path-sum ├── 0112-path-sum.cpp └── README.md ├── 0118-pascals-triangle ├── 0118-pascals-triangle.cpp └── README.md ├── 0121-best-time-to-buy-and-sell-stock ├── 0121-best-time-to-buy-and-sell-stock.cpp ├── 0121-best-time-to-buy-and-sell-stock.java └── README.md ├── 0124-binary-tree-maximum-path-sum ├── 0124-binary-tree-maximum-path-sum.cpp └── README.md ├── 0125-valid-palindrome ├── 0125-valid-palindrome.cpp ├── 0125-valid-palindrome.java └── README.md ├── 0128-longest-consecutive-sequence ├── 0128-longest-consecutive-sequence.cpp ├── 0128-longest-consecutive-sequence.java └── README.md ├── 0131-palindrome-partitioning ├── 0131-palindrome-partitioning.cpp └── README.md ├── 0135-candy ├── 0135-candy.cpp └── README.md ├── 0136-single-number ├── 0136-single-number.cpp └── README.md ├── 0138-copy-list-with-random-pointer ├── 0138-copy-list-with-random-pointer.java └── README.md ├── 0141-linked-list-cycle ├── 0141-linked-list-cycle.cpp ├── 0141-linked-list-cycle.java └── README.md ├── 0142-linked-list-cycle-ii ├── 0142-linked-list-cycle-ii.cpp ├── 0142-linked-list-cycle-ii.java └── README.md ├── 0144-binary-tree-preorder-traversal ├── 0144-binary-tree-preorder-traversal.cpp └── README.md ├── 0145-binary-tree-postorder-traversal ├── 0145-binary-tree-postorder-traversal.cpp ├── 0145-binary-tree-postorder-traversal.java └── README.md ├── 0148-sort-list ├── 0148-sort-list.cpp └── README.md ├── 0151-reverse-words-in-a-string ├── 0151-reverse-words-in-a-string.java └── README.md ├── 0153-find-minimum-in-rotated-sorted-array ├── 0153-find-minimum-in-rotated-sorted-array.cpp └── README.md ├── 0160-intersection-of-two-linked-lists ├── 0160-intersection-of-two-linked-lists.cpp ├── 0160-intersection-of-two-linked-lists.java └── README.md ├── 0169-majority-element ├── 0169-majority-element.cpp ├── 0169-majority-element.java └── README.md ├── 0175-combine-two-tables ├── 0175-combine-two-tables.sql └── README.md ├── 0189-rotate-array ├── 0189-rotate-array.cpp └── README.md ├── 0199-binary-tree-right-side-view ├── 0199-binary-tree-right-side-view.cpp └── README.md ├── 0200-number-of-islands ├── 0200-number-of-islands.cpp ├── 0200-number-of-islands.java └── README.md ├── 0202-happy-number ├── 0202-happy-number.cpp └── README.md ├── 0203-remove-linked-list-elements ├── 0203-remove-linked-list-elements.cpp └── README.md ├── 0205-isomorphic-strings ├── 0205-isomorphic-strings.cpp └── README.md ├── 0206-reverse-linked-list ├── 0206-reverse-linked-list.cpp └── README.md ├── 0215-kth-largest-element-in-an-array ├── 0215-kth-largest-element-in-an-array.cpp └── README.md ├── 0217-contains-duplicate ├── 0217-contains-duplicate.cpp └── README.md ├── 0222-count-complete-tree-nodes ├── 0222-count-complete-tree-nodes.cpp └── README.md ├── 0225-implement-stack-using-queues ├── 0225-implement-stack-using-queues.cpp └── README.md ├── 0226-invert-binary-tree ├── 0226-invert-binary-tree.cpp ├── 0226-invert-binary-tree.java └── README.md ├── 0228-summary-ranges ├── 0228-summary-ranges.java └── README.md ├── 0229-majority-element-ii ├── 0229-majority-element-ii.cpp └── README.md ├── 0230-kth-smallest-element-in-a-bst ├── 0230-kth-smallest-element-in-a-bst.cpp └── README.md ├── 0232-implement-queue-using-stacks ├── 0232-implement-queue-using-stacks.cpp └── README.md ├── 0234-palindrome-linked-list ├── 0234-palindrome-linked-list.cpp ├── 0234-palindrome-linked-list.java └── README.md ├── 0235-lowest-common-ancestor-of-a-binary-search-tree ├── 0235-lowest-common-ancestor-of-a-binary-search-tree.cpp └── README.md ├── 0236-lowest-common-ancestor-of-a-binary-tree ├── 0236-lowest-common-ancestor-of-a-binary-tree.cpp └── README.md ├── 0237-delete-node-in-a-linked-list ├── 0237-delete-node-in-a-linked-list.java └── README.md ├── 0238-product-of-array-except-self ├── 0238-product-of-array-except-self.cpp └── README.md ├── 0242-valid-anagram ├── 0242-valid-anagram.cpp └── README.md ├── 0257-binary-tree-paths ├── 0257-binary-tree-paths.cpp └── README.md ├── 0258-add-digits ├── 0258-add-digits.cpp └── README.md ├── 0268-missing-number ├── 0268-missing-number.cpp └── README.md ├── 0283-move-zeroes ├── 0283-move-zeroes.cpp └── README.md ├── 0287-find-the-duplicate-number ├── 0287-find-the-duplicate-number.cpp └── README.md ├── 0303-range-sum-query-immutable ├── 0303-range-sum-query-immutable.cpp └── README.md ├── 0326-power-of-three ├── 0326-power-of-three.cpp └── README.md ├── 0328-odd-even-linked-list ├── 0328-odd-even-linked-list.cpp └── README.md ├── 0338-counting-bits ├── 0338-counting-bits.cpp └── README.md ├── 0344-reverse-string ├── 0344-reverse-string.cpp ├── 0344-reverse-string.java └── README.md ├── 0347-top-k-frequent-elements ├── 0347-top-k-frequent-elements.cpp └── README.md ├── 0349-intersection-of-two-arrays ├── 0349-intersection-of-two-arrays.cpp └── README.md ├── 0387-first-unique-character-in-a-string ├── 0387-first-unique-character-in-a-string.cpp └── README.md ├── 0392-is-subsequence ├── 0392-is-subsequence.cpp └── README.md ├── 0434-number-of-segments-in-a-string ├── 0434-number-of-segments-in-a-string.java └── README.md ├── 0442-find-all-duplicates-in-an-array ├── 0442-find-all-duplicates-in-an-array.cpp └── README.md ├── 0448-find-all-numbers-disappeared-in-an-array ├── 0448-find-all-numbers-disappeared-in-an-array.cs └── README.md ├── 0450-delete-node-in-a-bst ├── 0450-delete-node-in-a-bst.cpp └── README.md ├── 0451-sort-characters-by-frequency ├── 0451-sort-characters-by-frequency.cpp └── README.md ├── 0476-number-complement ├── 0476-number-complement.java └── README.md ├── 0485-max-consecutive-ones ├── 0485-max-consecutive-ones.cpp └── README.md ├── 0491-non-decreasing-subsequences ├── 0491-non-decreasing-subsequences.java └── README.md ├── 0493-reverse-pairs ├── 0493-reverse-pairs.cpp └── README.md ├── 0509-fibonacci-number ├── 0509-fibonacci-number.cpp └── README.md ├── 0532-k-diff-pairs-in-an-array ├── 0532-k-diff-pairs-in-an-array.cpp └── README.md ├── 0540-single-element-in-a-sorted-array ├── 0540-single-element-in-a-sorted-array.cpp └── README.md ├── 0543-diameter-of-binary-tree ├── 0543-diameter-of-binary-tree.cpp └── README.md ├── 0560-subarray-sum-equals-k ├── 0560-subarray-sum-equals-k.cpp └── README.md ├── 0583-delete-operation-for-two-strings ├── 0583-delete-operation-for-two-strings.cpp └── README.md ├── 0637-average-of-levels-in-binary-tree ├── 0637-average-of-levels-in-binary-tree.cpp └── README.md ├── 0645-set-mismatch ├── 0645-set-mismatch.cpp └── README.md ├── 0661-image-smoother ├── 0661-image-smoother.java └── README.md ├── 0662-maximum-width-of-binary-tree ├── 0662-maximum-width-of-binary-tree.cpp └── README.md ├── 0700-search-in-a-binary-search-tree ├── 0700-search-in-a-binary-search-tree.cpp └── README.md ├── 0704-binary-search ├── 0704-binary-search.cpp └── README.md ├── 0744-find-smallest-letter-greater-than-target ├── 0744-find-smallest-letter-greater-than-target.cpp └── README.md ├── 0746-min-cost-climbing-stairs ├── 0746-min-cost-climbing-stairs.cpp └── README.md ├── 0784-insert-into-a-binary-search-tree ├── 0784-insert-into-a-binary-search-tree.cpp └── README.md ├── 0796-rotate-string ├── 0796-rotate-string.cpp ├── 0796-rotate-string.java └── README.md ├── 0875-koko-eating-bananas ├── 0875-koko-eating-bananas.cpp └── README.md ├── 0876-middle-of-the-linked-list ├── 0876-middle-of-the-linked-list.cpp └── README.md ├── 0977-squares-of-a-sorted-array ├── 0977-squares-of-a-sorted-array.cpp └── README.md ├── 0980-unique-paths-iii ├── 0980-unique-paths-iii.java └── README.md ├── 1011-capacity-to-ship-packages-within-d-days ├── 1011-capacity-to-ship-packages-within-d-days.cpp └── README.md ├── 1021-remove-outermost-parentheses ├── 1021-remove-outermost-parentheses.cpp └── README.md ├── 1025-divisor-game ├── 1025-divisor-game.cpp └── README.md ├── 1143-longest-common-subsequence ├── 1143-longest-common-subsequence.cpp └── README.md ├── 1217-minimum-cost-to-move-chips-to-the-same-position ├── 1217-minimum-cost-to-move-chips-to-the-same-position.cpp └── README.md ├── 1283-find-the-smallest-divisor-given-a-threshold ├── 1283-find-the-smallest-divisor-given-a-threshold.cpp └── README.md ├── 1482-minimum-number-of-days-to-make-m-bouquets ├── 1482-minimum-number-of-days-to-make-m-bouquets.cpp └── README.md ├── 1539-kth-missing-positive-number ├── 1539-kth-missing-positive-number.cpp └── README.md ├── 1614-maximum-nesting-depth-of-the-parentheses ├── 1614-maximum-nesting-depth-of-the-parentheses.cpp └── README.md ├── 1684-count-the-number-of-consistent-strings ├── 1684-count-the-number-of-consistent-strings.cpp └── README.md ├── 1721-swapping-nodes-in-a-linked-list ├── 1721-swapping-nodes-in-a-linked-list.cpp └── README.md ├── 1752-check-if-array-is-sorted-and-rotated ├── 1752-check-if-array-is-sorted-and-rotated.java └── README.md ├── 1903-largest-odd-number-in-string ├── 1903-largest-odd-number-in-string.cpp └── README.md ├── 2095-delete-the-middle-node-of-a-linked-list ├── 2095-delete-the-middle-node-of-a-linked-list.cpp └── README.md ├── 2149-rearrange-array-elements-by-sign ├── 2149-rearrange-array-elements-by-sign.cpp └── README.md ├── 2216-delete-the-middle-node-of-a-linked-list ├── 2216-delete-the-middle-node-of-a-linked-list.cpp └── README.md ├── 2305-fair-distribution-of-cookies ├── 2305-fair-distribution-of-cookies.cpp └── README.md ├── 2461-maximum-sum-of-distinct-subarrays-with-length-k ├── 2461-maximum-sum-of-distinct-subarrays-with-length-k.cpp └── README.md ├── 2914-minimum-number-of-changes-to-make-binary-string-beautiful ├── 2914-minimum-number-of-changes-to-make-binary-string-beautiful.cpp └── README.md ├── README.md └── stats.json /0001-two-sum/0001-two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | map mp; 5 | for(int i=0;ival; 23 | l1 = l1->next; 24 | } 25 | if(l2!=NULL){ 26 | sum+=l2->val; 27 | l2 = l2->next; 28 | } 29 | 30 | sum += carry; 31 | carry = sum/10; 32 | ListNode* newNode = new ListNode(sum%10); 33 | temp->next = newNode; 34 | temp = temp->next; 35 | } 36 | return dummy->next; 37 | } 38 | }; -------------------------------------------------------------------------------- /0003-longest-substring-without-repeating-characters/0003-longest-substring-without-repeating-characters.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLongestSubstring(String str) { 3 | if(str.length()==0) 4 | return 0; 5 | int maxans = Integer.MIN_VALUE; 6 | Set < Character > set = new HashSet < > (); 7 | int l = 0; 8 | for (int r = 0; r < str.length(); r++) // outer loop for traversing the string 9 | { 10 | if (set.contains(str.charAt(r))) //if duplicate element is found 11 | { 12 | while (l < r && set.contains(str.charAt(r))) { 13 | set.remove(str.charAt(l)); 14 | l++; 15 | } 16 | } 17 | set.add(str.charAt(r)); 18 | maxans = Math.max(maxans, r - l + 1); 19 | } 20 | return maxans; 21 | } 22 | } -------------------------------------------------------------------------------- /0007-reverse-integer/0007-reverse-integer.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | String n = ""+x; 4 | String rev = ""; 5 | if(x>0){ 6 | rev = new StringBuilder(n).reverse().toString(); 7 | }else if(x<0){ 8 | rev = "-"+new StringBuilder(n.substring(1)).reverse().toString(); 9 | }else{ 10 | return 0; 11 | } 12 | try { 13 | return Integer.parseInt(rev); 14 | }catch (Exception e) { 15 | return 0; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /0007-reverse-integer/README.md: -------------------------------------------------------------------------------- 1 |

7. Reverse Integer

Medium


Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

2 | 3 |

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: x = 123
10 | Output: 321
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: x = -123
17 | Output: -321
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: x = 120
24 | Output: 21
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • -231 <= x <= 231 - 1
  • 32 |
33 | -------------------------------------------------------------------------------- /0009-palindrome-number/0009-palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x < 0) return false; 5 | long rev = 0; 6 | long num =x; 7 | while(x > 0){ 8 | int rem = x%10; 9 | rev = rev*10+rem; 10 | x /= 10; 11 | } 12 | return (num==rev); 13 | } 14 | }; -------------------------------------------------------------------------------- /0013-roman-to-integer/0013-roman-to-integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | int ans = 0; 5 | int n = s.length(); 6 | for(int i=0;i< n;i++){ 7 | if(s[i]=='I'){ // 1 8 | if(i+1 < n && s[i+1] == 'V' || s[i+1]=='X'){ 9 | ans -= 1; 10 | }else{ 11 | ans += 1; 12 | } 13 | }if(s[i]=='V'){ // 5 14 | ans += 5; 15 | }if(s[i]=='X'){ // 10 16 | if(i+1 < n && s[i+1]=='L' || s[i+1]=='C'){ 17 | ans -= 10; 18 | }else{ 19 | ans += 10; 20 | } 21 | }if(s[i]=='L'){ // 50 22 | ans += 50; 23 | }if(s[i]=='C'){ //100 24 | if(i+1 < n && s[i+1]=='D' || s[i+1]=='M'){ 25 | ans -= 100; 26 | }else{ 27 | ans += 100; 28 | } 29 | }if(s[i]=='D'){ 30 | ans += 500; 31 | }if(s[i]=='M'){ 32 | ans += 1000; 33 | } 34 | } 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /0014-longest-common-prefix/0014-longest-common-prefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | StringBuilder ans = new StringBuilder(); 4 | Arrays.sort(strs); 5 | int n = strs.length; 6 | char []first = strs[0].toCharArray(); 7 | char []last = strs[n-1].toCharArray(); 8 | 9 | for(int i = 0 ; i< first.length ; i++){ 10 | if(first[i] != last[i]){ 11 | break; 12 | }else{ 13 | ans.append(first[i]); 14 | } 15 | } 16 | return ans.toString(); 17 | } 18 | } -------------------------------------------------------------------------------- /0014-longest-common-prefix/README.md: -------------------------------------------------------------------------------- 1 |

14. Longest Common Prefix

Easy


Write a function to find the longest common prefix string amongst an array of strings.

2 | 3 |

If there is no common prefix, return an empty string "".

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: strs = ["flower","flow","flight"]
10 | Output: "fl"
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: strs = ["dog","racecar","car"]
17 | Output: ""
18 | Explanation: There is no common prefix among the input strings.
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= strs.length <= 200
  • 26 |
  • 0 <= strs[i].length <= 200
  • 27 |
  • strs[i] consists of only lowercase English letters if it is non-empty.
  • 28 |
29 | -------------------------------------------------------------------------------- /0015-3sum/0015-3sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> threeSum(int[] arr) { 3 | if(arr == null || arr.length < 3) return new ArrayList<>(); 4 | Arrays.sort(arr); 5 | Set> res = new HashSet<>(); 6 | 7 | for(int i=0;i(res); 25 | } 26 | } -------------------------------------------------------------------------------- /0017-letter-combinations-of-a-phone-number/0017-letter-combinations-of-a-phone-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(vector& ans, string op, int i, string mp[], string d){ 4 | // base case 5 | if(i >= d.length()){ 6 | ans.push_back(op); 7 | return; 8 | } 9 | 10 | int num = d[i]-'0'; 11 | string val = mp[num]; 12 | 13 | for(int j=0;j letterCombinations(string digits) { 21 | if(digits.length()==0) return {}; 22 | vector ans; 23 | string op; 24 | int i = 0; 25 | string mapping[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 26 | solve(ans,op,i,mapping,digits); 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /0018-4sum/README.md: -------------------------------------------------------------------------------- 1 |

18. 4Sum

Medium


Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

2 | 3 |
    4 |
  • 0 <= a, b, c, d < n
  • 5 |
  • a, b, c, and d are distinct.
  • 6 |
  • nums[a] + nums[b] + nums[c] + nums[d] == target
  • 7 |
8 | 9 |

You may return the answer in any order.

10 | 11 |

 

12 |

Example 1:

13 | 14 |
15 | Input: nums = [1,0,-1,0,-2,2], target = 0
16 | Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
17 | 
18 | 19 |

Example 2:

20 | 21 |
22 | Input: nums = [2,2,2,2,2], target = 8
23 | Output: [[2,2,2,2]]
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • 1 <= nums.length <= 200
  • 31 |
  • -109 <= nums[i] <= 109
  • 32 |
  • -109 <= target <= 109
  • 33 |
34 | -------------------------------------------------------------------------------- /0019-remove-nth-node-from-end-of-list/0019-remove-nth-node-from-end-of-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeNthFromEnd(ListNode* head, int n) { 14 | ListNode* dummy = new ListNode(); 15 | dummy->next = head; 16 | ListNode* s = dummy; 17 | ListNode* f = dummy; 18 | 19 | for(int i=0;inext; 21 | } 22 | 23 | while(f->next!=NULL){ 24 | f = f->next; 25 | s = s->next; 26 | } 27 | 28 | ListNode* delNode = s->next; 29 | s->next = s->next->next; 30 | delete delNode; 31 | return dummy->next; 32 | } 33 | }; -------------------------------------------------------------------------------- /0019-remove-nth-node-from-end-of-list/README.md: -------------------------------------------------------------------------------- 1 |

19. Remove Nth Node From End of List

Medium


Given the head of a linked list, remove the nth node from the end of the list and return its head.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,2,3,4,5], n = 2
 8 | Output: [1,2,3,5]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [1], n = 1
15 | Output: []
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: head = [1,2], n = 1
22 | Output: [1]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the list is sz.
  • 30 |
  • 1 <= sz <= 30
  • 31 |
  • 0 <= Node.val <= 100
  • 32 |
  • 1 <= n <= sz
  • 33 |
34 | 35 |

 

36 |

Follow up: Could you do this in one pass?

37 | -------------------------------------------------------------------------------- /0020-valid-parentheses/0020-valid-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack st; 5 | for(int i=0;i arr; 15 | ListNode* t1 = list1; 16 | ListNode* t2 = list2; 17 | while(t1!=NULL){ 18 | arr.push_back(t1->val); 19 | t1 = t1->next; 20 | } 21 | while(t2!=NULL){ 22 | arr.push_back(t2->val); 23 | t2 = t2->next; 24 | } 25 | sort(arr.begin(),arr.end()); 26 | 27 | if (arr.empty()) return nullptr; 28 | 29 | ListNode* newNode = new ListNode(arr[0]); 30 | ListNode* temp = newNode; 31 | for(int i=1;inext = new ListNode(arr[i]); 33 | temp = temp->next; 34 | } 35 | return newNode; 36 | } 37 | }; -------------------------------------------------------------------------------- /0024-swap-nodes-in-pairs/0024-swap-nodes-in-pairs.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* swapPairs(ListNode* head) { 14 | if(head==NULL || head->next==NULL){ 15 | return head; 16 | } 17 | ListNode* temp = head; 18 | vector arr; 19 | while(temp!=NULL){ 20 | arr.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | 24 | for(int i=0;ival = arr[i]; 30 | temp = temp->next; 31 | } 32 | return head; 33 | } 34 | }; -------------------------------------------------------------------------------- /0025-reverse-nodes-in-k-group/0025-reverse-nodes-in-k-group.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseKGroup(ListNode* head, int k) { 14 | ListNode* t = head; 15 | vector arr; 16 | while(t!=NULL){ 17 | arr.push_back(t->val); 18 | t = t->next; 19 | } 20 | 21 | for(int i=0;i+k<=arr.size();i=i+k){ 22 | reverse(arr.begin()+i,arr.begin()+i+k); 23 | } 24 | 25 | t = head; 26 | for(int i=0;ival = arr[i]; 28 | t = t->next; 29 | } 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/0026-remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | set s; 5 | for(int i=0;i h) { 6 | return -1; 7 | } 8 | for (int i = 0; i <= h - n; i++) { 9 | if (haystack.charAt(i) == needle.charAt(0)) { 10 | if (haystack.substring(i, i + n).equals(needle)) { 11 | return i; 12 | } 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /0028-find-the-index-of-the-first-occurrence-in-a-string/README.md: -------------------------------------------------------------------------------- 1 |

28. Find the Index of the First Occurrence in a String

Easy


Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: haystack = "sadbutsad", needle = "sad"
 8 | Output: 0
 9 | Explanation: "sad" occurs at index 0 and 6.
10 | The first occurrence is at index 0, so we return 0.
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: haystack = "leetcode", needle = "leeto"
17 | Output: -1
18 | Explanation: "leeto" did not occur in "leetcode", so we return -1.
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= haystack.length, needle.length <= 104
  • 26 |
  • haystack and needle consist of only lowercase English characters.
  • 27 |
28 | -------------------------------------------------------------------------------- /0031-next-permutation/0031-next-permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& A) { 4 | int n = A.size(); // size of the array. 5 | 6 | // Step 1: Find the break point: 7 | int ind = -1; // break point 8 | for (int i = n - 2; i >= 0; i--) { 9 | if (A[i] < A[i + 1]) { 10 | // index i is the break point 11 | ind = i; 12 | break; 13 | } 14 | } 15 | 16 | // If break point does not exist: 17 | if (ind == -1) { 18 | // reverse the whole array: 19 | reverse(A.begin(), A.end()); 20 | return; 21 | } 22 | 23 | // Step 2: Find the next greater element 24 | // and swap it with arr[ind]: 25 | 26 | for (int i = n - 1; i > ind; i--) { 27 | if (A[i] > A[ind]) { 28 | swap(A[i], A[ind]); 29 | break; 30 | } 31 | } 32 | 33 | // Step 3: reverse the right half: 34 | reverse(A.begin() + ind + 1, A.end()); 35 | } 36 | }; -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | while(l<=r){ 7 | int m = l+(r-l)/2; 8 | if(nums[m] == target){ 9 | return m; 10 | } 11 | if(nums[m] >= nums[l]){ // left 12 | if(nums[l] <= target && nums[m] >= target){ 13 | r = m-1; 14 | }else{ 15 | l = m+1; 16 | } 17 | }else{ // right 18 | if(nums[m] <= target && nums[r] >= target){ 19 | l = m+1; 20 | }else{ 21 | r = m-1; 22 | } 23 | } 24 | } 25 | return -1; 26 | } 27 | }; -------------------------------------------------------------------------------- /0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int l = 0; 4 | int r = nums.length-1; 5 | while(l<=r){ 6 | int m = l+(r-l)/2; 7 | if(nums[m] == target){ 8 | return m; 9 | } 10 | if(nums[l] <= nums[m]){ 11 | if(nums[l] <= target && target < nums[m]){ 12 | r = m-1; 13 | }else{ 14 | l = m+1; 15 | } 16 | } 17 | else{ 18 | if(nums[m] < target && target<= nums[r]){ 19 | l = m+1; 20 | }else{ 21 | r = m-1; 22 | } 23 | } 24 | } 25 | return -1; 26 | } 27 | } -------------------------------------------------------------------------------- /0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | int st = -1; 7 | int end = -1; 8 | // for search first occ - 9 | while(l <= r){ 10 | int m = l+(r-l)/2; 11 | if(nums[m]==target){ 12 | st = m; 13 | r = m-1; 14 | }else if(nums[m] < target){ 15 | l = m+1; 16 | }else{ 17 | r = m-1; 18 | } 19 | } 20 | // reset value - 21 | l = 0; 22 | r = nums.size() - 1; 23 | // for search last occ - 24 | while(l <= r){ 25 | int m = l+(r-l)/2; 26 | if(nums[m]==target){ 27 | end = m; 28 | l = m+1; 29 | }else if(nums[m] < target){ 30 | l = m+1; 31 | }else{ 32 | r = m-1; 33 | } 34 | } 35 | return {st,end}; 36 | } 37 | }; -------------------------------------------------------------------------------- /0035-search-insert-position/0035-search-insert-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | if(nums[r] < target) return r+1; 7 | while(l <= r){ 8 | int m = l+(r-l)/2; 9 | if(nums[m] == target) return m; 10 | else if(nums[m] < target){ 11 | l = m+1; 12 | }else{ 13 | r = m-1; 14 | } 15 | } 16 | return l; 17 | } 18 | }; -------------------------------------------------------------------------------- /0035-search-insert-position/README.md: -------------------------------------------------------------------------------- 1 |

35. Search Insert Position

Easy


Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [1,3,5,6], target = 5
10 | Output: 2
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: nums = [1,3,5,6], target = 2
17 | Output: 1
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: nums = [1,3,5,6], target = 7
24 | Output: 4
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= nums.length <= 104
  • 32 |
  • -104 <= nums[i] <= 104
  • 33 |
  • nums contains distinct values sorted in ascending order.
  • 34 |
  • -104 <= target <= 104
  • 35 |
36 | -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int num = 1; 5 | sort(nums.begin(),nums.end()); 6 | 7 | for(int i=0;i0){ 9 | num++; 10 | } 11 | } 12 | return num; 13 | } 14 | }; -------------------------------------------------------------------------------- /0041-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 |

41. First Missing Positive

Hard


Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

2 | 3 |

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [1,2,0]
10 | Output: 3
11 | Explanation: The numbers in the range [1,2] are all in the array.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [3,4,-1,1]
18 | Output: 2
19 | Explanation: 1 is in the array but 2 is missing.
20 | 
21 | 22 |

Example 3:

23 | 24 |
25 | Input: nums = [7,8,9,11,12]
26 | Output: 1
27 | Explanation: The smallest positive integer 1 is missing.
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • 1 <= nums.length <= 105
  • 35 |
  • -231 <= nums[i] <= 231 - 1
  • 36 |
37 | -------------------------------------------------------------------------------- /0042-trapping-rain-water/0042-trapping-rain-water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n = height.size(); 5 | 6 | vector l(n); 7 | vector r(n); 8 | 9 | l[0] = height[0]; 10 | for(int i=1;i=0;i--){ 16 | r[i] = max(r[i+1],height[i]); 17 | } 18 | 19 | int ans = 0; 20 | for(int i=0;i42. Trapping Rain Water

Hard


Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
 8 | Output: 6
 9 | Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: height = [4,2,0,3,2,5]
16 | Output: 9
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • n == height.length
  • 24 |
  • 1 <= n <= 2 * 104
  • 25 |
  • 0 <= height[i] <= 105
  • 26 |
27 | -------------------------------------------------------------------------------- /0046-permutations/0046-permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void solve(vectorn , vector>&a , int idx){ 5 | if(idx >= n.size()){ 6 | a.push_back(n); 7 | return; 8 | } 9 | 10 | for(int i= idx;i> permute(vector& nums) { 18 | vector> ans; 19 | int i = 0; 20 | solve(nums,ans,i); 21 | return ans; 22 | 23 | } 24 | }; -------------------------------------------------------------------------------- /0046-permutations/README.md: -------------------------------------------------------------------------------- 1 |

46. Permutations

Medium


Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [1,2,3]
 6 | Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
 7 | 

Example 2:

8 |
Input: nums = [0,1]
 9 | Output: [[0,1],[1,0]]
10 | 

Example 3:

11 |
Input: nums = [1]
12 | Output: [[1]]
13 | 
14 |

 

15 |

Constraints:

16 | 17 |
    18 |
  • 1 <= nums.length <= 6
  • 19 |
  • -10 <= nums[i] <= 10
  • 20 |
  • All the integers of nums are unique.
  • 21 |
22 | -------------------------------------------------------------------------------- /0048-rotate-image/0048-rotate-image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | for(int i=0;i> groupAnagrams(vector& strs) { 4 | unordered_map> anagramMap; 5 | 6 | for (string s : strs) { 7 | string sortedStr = s; 8 | sort(sortedStr.begin(), sortedStr.end()); 9 | anagramMap[sortedStr].push_back(s); 10 | } 11 | 12 | vector> result; 13 | for (auto& pair : anagramMap) { 14 | result.push_back(pair.second); 15 | } 16 | 17 | return result; 18 | 19 | } 20 | }; -------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) { 4 | return pow(x,n); 5 | } 6 | }; -------------------------------------------------------------------------------- /0050-powx-n/0050-powx-n.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double myPow(double x, int n) { 3 | double ans = 1.0; 4 | long power = n; // Convert n to long to avoid overflow 5 | 6 | if (power < 0) { 7 | power = -power; // Make it positive 8 | x = 1 / x; // Take reciprocal 9 | } 10 | 11 | while (power > 0) { 12 | if (power % 2 == 1) { 13 | ans = ans * x; 14 | power = power - 1; 15 | } else { 16 | x = x * x; 17 | power = power / 2; 18 | } 19 | } 20 | return ans; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /0050-powx-n/README.md: -------------------------------------------------------------------------------- 1 |

50. Pow(x, n)

Medium


Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: x = 2.00000, n = 10
 8 | Output: 1024.00000
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: x = 2.10000, n = 3
15 | Output: 9.26100
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: x = 2.00000, n = -2
22 | Output: 0.25000
23 | Explanation: 2-2 = 1/22 = 1/4 = 0.25
24 | 
25 | 26 |

 

27 |

Constraints:

28 | 29 |
    30 |
  • -100.0 < x < 100.0
  • 31 |
  • -231 <= n <= 231-1
  • 32 |
  • n is an integer.
  • 33 |
  • Either x is not zero or n > 0.
  • 34 |
  • -104 <= xn <= 104
  • 35 |
36 | -------------------------------------------------------------------------------- /0053-maximum-subarray/0053-maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int n = nums.size(); 5 | int ans = INT_MIN; 6 | int sum = 0; 7 | for(int i=0;i ans) ans = sum; 10 | if(sum < 0) sum = 0; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /0054-spiral-matrix/0054-spiral-matrix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | int rowStart = 0 , colStart = 0; 5 | int rowEnd = matrix.size()-1; 6 | int colEnd = matrix[0].size()-1; 7 | 8 | vector ans; 9 | 10 | while(rowStart <= rowEnd && colStart <= colEnd){ 11 | 12 | for(int i=colStart ; i<=colEnd ; i++){ 13 | ans.push_back(matrix[rowStart][i]); 14 | } 15 | rowStart++; 16 | for(int i = rowStart ; i <= rowEnd ; i++){ 17 | ans.push_back(matrix[i][colEnd]); 18 | } 19 | colEnd--; 20 | if(rowStart <= rowEnd){ 21 | for(int i=colEnd ; i >= colStart ; i--){ 22 | ans.push_back(matrix[rowEnd][i]); 23 | } 24 | } 25 | rowEnd--; 26 | if(colStart <= colEnd){ 27 | for(int i=rowEnd ; i>= rowStart ; i-- ){ 28 | ans.push_back(matrix[i][colStart]); 29 | } 30 | } 31 | colStart++; 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /0054-spiral-matrix/README.md: -------------------------------------------------------------------------------- 1 |

54. Spiral Matrix

Medium


Given an m x n matrix, return all elements of the matrix in spiral order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
 8 | Output: [1,2,3,6,9,8,7,4,5]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
15 | Output: [1,2,3,4,8,12,11,10,9,5,6,7]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • m == matrix.length
  • 23 |
  • n == matrix[i].length
  • 24 |
  • 1 <= m, n <= 10
  • 25 |
  • -100 <= matrix[i][j] <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0056-merge-intervals/0056-merge-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | vector> ans; 5 | sort(intervals.begin(),intervals.end()); 6 | int n = intervals.size(); 7 | 8 | for(int i=0;i ans.back()[1]){ 10 | ans.push_back(intervals[i]); 11 | }else{ 12 | ans.back()[1] = max(ans.back()[1],intervals[i][1]); 13 | } 14 | } 15 | return ans; 16 | } 17 | }; -------------------------------------------------------------------------------- /0056-merge-intervals/README.md: -------------------------------------------------------------------------------- 1 |

56. Merge Intervals

Medium


Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
 8 | Output: [[1,6],[8,10],[15,18]]
 9 | Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: intervals = [[1,4],[4,5]]
16 | Output: [[1,5]]
17 | Explanation: Intervals [1,4] and [4,5] are considered overlapping.
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= intervals.length <= 104
  • 25 |
  • intervals[i].length == 2
  • 26 |
  • 0 <= starti <= endi <= 104
  • 27 |
28 | -------------------------------------------------------------------------------- /0058-length-of-last-word/0058-length-of-last-word.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLastWord(String s) { 3 | 4 | String str = s.trim(); 5 | int len = str.length(); 6 | 7 | int count = 0; 8 | for(int i=len-1 ; i>=0 ; i--){ 9 | if(str.charAt(i) != ' '){ 10 | count++; 11 | } 12 | else{ 13 | break; 14 | } 15 | } 16 | return count; 17 | } 18 | } -------------------------------------------------------------------------------- /0061-rotate-list/0061-rotate-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* rotateRight(ListNode* head, int k) { 14 | if(head==NULL || head->next==NULL || k==0) return head; 15 | 16 | ListNode* t = head; 17 | int len = 1; 18 | while(t->next){ 19 | len++; 20 | t = t->next; 21 | } 22 | 23 | t->next = head; 24 | k = k%len; 25 | k = len-k; 26 | 27 | while(k--) t = t->next; 28 | 29 | head = t->next; 30 | t->next = NULL; 31 | 32 | return head; 33 | } 34 | }; -------------------------------------------------------------------------------- /0061-rotate-list/README.md: -------------------------------------------------------------------------------- 1 |

61. Rotate List

Medium


Given the head of a linked list, rotate the list to the right by k places.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,2,3,4,5], k = 2
 8 | Output: [4,5,1,2,3]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [0,1,2], k = 4
15 | Output: [2,0,1]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is in the range [0, 500].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
  • 0 <= k <= 2 * 109
  • 25 |
26 | -------------------------------------------------------------------------------- /0062-unique-paths/0062-unique-paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPath(int m,int n,int i,int j,vector>& dp){ 4 | // Golas ----- 5 | if(i==m-1 && j==n-1) return 1; 6 | // inValid conditions ----- 7 | if(i< 0 || j < 0 || i>=m || j>=n) return 0; 8 | // check down , right ----- 9 | 10 | //use DP 11 | if(dp[i][j]!=-1) return dp[i][j]; 12 | return dp[i][j] = countPath(m,n,i+1,j,dp) + countPath(m,n,i,j+1,dp); 13 | } 14 | 15 | int uniquePaths(int m, int n) { 16 | vector> dp (m , vector(n,-1)); 17 | int ans = countPath(m,n,0,0 ,dp); 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /0070-climbing-stairs/0070-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | vector dp(46,-1); 2 | class Solution { 3 | public: 4 | int climbStairs(int n) { 5 | if(n==0 || n==1 || n==2) return n; 6 | if(dp[n]!=-1) return dp[n]; 7 | return dp[n] = climbStairs(n-1)+climbStairs(n-2); 8 | } 9 | }; -------------------------------------------------------------------------------- /0070-climbing-stairs/README.md: -------------------------------------------------------------------------------- 1 |

70. Climbing Stairs

Easy


You are climbing a staircase. It takes n steps to reach the top.

2 | 3 |

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 2
10 | Output: 2
11 | Explanation: There are two ways to climb to the top.
12 | 1. 1 step + 1 step
13 | 2. 2 steps
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: n = 3
20 | Output: 3
21 | Explanation: There are three ways to climb to the top.
22 | 1. 1 step + 1 step + 1 step
23 | 2. 1 step + 2 steps
24 | 3. 2 steps + 1 step
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= n <= 45
  • 32 |
33 | -------------------------------------------------------------------------------- /0073-set-matrix-zeroes/0073-set-matrix-zeroes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void setZeroes(vector>& matrix) { 4 | int m = matrix.size(); 5 | int n = matrix[0].size(); 6 | vector row(m,0); // record all rows , where 0 found make the index 1 7 | vector col(n,0); // record all colums , where 0 found make the index 1 8 | for(int i=0;i>& matrix, int target) { 4 | int r = matrix.size(); 5 | int c = matrix[0].size(); 6 | 7 | for(int i=0;i target) h = mid-1; 14 | } 15 | return false; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /0075-sort-colors/0075-sort-colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | // Dutch National Flag Algo 5 | 6 | int low = 0 , mid = 0 , high = nums.size()-1; 7 | 8 | while(mid <= high){ 9 | if(nums[mid]==0){ 10 | swap(nums[low],nums[mid]); 11 | low++; 12 | mid++; 13 | } 14 | else if(nums[mid]==1){ 15 | mid++; 16 | } 17 | else if(nums[mid]==2){ 18 | swap(nums[mid],nums[high]); 19 | high--; 20 | } 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /0075-sort-colors/README.md: -------------------------------------------------------------------------------- 1 |

75. Sort Colors

Medium


Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

2 | 3 |

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

4 | 5 |

You must solve this problem without using the library's sort function.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: nums = [2,0,2,1,1,0]
12 | Output: [0,0,1,1,2,2]
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: nums = [2,0,1]
19 | Output: [0,1,2]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • n == nums.length
  • 27 |
  • 1 <= n <= 300
  • 28 |
  • nums[i] is either 0, 1, or 2.
  • 29 |
30 | 31 |

 

32 |

Follow up: Could you come up with a one-pass algorithm using only constant extra space?

33 | -------------------------------------------------------------------------------- /0079-word-search/0079-word-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPresent(vector> &board,string word,int indx , int i,int j){ 4 | if(indx == word.size()){ 5 | return true; 6 | } 7 | 8 | if(i<0 || j<0 || i>=board.size() || j>=board[0].size() || board[i][j]!=word[indx]){ 9 | return false; 10 | } 11 | 12 | char temp = board[i][j]; 13 | board[i][j] = '#'; 14 | 15 | bool ans = isPresent(board,word,indx+1,i+1,j) || isPresent(board,word,indx+1,i-1,j) 16 | || isPresent(board,word,indx+1,i,j+1) || isPresent(board,word,indx+1,i,j-1); 17 | board[i][j] = temp; 18 | return ans; 19 | } 20 | 21 | bool exist(vector>& board, string word) { 22 | for(int i=0;i= b.length || j>=b[0].length || b[i][j]!= w.charAt(index) || b[i][j] =='$'){ 9 | return false; 10 | } 11 | 12 | char temp = b[i][j]; 13 | b[i][j] = '$'; 14 | // Explore all paths - 15 | boolean ans = findWord(i+1,j,index+1,b,w) || 16 | findWord(i-1,j,index+1,b,w) || 17 | findWord(i,j+1,index+1,b,w) || 18 | findWord(i,j-1,index+1,b,w); 19 | b[i][j] = temp; 20 | return ans; 21 | 22 | } 23 | 24 | public boolean exist(char[][] board, String word) { 25 | for(int i=0 ; i< board.length ; i++){ 26 | for(int j=0; j& nums, int target) { 4 | int l = 0; 5 | int r = nums.size()-1; 6 | while(l <= r){ 7 | int m = l+(r-l)/2; 8 | if(nums[m]==target){ 9 | return true; 10 | } 11 | if(nums[l] == nums[m] && nums[m]==nums[r]){ 12 | l++; 13 | r--; 14 | continue; 15 | } 16 | 17 | if(nums[l] <= nums[m]){ // left 18 | if(nums[l] <= target && target < nums[m]){ 19 | r = m-1; 20 | }else{ 21 | l = m+1; 22 | } 23 | }else{ // right 24 | if(nums[m] < target && target <= nums[r]){ 25 | l = m+1; 26 | }else{ 27 | r = m-1; 28 | } 29 | } 30 | } 31 | return false; 32 | } 33 | }; -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/0083-remove-duplicates-from-sorted-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteDuplicates(ListNode* head) { 14 | ListNode* p = head; 15 | while(p!=NULL && p->next!=NULL){ 16 | if(p->val == p->next->val){ 17 | ListNode* duplicate = p->next; 18 | p->next = p->next->next; 19 | delete duplicate; 20 | }else{ 21 | p = p->next; 22 | } 23 | } 24 | 25 | return head; 26 | } 27 | }; -------------------------------------------------------------------------------- /0083-remove-duplicates-from-sorted-list/README.md: -------------------------------------------------------------------------------- 1 |

83. Remove Duplicates from Sorted List

Easy


Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,1,2]
 8 | Output: [1,2]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [1,1,2,3,3]
15 | Output: [1,2,3]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is in the range [0, 300].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
  • The list is guaranteed to be sorted in ascending order.
  • 25 |
26 | -------------------------------------------------------------------------------- /0088-merge-sorted-array/0088-merge-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | for(int i=0;i& nums, vector>& ans, vector& ds , int idx){ 4 | if(idx==nums.size()){ 5 | if(find(ans.begin(),ans.end(), ds) == ans.end()){ 6 | ans.push_back(ds); 7 | } 8 | return; 9 | } 10 | subsets(nums,ans,ds,idx+1); 11 | ds.push_back(nums[idx]); 12 | subsets(nums,ans,ds,idx+1); 13 | ds.pop_back(); 14 | } 15 | 16 | vector> subsetsWithDup(vector& nums) { 17 | sort(nums.begin(),nums.end()); 18 | vector> ans; 19 | vector ds; 20 | subsets(nums , ans , ds, 0); 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0090-subsets-ii/README.md: -------------------------------------------------------------------------------- 1 |

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [1,2,2]
 8 | Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
 9 | 

Example 2:

10 |
Input: nums = [0]
11 | Output: [[],[0]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 10
  • 18 |
  • -10 <= nums[i] <= 10
  • 19 |
20 | -------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/0094-binary-tree-inorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void inorder(TreeNode* root,vector& ans){ 15 | if(root==NULL){ 16 | return; 17 | } 18 | inorder(root->left,ans); 19 | ans.push_back(root->val); 20 | inorder(root->right,ans); 21 | } 22 | vector inorderTraversal(TreeNode* root) { 23 | vector ans; 24 | inorder(root,ans); 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /0096-unique-binary-search-trees/0096-unique-binary-search-trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int n , vector& dp){ 4 | if(n==0 || n== 1){ 5 | return 1; 6 | } 7 | if(dp[n]!=-1) return dp[n]; 8 | int sum = 0; 9 | for(int i=1;i<=n;i++){ 10 | int ls = solve(i-1 ,dp); 11 | int rs = solve(n-i ,dp); 12 | sum += ls*rs; 13 | } 14 | dp[n] = sum; 15 | return dp[n]; 16 | } 17 | int numTrees(int n) { 18 | vector dp(n+1,-1); 19 | int ans = solve(n,dp); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /0096-unique-binary-search-trees/README.md: -------------------------------------------------------------------------------- 1 |

96. Unique Binary Search Trees

Medium


Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: n = 3
 8 | Output: 5
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: n = 1
15 | Output: 1
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= n <= 19
  • 23 |
24 | -------------------------------------------------------------------------------- /0098-validate-binary-search-tree/0098-validate-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isValid(TreeNode* root,long long min,long long max){ 15 | if(!root) return true; 16 | if(root->val >= max || root->val <= min) return false; 17 | return isValid(root->left,min,root->val) && isValid(root->right,root->val,max); 18 | } 19 | 20 | bool isValidBST(TreeNode* root) { 21 | return isValid(root , LLONG_MIN , LLONG_MAX); 22 | } 23 | }; -------------------------------------------------------------------------------- /0100-same-tree/0100-same-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isSameTree(TreeNode* p, TreeNode* q) { 15 | if(!p && !q) return true; 16 | if(!p || !q) return false; 17 | if(p->val != q->val) return false; 18 | return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); 19 | } 20 | }; -------------------------------------------------------------------------------- /0101-symmetric-tree/0101-symmetric-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isMirror(TreeNode* l,TreeNode* r){ 15 | if(l==NULL && r==NULL) return true; 16 | if(l==NULL || r==NULL) return false; 17 | if(l->val != r->val) return false; 18 | return isMirror(l->left,r->right) && isMirror(l->right,r->left); 19 | 20 | } 21 | bool isSymmetric(TreeNode* root) { 22 | if(root==NULL) return true; 23 | return isMirror(root->left,root->right); 24 | } 25 | }; -------------------------------------------------------------------------------- /0101-symmetric-tree/README.md: -------------------------------------------------------------------------------- 1 |

101. Symmetric Tree

Easy


Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [1,2,2,3,4,4,3]
 8 | Output: true
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: root = [1,2,2,null,3,null,3]
15 | Output: false
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the tree is in the range [1, 1000].
  • 23 |
  • -100 <= Node.val <= 100
  • 24 |
25 | 26 |

 

27 | Follow up: Could you solve it both recursively and iteratively? -------------------------------------------------------------------------------- /0102-binary-tree-level-order-traversal/0102-binary-tree-level-order-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector> levelOrder(TreeNode* root) { 15 | vector> ans; 16 | if(root==NULL) return ans; 17 | 18 | queue q; 19 | q.push(root); 20 | while(!q.empty()){ 21 | int size = q.size(); 22 | vector store; 23 | for(int i=0;ival); 27 | 28 | if(node->left) 29 | q.push(node->left); 30 | 31 | if(node->right) 32 | q.push(node->right); 33 | } 34 | ans.push_back(store); 35 | } 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /0102-binary-tree-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 |

102. Binary Tree Level Order Traversal

Medium


Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [3,9,20,null,null,15,7]
 8 | Output: [[3],[9,20],[15,7]]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: root = [1]
15 | Output: [[1]]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 30 |
  • -1000 <= Node.val <= 1000
  • 31 |
32 | -------------------------------------------------------------------------------- /0103-binary-tree-zigzag-level-order-traversal/0103-binary-tree-zigzag-level-order-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector> zigzagLevelOrder(TreeNode* root) { 15 | vector> ans; 16 | if(root==NULL) return ans; 17 | 18 | queue q; 19 | q.push(root); 20 | int level = 0; 21 | 22 | while(!q.empty()){ 23 | level++; 24 | int size = q.size(); 25 | vector store; 26 | for(int i=0;ival); 30 | 31 | if(node->left){ 32 | q.push(node->left); 33 | } 34 | if(node->right){ 35 | q.push(node->right); 36 | } 37 | } 38 | if(level % 2 == 0){ 39 | reverse(store.begin(),store.end()); 40 | } 41 | ans.push_back(store); 42 | } 43 | return ans; 44 | } 45 | }; -------------------------------------------------------------------------------- /0103-binary-tree-zigzag-level-order-traversal/README.md: -------------------------------------------------------------------------------- 1 |

103. Binary Tree Zigzag Level Order Traversal

Medium


Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [3,9,20,null,null,15,7]
 8 | Output: [[3],[20,9],[15,7]]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: root = [1]
15 | Output: [[1]]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 2000].
  • 30 |
  • -100 <= Node.val <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | if(root==NULL) return 0; 16 | int dpt = 0; 17 | queue q; 18 | q.push(root); 19 | while(!q.empty()){ 20 | dpt++; 21 | int n = q.size(); 22 | for(int i=0;ileft) q.push(node->left); 26 | if(node->right) q.push(node->right); 27 | } 28 | } 29 | return dpt; 30 | } 31 | }; -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/0104-maximum-depth-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int maxDepth(TreeNode root) { 18 | if(root == null) return 0; 19 | 20 | int leftSub = maxDepth(root.left); 21 | int rightSub = maxDepth(root.right); 22 | 23 | return 1+ Math.max(leftSub,rightSub); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /0104-maximum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

104. Maximum Depth of Binary Tree

Easy


Given the root of a binary tree, return its maximum depth.

2 | 3 |

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [3,9,20,null,null,15,7]
10 | Output: 3
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [1,null,2]
17 | Output: 2
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [0, 104].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0108-convert-sorted-array-to-binary-search-tree/0108-convert-sorted-array-to-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* helper(vector &n , int l , int r){ 15 | if(l > r) return NULL; 16 | int m = l+(r-l)/2; 17 | TreeNode* root = new TreeNode(n[m]); 18 | root->left = helper(n,l,m-1); 19 | root->right = helper(n,m+1,r); 20 | return root; 21 | } 22 | TreeNode* sortedArrayToBST(vector& nums) { 23 | int l = 0; 24 | int r = nums.size()-1; 25 | return helper(nums,l,r); 26 | } 27 | }; -------------------------------------------------------------------------------- /0111-minimum-depth-of-binary-tree/0111-minimum-depth-of-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public int minDepth(TreeNode root) { 18 | if(root == null) return 0; 19 | 20 | int leftSub = minDepth(root.left); 21 | int rightSub = minDepth(root.right); 22 | 23 | if(leftSub == 0) return 1+rightSub; 24 | if(rightSub == 0) return 1+leftSub; 25 | 26 | return 1+Math.min(leftSub,rightSub); 27 | } 28 | } -------------------------------------------------------------------------------- /0111-minimum-depth-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

111. Minimum Depth of Binary Tree

Easy


Given a binary tree, find its minimum depth.

2 | 3 |

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

4 | 5 |

Note: A leaf is a node with no children.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: root = [3,9,20,null,null,15,7]
12 | Output: 2
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: root = [2,null,3,null,4,null,5,null,6]
19 | Output: 5
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the tree is in the range [0, 105].
  • 27 |
  • -1000 <= Node.val <= 1000
  • 28 |
29 | -------------------------------------------------------------------------------- /0112-path-sum/0112-path-sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool findSum(TreeNode* root , int sum , int targetSum){ 15 | if(!root) return false; 16 | sum += root->val; 17 | 18 | if(!root->left && !root->right){ 19 | return sum == targetSum; 20 | } 21 | 22 | int l = findSum(root->left,sum,targetSum); 23 | int r = findSum(root->right,sum,targetSum); 24 | return l || r; 25 | 26 | } 27 | bool hasPathSum(TreeNode* root, int targetSum) { 28 | int sum = 0; 29 | bool ans = findSum(root , sum , targetSum); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /0118-pascals-triangle/0118-pascals-triangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long nCr(int r,int c){ 4 | long long res = 1; 5 | for(int i=0;i> generate(int numRows) { 13 | vector> ans; 14 | for(int r=1;r<=numRows;r++){ 15 | vector store; 16 | for(int c=1;c<=r;c++){ 17 | store.push_back(nCr(r-1,c-1)); 18 | } 19 | ans.push_back(store); 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0118-pascals-triangle/README.md: -------------------------------------------------------------------------------- 1 |

118. Pascal's Triangle

Easy


Given an integer numRows, return the first numRows of Pascal's triangle.

2 | 3 |

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

4 | 5 |

 

6 |

Example 1:

7 |
Input: numRows = 5
 8 | Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
 9 | 

Example 2:

10 |
Input: numRows = 1
11 | Output: [[1]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= numRows <= 30
  • 18 |
19 | -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& p) { 4 | int minpri = p[0]; 5 | int mxpro = 0; 6 | 7 | for(int i=1;i p[i]){ 9 | minpri = p[i]; 10 | } 11 | int cost = p[i]-minpri; 12 | mxpro = max(cost,mxpro); 13 | } 14 | return mxpro; 15 | } 16 | }; -------------------------------------------------------------------------------- /0121-best-time-to-buy-and-sell-stock/0121-best-time-to-buy-and-sell-stock.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices){ 3 | int len = prices.length; 4 | int maxProfit = 0; 5 | int minPrice = prices[0]; 6 | 7 | 8 | // for (int i = 0; i < len; i++) { 9 | // for (int j = i + 1; j < len; j++) { 10 | // int profit = prices[j] - prices[i]; 11 | // if (profit > maxProfit) { 12 | // maxProfit = profit; 13 | // } 14 | // } 15 | // } 16 | // return maxProfit; 17 | 18 | 19 | for(int i=0;i prices[i]){ 22 | minPrice = prices[i]; 23 | } 24 | 25 | int profit = prices[i] - minPrice; 26 | 27 | if(profit > maxProfit){ 28 | maxProfit = profit; 29 | } 30 | 31 | } 32 | return maxProfit; 33 | } 34 | } -------------------------------------------------------------------------------- /0124-binary-tree-maximum-path-sum/0124-binary-tree-maximum-path-sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int path(TreeNode *root,int &maxpath){ 15 | if(root==NULL)return 0; 16 | 17 | int left=max(0,path(root->left,maxpath)); 18 | int right=max(0,path(root->right,maxpath)); 19 | maxpath=max(maxpath,root->val+left+right); 20 | return root->val+max(left,right); 21 | } 22 | int maxPathSum(TreeNode* root) { 23 | int maxpath=INT_MIN; 24 | path(root,maxpath); 25 | return maxpath; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /0125-valid-palindrome/0125-valid-palindrome.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s) { 4 | string ans = ""; 5 | for(int i=0;i& nums) { 4 | if(nums.size()==0) return 0; 5 | int longest = 1 , count = 0 , lastsmall = INT_MIN; 6 | sort(nums.begin(),nums.end()); 7 | for(int i=0;i numSet = new TreeSet<>(); 5 | for (int num : nums) { 6 | numSet.add(num); // TreeSet automatically sorts elements and removes duplicates 7 | } 8 | 9 | int longest = 1, count = 0, lastSmall = Integer.MIN_VALUE; 10 | for (int num : numSet) { 11 | if (num - 1 == lastSmall) { 12 | count++; 13 | lastSmall = num; 14 | } else { 15 | count = 1; 16 | lastSmall = num; 17 | } 18 | longest = Math.max(longest, count); 19 | } 20 | return longest; 21 | } 22 | } -------------------------------------------------------------------------------- /0128-longest-consecutive-sequence/README.md: -------------------------------------------------------------------------------- 1 |

128. Longest Consecutive Sequence

Medium


Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

2 | 3 |

You must write an algorithm that runs in O(n) time.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [100,4,200,1,3,2]
10 | Output: 4
11 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [0,3,7,2,5,8,4,6,0,1]
18 | Output: 9
19 | 
20 | 21 |

Example 3:

22 | 23 |
24 | Input: nums = [1,0,1,2]
25 | Output: 3
26 | 
27 | 28 |

 

29 |

Constraints:

30 | 31 |
    32 |
  • 0 <= nums.length <= 105
  • 33 |
  • -109 <= nums[i] <= 109
  • 34 |
35 | -------------------------------------------------------------------------------- /0131-palindrome-partitioning/0131-palindrome-partitioning.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(string s, int st , int end){ 4 | while(st <= end){ 5 | if(s[st++]!=s[end--]) return false; 6 | } 7 | return true; 8 | } 9 | 10 | void fun(string s , int idx , vector& path, vector>& ans){ 11 | if(s.length()==idx){ 12 | ans.push_back(path); 13 | return; 14 | } 15 | for(int i=idx;i> partition(string s) { 25 | vector> ans; 26 | vector path; 27 | fun(s,0,path,ans); 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /0131-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 |

131. Palindrome Partitioning

Medium


Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "aab"
 6 | Output: [["a","a","b"],["aa","b"]]
 7 | 

Example 2:

8 |
Input: s = "a"
 9 | Output: [["a"]]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= s.length <= 16
  • 16 |
  • s contains only lowercase English letters.
  • 17 |
18 | -------------------------------------------------------------------------------- /0135-candy/0135-candy.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int candy(vector &ratings) 4 | { 5 | int size=ratings.size(); 6 | if(size<=1) 7 | return size; 8 | vector num(size,1); 9 | for (int i = 1; i < size; i++) 10 | { 11 | if(ratings[i]>ratings[i-1]) 12 | num[i]=num[i-1]+1; 13 | } 14 | for (int i= size-1; i>0 ; i--) 15 | { 16 | if(ratings[i-1]>ratings[i]) 17 | num[i-1]=max(num[i]+1,num[i-1]); 18 | } 19 | int result=0; 20 | for (int i = 0; i < size; i++) 21 | { 22 | result+=num[i]; 23 | // cout<& nums) { 4 | int ans = 0; 5 | for(int i=0;i136. Single Number

Easy


Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

2 | 3 |

You must implement a solution with a linear runtime complexity and use only constant extra space.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [2,2,1]
 8 | Output: 1
 9 | 

Example 2:

10 |
Input: nums = [4,1,2,1,2]
11 | Output: 4
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: 1
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • 1 <= nums.length <= 3 * 104
  • 21 |
  • -3 * 104 <= nums[i] <= 3 * 104
  • 22 |
  • Each element in the array appears twice except for one element which appears only once.
  • 23 |
24 | -------------------------------------------------------------------------------- /0138-copy-list-with-random-pointer/0138-copy-list-with-random-pointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | int val; 5 | Node next; 6 | Node random; 7 | 8 | public Node(int val) { 9 | this.val = val; 10 | this.next = null; 11 | this.random = null; 12 | } 13 | } 14 | */ 15 | 16 | class Solution { 17 | public Node copyRandomList(Node head) { 18 | if (head == null) return null; 19 | 20 | Node curr = head; // Create new list w/ same values 21 | while (curr != null) { 22 | Node newNode = new Node(curr.val); 23 | newNode.next = curr.next; 24 | curr.next = newNode; 25 | curr = newNode.next; 26 | } 27 | 28 | curr = head; // Copy the random pointers 29 | while (curr != null) { 30 | if (curr.random != null) 31 | curr.next.random = curr.random.next; 32 | curr = curr.next.next; 33 | } 34 | 35 | curr = head; // Separate the two lists 36 | Node newHead = head.next; 37 | Node newCurr = newHead; 38 | while (curr != null) { 39 | curr.next = newCurr.next; 40 | curr = curr.next; 41 | if (curr != null) { 42 | newCurr.next = curr.next; 43 | newCurr = newCurr.next; 44 | } 45 | } 46 | 47 | return newHead; 48 | 49 | } 50 | } -------------------------------------------------------------------------------- /0141-linked-list-cycle/0141-linked-list-cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | unordered_set visited; 13 | 14 | while(head){ 15 | if(visited.find(head)!=visited.end()) return true; 16 | 17 | visited.insert(head); 18 | head = head->next; 19 | } 20 | return false; 21 | } 22 | }; -------------------------------------------------------------------------------- /0141-linked-list-cycle/0141-linked-list-cycle.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public boolean hasCycle(ListNode head) { 14 | if(head==null || head.next==null) return false; 15 | 16 | ListNode s = head; 17 | ListNode f = head; 18 | 19 | while(f.next!=null && f.next.next!=null){ 20 | s = s.next; 21 | f = f.next.next; 22 | 23 | if(s==f) return true; 24 | } 25 | return false; 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *detectCycle(ListNode *head) { 12 | if(!head){ 13 | return NULL; 14 | } 15 | 16 | ListNode* slow = head; 17 | ListNode* fast = head; 18 | ListNode* point = head; 19 | 20 | while(fast->next && fast->next->next){ 21 | slow = slow->next; 22 | fast = fast->next->next;; 23 | 24 | if(slow == fast){ 25 | while(point!=slow){ 26 | point=point->next; 27 | slow=slow->next; 28 | } 29 | return point; 30 | } 31 | 32 | } 33 | return NULL; 34 | } 35 | }; -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public ListNode detectCycle(ListNode head) { 14 | if(head==null || head.next==null) return null; 15 | 16 | ListNode s = head; 17 | ListNode f = head; 18 | 19 | while(f.next!=null && f.next.next!=null){ 20 | s = s.next; 21 | f = f.next.next; 22 | 23 | if(s==f){ 24 | ListNode d = head; 25 | while(s!=d){ 26 | s = s.next; 27 | d = d.next; 28 | } 29 | return d; 30 | } 31 | } 32 | return null; 33 | } 34 | } -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector preorderTraversal(TreeNode* root) { 15 | vector ans; 16 | if(root==NULL) return ans; 17 | 18 | stack s; 19 | s.push(root); 20 | 21 | while(!s.empty()){ 22 | TreeNode* node = s.top(); 23 | s.pop(); 24 | ans.push_back(node->val); 25 | if(node->right){ 26 | s.push(node->right); 27 | } 28 | if(node->left){ 29 | s.push(node->left); 30 | } 31 | 32 | } 33 | return ans; 34 | 35 | } 36 | }; -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector postorderTraversal(TreeNode* root) { 15 | vector ans; 16 | if(root==NULL) return ans; 17 | 18 | stack s1,s2; 19 | s1.push(root); 20 | while(!s1.empty()){ 21 | root = s1.top(); 22 | s1.pop(); 23 | s2.push(root); 24 | 25 | if(root->left){ 26 | s1.push(root->left); 27 | } 28 | if(root->right){ 29 | s1.push(root->right); 30 | } 31 | } 32 | while(!s2.empty()){ 33 | ans.push_back(s2.top()->val); 34 | s2.pop(); 35 | } 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/0145-binary-tree-postorder-traversal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public void postorder(TreeNode root,List ans){ 18 | if(root==null) return; 19 | postorder(root.left,ans); 20 | postorder(root.right,ans); 21 | ans.add(root.val); 22 | } 23 | public List postorderTraversal(TreeNode root) { 24 | List ans = new ArrayList<>(); 25 | postorder(root,ans); 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /0148-sort-list/0148-sort-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* sortList(ListNode* head) { 14 | if(!head) return NULL; 15 | ListNode* temp = head; 16 | vector arr; 17 | while(temp!=NULL){ 18 | arr.push_back(temp->val); 19 | temp = temp->next; 20 | } 21 | temp = head; 22 | sort(arr.begin(),arr.end()); 23 | for(int i=0;ival = arr[i]; 25 | temp = temp->next; 26 | } 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /0148-sort-list/README.md: -------------------------------------------------------------------------------- 1 |

148. Sort List

Medium


Given the head of a linked list, return the list after sorting it in ascending order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [4,2,1,3]
 8 | Output: [1,2,3,4]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [-1,5,3,4,0]
15 | Output: [-1,0,3,4,5]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: head = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the list is in the range [0, 5 * 104].
  • 30 |
  • -105 <= Node.val <= 105
  • 31 |
32 | 33 |

 

34 |

Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?

35 | -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/0151-reverse-words-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | String []arr = s.split("\\s+"); 4 | StringBuilder ans = new StringBuilder(); 5 | int n = arr.length; 6 | for(int i = n-1 ; i>=0 ;i--){ 7 | ans.append(arr[i]); 8 | ans.append(" "); 9 | } 10 | return ans.toString().trim(); 11 | } 12 | } -------------------------------------------------------------------------------- /0153-find-minimum-in-rotated-sorted-array/0153-find-minimum-in-rotated-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& arr) { 4 | int low = 0, high = arr.size() - 1; 5 | int ans = INT_MAX; 6 | while (low <= high) { 7 | int mid = (low + high) / 2; 8 | if (arr[low] <= arr[mid]) { 9 | ans = min(ans, arr[low]); 10 | low = mid + 1; 11 | } 12 | else { 13 | ans = min(ans, arr[mid]); 14 | high = mid - 1; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0160-intersection-of-two-linked-lists/0160-intersection-of-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | if(!headA && !headB){ 13 | return NULL; 14 | } 15 | ListNode* t1 = headA; 16 | ListNode* t2 = headB; 17 | 18 | while(t1!=t2){ 19 | if(t1==NULL){ 20 | t1 = headB; 21 | }else{ 22 | t1 = t1->next; 23 | } 24 | 25 | if(t2==NULL){ 26 | t2 = headA; 27 | }else{ 28 | t2 = t2->next; 29 | } 30 | } 31 | return t1; 32 | } 33 | }; -------------------------------------------------------------------------------- /0160-intersection-of-two-linked-lists/0160-intersection-of-two-linked-lists.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 14 | if(headA==null || headB==null) return null; 15 | 16 | ListNode a = headA; 17 | ListNode b = headB; 18 | 19 | while(a!=b){ 20 | a = a==null ? headB : a.next; 21 | b = b==null ? headA : b.next; 22 | } 23 | return a; 24 | } 25 | } -------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | map mp; 5 | for(int i=0;i mxfreq){ 12 | mxfreq = ele.second; 13 | mxele = ele.first; 14 | } 15 | } 16 | return mxele; 17 | } 18 | }; -------------------------------------------------------------------------------- /0169-majority-element/0169-majority-element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | Arrays.sort(nums); 4 | return nums[nums.length / 2]; 5 | } 6 | } -------------------------------------------------------------------------------- /0169-majority-element/README.md: -------------------------------------------------------------------------------- 1 |

169. Majority Element

Easy


Given an array nums of size n, return the majority element.

2 | 3 |

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [3,2,3]
 8 | Output: 3
 9 | 

Example 2:

10 |
Input: nums = [2,2,1,1,1,2,2]
11 | Output: 2
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • n == nums.length
  • 18 |
  • 1 <= n <= 5 * 104
  • 19 |
  • -109 <= nums[i] <= 109
  • 20 |
21 | 22 |

 

23 | Follow-up: Could you solve the problem in linear time and in O(1) space? -------------------------------------------------------------------------------- /0175-combine-two-tables/0175-combine-two-tables.sql: -------------------------------------------------------------------------------- 1 | /* Write your PL/SQL query statement below */ 2 | SELECT p.firstName,p.lastName,a.city,a.state 3 | FROM person p LEFT JOIN address a ON p.personId = a.personId 4 | -------------------------------------------------------------------------------- /0189-rotate-array/0189-rotate-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | if(n<=1) return; 6 | k = k%n; 7 | vector ans; 8 | for(int i=n-k; i<=n-1 ; i++){ 9 | ans.push_back(nums[i]); 10 | } 11 | for(int i=0;i& ans){ 15 | if(root==NULL) return; 16 | 17 | if(lev == ans.size()) ans.push_back(root->val); 18 | 19 | rightview(root->right,lev+1,ans); 20 | rightview(root->left,lev+1,ans); 21 | } 22 | 23 | vector rightSideView(TreeNode* root) { 24 | vector ans; 25 | if(root==NULL) return {}; 26 | int lev = 0; 27 | ans.push_back(root->val); 28 | rightview(root,lev,ans); 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /0200-number-of-islands/0200-number-of-islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void findIsland(int i,int j,vector>& grid){ 4 | if(i<0 || j<0 || i>=grid.size() || j>=grid[0].size() || grid[i][j]=='0'){ 5 | return; 6 | } 7 | 8 | grid[i][j] = '0'; 9 | 10 | findIsland(i+1,j,grid); 11 | findIsland(i-1,j,grid); 12 | findIsland(i,j+1,grid); 13 | findIsland(i,j-1,grid); 14 | 15 | return; 16 | } 17 | void func(vector>& grid){ 18 | for(int i=0;i>& grid) { 28 | int c = 0; 29 | for(int i=0;i 0){ 7 | int rem = n%10; 8 | sum += pow(rem,2); 9 | n /= 10; 10 | } 11 | if(sum==1 || sum/10==0 && sum!=7){ 12 | break; 13 | } 14 | n = sum; 15 | sum = 0; 16 | } 17 | if(sum == 1){ 18 | return true; 19 | }else{ 20 | return false; 21 | } 22 | } 23 | }; -------------------------------------------------------------------------------- /0203-remove-linked-list-elements/0203-remove-linked-list-elements.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* removeElements(ListNode* head, int val) { 14 | ListNode* dummy = new ListNode(0); 15 | dummy->next = head; 16 | 17 | ListNode* prev = dummy; 18 | ListNode* crnt = head; 19 | 20 | while(crnt!=NULL){ 21 | if(crnt->val == val){ 22 | prev->next = crnt->next; 23 | delete crnt; 24 | crnt = prev->next; 25 | }else{ 26 | crnt = crnt->next; 27 | prev = prev->next; 28 | } 29 | } 30 | 31 | return dummy->next; 32 | } 33 | }; -------------------------------------------------------------------------------- /0203-remove-linked-list-elements/README.md: -------------------------------------------------------------------------------- 1 |

203. Remove Linked List Elements

Easy


Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,2,6,3,4,5,6], val = 6
 8 | Output: [1,2,3,4,5]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [], val = 1
15 | Output: []
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: head = [7,7,7,7], val = 7
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the list is in the range [0, 104].
  • 30 |
  • 1 <= Node.val <= 50
  • 31 |
  • 0 <= val <= 50
  • 32 |
33 | -------------------------------------------------------------------------------- /0205-isomorphic-strings/0205-isomorphic-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIsomorphic(string s, string t){ 4 | vector idxs(128,0); 5 | vector idxt(128,0); 6 | if(s.length() != t.length()){ 7 | return false; 8 | } 9 | 10 | for(int i=0;i st; 16 | while(temp!=NULL){ 17 | st.push(temp->val); 18 | temp = temp->next; 19 | } 20 | temp = head; 21 | while(!st.empty()){ 22 | int x = st.top(); 23 | temp->val = x; 24 | temp = temp->next; 25 | st.pop(); 26 | } 27 | return head; 28 | } 29 | }; -------------------------------------------------------------------------------- /0206-reverse-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

206. Reverse Linked List

Easy


Given the head of a singly linked list, reverse the list, and return the reversed list.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,2,3,4,5]
 8 | Output: [5,4,3,2,1]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [1,2]
15 | Output: [2,1]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: head = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the list is the range [0, 5000].
  • 30 |
  • -5000 <= Node.val <= 5000
  • 31 |
32 | 33 |

 

34 |

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

35 | -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& arr, int k) { 4 | int ans = 0; 5 | sort(arr.begin(), arr.end(), greater()); 6 | int i = 0; 7 | while (k--) { 8 | ans = arr[i]; 9 | i++; 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

215. Kth Largest Element in an Array

Medium


Given an integer array nums and an integer k, return the kth largest element in the array.

2 | 3 |

Note that it is the kth largest element in the sorted order, not the kth distinct element.

4 | 5 |

Can you solve it without sorting?

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [3,2,1,5,6,4], k = 2
10 | Output: 5
11 | 

Example 2:

12 |
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
13 | Output: 4
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= k <= nums.length <= 105
  • 20 |
  • -104 <= nums[i] <= 104
  • 21 |
22 | -------------------------------------------------------------------------------- /0217-contains-duplicate/0217-contains-duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size(); 6 | for(int i = 0; i < n - 1; i++) { 7 | if(nums[i] == nums[i + 1]) { 8 | return true; 9 | } 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /0222-count-complete-tree-nodes/0222-count-complete-tree-nodes.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int countNodes(TreeNode* root) { 15 | if(root==NULL) return 0; 16 | int l = countNodes(root->left); 17 | int r = countNodes(root->right); 18 | return 1+l+r; 19 | } 20 | }; -------------------------------------------------------------------------------- /0225-implement-stack-using-queues/0225-implement-stack-using-queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | private: 3 | deque ans; 4 | public: 5 | MyStack() { 6 | ans = deque(); 7 | } 8 | 9 | void push(int x) { 10 | ans.push_back(x); 11 | } 12 | 13 | int pop() { 14 | int topele = ans.back(); 15 | ans.pop_back(); 16 | return topele; 17 | } 18 | 19 | int top() { 20 | return ans.back(); 21 | } 22 | 23 | bool empty() { 24 | return ans.empty(); 25 | } 26 | }; 27 | 28 | /** 29 | * Your MyStack object will be instantiated and called as such: 30 | * MyStack* obj = new MyStack(); 31 | * obj->push(x); 32 | * int param_2 = obj->pop(); 33 | * int param_3 = obj->top(); 34 | * bool param_4 = obj->empty(); 35 | */ -------------------------------------------------------------------------------- /0226-invert-binary-tree/0226-invert-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* invertTree(TreeNode* root) { 15 | if(!root){ 16 | return NULL; 17 | } 18 | TreeNode* temp = root->left; 19 | root->left = root->right; 20 | root->right = temp; 21 | 22 | invertTree(root->left); 23 | invertTree(root->right); 24 | 25 | return root; 26 | } 27 | }; -------------------------------------------------------------------------------- /0226-invert-binary-tree/0226-invert-binary-tree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public TreeNode invertTree(TreeNode root) { 18 | if(root==null){ 19 | return null; 20 | } 21 | TreeNode temp = root.left; 22 | root.left = root.right; 23 | root.right = temp; 24 | 25 | invertTree(root.left); 26 | invertTree(root.right); 27 | 28 | return root; 29 | } 30 | } -------------------------------------------------------------------------------- /0226-invert-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

226. Invert Binary Tree

Easy


Given the root of a binary tree, invert the tree, and return its root.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: root = [4,2,7,1,3,6,9]
 8 | Output: [4,7,2,9,6,3,1]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: root = [2,1,3]
15 | Output: [2,3,1]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: root = []
22 | Output: []
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • The number of nodes in the tree is in the range [0, 100].
  • 30 |
  • -100 <= Node.val <= 100
  • 31 |
32 | -------------------------------------------------------------------------------- /0228-summary-ranges/0228-summary-ranges.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List summaryRanges(int[] nums) { 3 | ArrayList ans = new ArrayList<>(); 4 | 5 | for(int i=0;i"+nums[i]); 17 | } 18 | 19 | 20 | } 21 | return ans; 22 | } 23 | } -------------------------------------------------------------------------------- /0229-majority-element-ii/0229-majority-element-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector majorityElement(vector& nums) { 4 | int n = nums.size(); 5 | map mp; 6 | for(int i=0;i ans; 11 | for(auto ele : mp){ 12 | if(ele.second > n/3) ans.push_back(ele.first); 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /0229-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 |

229. Majority Element II

Medium


Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [3,2,3]
 8 | Output: [3]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: nums = [1]
15 | Output: [1]
16 | 
17 | 18 |

Example 3:

19 | 20 |
21 | Input: nums = [1,2]
22 | Output: [1,2]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 1 <= nums.length <= 5 * 104
  • 30 |
  • -109 <= nums[i] <= 109
  • 31 |
32 | 33 |

 

34 |

Follow up: Could you solve the problem in linear time and in O(1) space?

35 | -------------------------------------------------------------------------------- /0230-kth-smallest-element-in-a-bst/0230-kth-smallest-element-in-a-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void inorder(TreeNode* root , vector& arr){ 15 | if(root==NULL){ 16 | return; 17 | } 18 | if(root->left) inorder(root->left,arr); 19 | arr.push_back(root->val); 20 | if(root->right) inorder(root->right,arr); 21 | } 22 | int kthSmallest(TreeNode* root, int k) { 23 | vector arr; 24 | inorder(root,arr); 25 | return arr[k-1]; 26 | } 27 | }; -------------------------------------------------------------------------------- /0232-implement-queue-using-stacks/0232-implement-queue-using-stacks.cpp: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | private: 3 | stack s1,s2; 4 | public: 5 | MyQueue() { 6 | 7 | } 8 | 9 | void push(int x) { 10 | while(!s1.empty()){ 11 | s2.push(s1.top()); 12 | s1.pop(); 13 | } 14 | s1.push(x); 15 | while(!s2.empty()){ 16 | s1.push(s2.top()); 17 | s2.pop(); 18 | } 19 | 20 | } 21 | 22 | int pop() { 23 | int topele = s1.top(); 24 | s1.pop(); 25 | return topele; 26 | } 27 | 28 | int peek() { 29 | return s1.top(); 30 | } 31 | 32 | bool empty() { 33 | return s1.empty(); 34 | } 35 | }; 36 | 37 | /** 38 | * Your MyQueue object will be instantiated and called as such: 39 | * MyQueue* obj = new MyQueue(); 40 | * obj->push(x); 41 | * int param_2 = obj->pop(); 42 | * int param_3 = obj->peek(); 43 | * bool param_4 = obj->empty(); 44 | */ -------------------------------------------------------------------------------- /0234-palindrome-linked-list/0234-palindrome-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | bool isPalindrome(ListNode* head) { 14 | ListNode* t = head; 15 | vector arr; 16 | while(t!=NULL){ 17 | arr.push_back(t->val); 18 | t = t->next; 19 | } 20 | vector rev = arr; 21 | reverse(rev.begin(),rev.end()); 22 | 23 | return arr==rev; 24 | } 25 | }; -------------------------------------------------------------------------------- /0234-palindrome-linked-list/0234-palindrome-linked-list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode revlist(ListNode h){ 13 | if(h==null || h.next==null) return h; 14 | 15 | ListNode newNode = revlist(h.next); 16 | ListNode f = h.next; 17 | f.next = h; 18 | h.next = null; 19 | return newNode; 20 | } 21 | 22 | public boolean isPalindrome(ListNode head) { 23 | if(head==null || head.next==null) return true; 24 | 25 | ListNode s = head; 26 | ListNode f = head; 27 | // find middle of list 28 | while(f.next!=null && f.next.next!=null){ 29 | s = s.next; 30 | f = f.next.next; 31 | } 32 | // reverse the right part 33 | ListNode newHead = revlist(s.next); 34 | 35 | // check two pointers valu equal or not 36 | ListNode first = head; 37 | ListNode second = newHead; 38 | while(second!=null){ 39 | if(first.val!=second.val) return false; 40 | first = first.next; 41 | second = second.next; 42 | } 43 | return true; 44 | } 45 | } -------------------------------------------------------------------------------- /0234-palindrome-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

234. Palindrome Linked List

Easy


Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: head = [1,2,2,1]
 8 | Output: true
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: head = [1,2]
15 | Output: false
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • The number of nodes in the list is in the range [1, 105].
  • 23 |
  • 0 <= Node.val <= 9
  • 24 |
25 | 26 |

 

27 | Follow up: Could you do it in O(n) time and O(1) space? -------------------------------------------------------------------------------- /0235-lowest-common-ancestor-of-a-binary-search-tree/0235-lowest-common-ancestor-of-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | if(root==NULL || root==p || root==q) return root; 15 | TreeNode* left = lowestCommonAncestor(root->left,p,q); 16 | TreeNode* right = lowestCommonAncestor(root->right,p,q); 17 | 18 | if(left==NULL) return right; 19 | if(right==NULL) return left; 20 | return root; 21 | } 22 | }; -------------------------------------------------------------------------------- /0236-lowest-common-ancestor-of-a-binary-tree/0236-lowest-common-ancestor-of-a-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 13 | if (root == NULL || root == p || root == q) { 14 | return root; 15 | } 16 | TreeNode* left = lowestCommonAncestor(root->left, p, q); 17 | TreeNode* right = lowestCommonAncestor(root->right, p, q); 18 | 19 | if(left==NULL) return right; 20 | if(right==NULL) return left; 21 | return root; 22 | } 23 | }; -------------------------------------------------------------------------------- /0237-delete-node-in-a-linked-list/0237-delete-node-in-a-linked-list.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public void deleteNode(ListNode node) { 11 | node.val = node.next.val; 12 | node.next = node.next.next; 13 | } 14 | } -------------------------------------------------------------------------------- /0238-product-of-array-except-self/0238-product-of-array-except-self.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | int mul = 1 , zc=0;; 5 | int n = nums.size(); 6 | for(int i=0;i1) 14 | nums[i] = 0; 15 | else if(zc==1) 16 | nums[i]=(nums[i] == 0)?mul:0; 17 | else 18 | nums[i] = mul/nums[i]; 19 | } 20 | return nums; 21 | } 22 | }; -------------------------------------------------------------------------------- /0242-valid-anagram/0242-valid-anagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if(s.length() != t.length()){ 5 | return false; 6 | } 7 | sort(s.begin(),s.end()); 8 | sort(t.begin(),t.end()); 9 | 10 | for(int i=0;i242. Valid Anagram

Easy


Given two strings s and t, return true if t is an anagram of s, and false otherwise.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
7 |

Input: s = "anagram", t = "nagaram"

8 | 9 |

Output: true

10 |
11 | 12 |

Example 2:

13 | 14 |
15 |

Input: s = "rat", t = "car"

16 | 17 |

Output: false

18 |
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= s.length, t.length <= 5 * 104
  • 25 |
  • s and t consist of lowercase English letters.
  • 26 |
27 | 28 |

 

29 |

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?

30 | -------------------------------------------------------------------------------- /0257-binary-tree-paths/0257-binary-tree-paths.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void dfs(TreeNode* root , string str , vector &ans){ 15 | str += to_string(root->val); 16 | 17 | if(root->left == NULL && root->right == NULL){ 18 | ans.push_back(str); 19 | } 20 | str+= "->"; 21 | if(root->left){ 22 | dfs(root->left,str,ans); 23 | } 24 | if(root->right){ 25 | dfs(root->right,str,ans); 26 | } 27 | str.pop_back(); 28 | } 29 | 30 | vector binaryTreePaths(TreeNode* root) { 31 | vector ans; 32 | if(root==NULL) 33 | return ans; 34 | 35 | dfs(root,"",ans); 36 | return ans; 37 | } 38 | }; -------------------------------------------------------------------------------- /0257-binary-tree-paths/README.md: -------------------------------------------------------------------------------- 1 |

257. Binary Tree Paths

Easy


Given the root of a binary tree, return all root-to-leaf paths in any order.

2 | 3 |

A leaf is a node with no children.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [1,2,3,null,5]
10 | Output: ["1->2->5","1->3"]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [1]
17 | Output: ["1"]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [1, 100].
  • 25 |
  • -100 <= Node.val <= 100
  • 26 |
27 | -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fun(int num){ 4 | while(num >= 10){ 5 | int sum = 0; 6 | while(num > 0){ 7 | sum += num%10; 8 | num /= 10; 9 | } 10 | num = sum; 11 | } 12 | return num; 13 | } 14 | 15 | int addDigits(int num) { 16 | return fun(num); 17 | } 18 | }; -------------------------------------------------------------------------------- /0258-add-digits/README.md: -------------------------------------------------------------------------------- 1 |

258. Add Digits

Easy


Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: num = 38
 8 | Output: 2
 9 | Explanation: The process is
10 | 38 --> 3 + 8 --> 11
11 | 11 --> 1 + 1 --> 2 
12 | Since 2 has only one digit, return it.
13 | 
14 | 15 |

Example 2:

16 | 17 |
18 | Input: num = 0
19 | Output: 0
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 0 <= num <= 231 - 1
  • 27 |
28 | 29 |

 

30 |

Follow up: Could you do it without any loop/recursion in O(1) runtime?

31 | -------------------------------------------------------------------------------- /0268-missing-number/0268-missing-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | int n = nums.size(); 5 | sort(nums.begin(),nums.end()); 6 | int mx = nums[n-1]; 7 | int i; 8 | for(i=0;i& nums) { 4 | int idx = 0; 5 | int n = nums.size(); 6 | for(int i=0;i idx){ 10 | nums[idx++] = 0; 11 | } 12 | } 13 | }; -------------------------------------------------------------------------------- /0283-move-zeroes/README.md: -------------------------------------------------------------------------------- 1 |

283. Move Zeroes

Easy


Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

2 | 3 |

Note that you must do this in-place without making a copy of the array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [0,1,0,3,12]
 8 | Output: [1,3,12,0,0]
 9 | 

Example 2:

10 |
Input: nums = [0]
11 | Output: [0]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 104
  • 18 |
  • -231 <= nums[i] <= 231 - 1
  • 19 |
20 | 21 |

 

22 | Follow up: Could you minimize the total number of operations done? -------------------------------------------------------------------------------- /0287-find-the-duplicate-number/0287-find-the-duplicate-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | int s = nums[0]; 5 | int f = nums[0]; 6 | 7 | do{ 8 | s = nums[s]; 9 | f = nums[nums[f]]; 10 | }while(s != f); 11 | 12 | f = nums[0]; 13 | while(s != f){ 14 | s = nums[s]; 15 | f = nums[f]; 16 | } 17 | return s; 18 | } 19 | }; -------------------------------------------------------------------------------- /0303-range-sum-query-immutable/0303-range-sum-query-immutable.cpp: -------------------------------------------------------------------------------- 1 | class NumArray { 2 | public: 3 | vector arr; 4 | NumArray(vector& nums) { 5 | int sum = 0; 6 | for(int i=0;isumRange(left,right); 22 | */ -------------------------------------------------------------------------------- /0326-power-of-three/0326-power-of-three.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | if(n <= 0) return false; 5 | int mx = 20; 6 | ; 7 | for(int i=0;i326. Power of Three

Easy


Given an integer n, return true if it is a power of three. Otherwise, return false.

2 | 3 |

An integer n is a power of three, if there exists an integer x such that n == 3x.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: n = 27
10 | Output: true
11 | Explanation: 27 = 33
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: n = 0
18 | Output: false
19 | Explanation: There is no x where 3x = 0.
20 | 
21 | 22 |

Example 3:

23 | 24 |
25 | Input: n = -1
26 | Output: false
27 | Explanation: There is no x where 3x = (-1).
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 |
    34 |
  • -231 <= n <= 231 - 1
  • 35 |
36 | 37 |

 

38 | Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /0328-odd-even-linked-list/0328-odd-even-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* oddEvenList(ListNode* head) { 14 | if(head==NULL || head->next==NULL) return head; 15 | ListNode* temp = head; 16 | vector arr; 17 | while(temp!=NULL){ 18 | arr.push_back(temp->val); 19 | temp = temp->next; 20 | } 21 | vector ans; 22 | for(int i=0;ival = ans[i]; 31 | temp = temp->next; 32 | } 33 | return head; 34 | } 35 | }; -------------------------------------------------------------------------------- /0338-counting-bits/0338-counting-bits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | vector ans(n+1); 5 | for(int i=1;i<=n;i++){ 6 | if(i%2 != 0){ 7 | ans[i] = ans[i/2]+1; 8 | }else{ 9 | ans[i] = ans[i/2]; 10 | } 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /0344-reverse-string/0344-reverse-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void solve(vector& s , vector& rev ,int n){ 4 | if(n==0) return; 5 | rev.push_back(s[n-1]); 6 | solve(s,rev,n-1); 7 | } 8 | 9 | void reverseString(vector& s) { 10 | vector rev; 11 | int n = s.size(); 12 | solve(s,rev,n); 13 | 14 | for(int i=0;i344. Reverse String

Easy


Write a function that reverses a string. The input string is given as an array of characters s.

2 | 3 |

You must do this by modifying the input array in-place with O(1) extra memory.

4 | 5 |

 

6 |

Example 1:

7 |
Input: s = ["h","e","l","l","o"]
 8 | Output: ["o","l","l","e","h"]
 9 | 

Example 2:

10 |
Input: s = ["H","a","n","n","a","h"]
11 | Output: ["h","a","n","n","a","H"]
12 | 
13 |

 

14 |

Constraints:

15 | 16 | 20 | -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/0347-top-k-frequent-elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | vector ans; 5 | 6 | // Step 1: Count the frequency of each element in the array 7 | unordered_map freqMap; 8 | for (int num : nums) { 9 | freqMap[num]++; 10 | } 11 | 12 | // Step 2: Use a min-heap (priority queue) to store the k most frequent elements 13 | priority_queue, vector>, greater>> minHeap; 14 | 15 | // Step 3: Insert elements into the heap 16 | for (auto& entry : freqMap) { 17 | minHeap.push({entry.second, entry.first}); // Push {frequency, element} 18 | 19 | // If the heap size exceeds k, pop the element with the smallest frequency 20 | if (minHeap.size() > k) { 21 | minHeap.pop(); 22 | } 23 | } 24 | 25 | // Step 4: Extract the top k elements from the heap 26 | while (!minHeap.empty()) { 27 | ans.push_back(minHeap.top().second); // Push the element (not frequency) 28 | minHeap.pop(); 29 | } 30 | 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/README.md: -------------------------------------------------------------------------------- 1 |

347. Top K Frequent Elements

Medium


Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [1,1,1,2,2,3], k = 2
 6 | Output: [1,2]
 7 | 

Example 2:

8 |
Input: nums = [1], k = 1
 9 | Output: [1]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= nums.length <= 105
  • 16 |
  • -104 <= nums[i] <= 104
  • 17 |
  • k is in the range [1, the number of unique elements in the array].
  • 18 |
  • It is guaranteed that the answer is unique.
  • 19 |
20 | 21 |

 

22 |

Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

23 | -------------------------------------------------------------------------------- /0349-intersection-of-two-arrays/0349-intersection-of-two-arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | set s1,s2; 5 | 6 | for(auto ele : nums1){ 7 | s1.insert(ele); 8 | } 9 | 10 | for(auto ele : nums2){ 11 | if(s1.find(ele) != s1.end()){ 12 | s2.insert(ele); 13 | } 14 | } 15 | vector ans(s2.begin(),s2.end()); 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /0349-intersection-of-two-arrays/README.md: -------------------------------------------------------------------------------- 1 |

349. Intersection of Two Arrays

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums1 = [1,2,2,1], nums2 = [2,2]
 8 | Output: [2]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
15 | Output: [9,4]
16 | Explanation: [4,9] is also accepted.
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 24 |
  • 0 <= nums1[i], nums2[i] <= 1000
  • 25 |
26 | -------------------------------------------------------------------------------- /0387-first-unique-character-in-a-string/0387-first-unique-character-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | unordered_map mp; 5 | 6 | for(auto ele : s){ 7 | mp[ele]++; 8 | } 9 | 10 | for(int i=0;i434. Number of Segments in a String

Easy


Given a string s, return the number of segments in the string.

2 | 3 |

A segment is defined to be a contiguous sequence of non-space characters.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: s = "Hello, my name is John"
10 | Output: 5
11 | Explanation: The five segments are ["Hello,", "my", "name", "is", "John"]
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: s = "Hello"
18 | Output: 1
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 0 <= s.length <= 300
  • 26 |
  • s consists of lowercase and uppercase English letters, digits, or one of the following characters "!@#$%^&*()_+-=',.:".
  • 27 |
  • The only space character in s is ' '.
  • 28 |
29 | -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/0442-find-all-duplicates-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | map mp; 5 | for(auto ele : nums){ 6 | mp[ele]++; 7 | } 8 | 9 | vector ans; 10 | 11 | for(auto it : mp){ 12 | if(it.second > 1){ 13 | ans.push_back(it.first); 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /0442-find-all-duplicates-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

442. Find All Duplicates in an Array

Medium


Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appears twice.

2 | 3 |

You must write an algorithm that runs in O(n) time and uses only constant auxiliary space, excluding the space needed to store the output

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [4,3,2,7,8,2,3,1]
 8 | Output: [2,3]
 9 | 

Example 2:

10 |
Input: nums = [1,1,2]
11 | Output: [1]
12 | 

Example 3:

13 |
Input: nums = [1]
14 | Output: []
15 | 
16 |

 

17 |

Constraints:

18 | 19 |
    20 |
  • n == nums.length
  • 21 |
  • 1 <= n <= 105
  • 22 |
  • 1 <= nums[i] <= n
  • 23 |
  • Each element in nums appears once or twice.
  • 24 |
25 | -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/0448-find-all-numbers-disappeared-in-an-array.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public IList FindDisappearedNumbers(int[] nums) { 3 | IList ans = new List(); 4 | 5 | foreach(int num in nums){ 6 | int indx = Math.Abs(num)-1; 7 | if(nums[indx] > 0){ 8 | nums[indx] *= -1; 9 | } 10 | } 11 | for(int i=0;i 0){ 13 | ans.Add(i+1); 14 | } 15 | } 16 | return ans; 17 | } 18 | } -------------------------------------------------------------------------------- /0448-find-all-numbers-disappeared-in-an-array/README.md: -------------------------------------------------------------------------------- 1 |

448. Find All Numbers Disappeared in an Array

Easy


Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

2 | 3 |

 

4 |

Example 1:

5 |
Input: nums = [4,3,2,7,8,2,3,1]
 6 | Output: [5,6]
 7 | 

Example 2:

8 |
Input: nums = [1,1]
 9 | Output: [2]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • n == nums.length
  • 16 |
  • 1 <= n <= 105
  • 17 |
  • 1 <= nums[i] <= n
  • 18 |
19 | 20 |

 

21 |

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

22 | -------------------------------------------------------------------------------- /0450-delete-node-in-a-bst/0450-delete-node-in-a-bst.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* deleteNode(TreeNode* root, int key) { 15 | if (!root) return root; 16 | if (key < root->val) root->left = deleteNode(root->left, key); 17 | else if (key > root->val) root->right = deleteNode(root->right, key); 18 | else { 19 | if (!root->right) return root->left; 20 | if (!root->left) return root->right; 21 | auto tmp = findMinNode(root->right); 22 | root->val = tmp->val; 23 | root->right = deleteNode(root->right, tmp->val); 24 | } 25 | return root; 26 | } 27 | 28 | private: 29 | TreeNode* findMinNode(TreeNode* root) { 30 | if (!root || !root->left) return root; 31 | return findMinNode(root->left); 32 | } 33 | }; -------------------------------------------------------------------------------- /0451-sort-characters-by-frequency/0451-sort-characters-by-frequency.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | string frequencySort(string s) { 4 | unordered_map freq; 5 | for(char c : s){ 6 | freq[c]++; 7 | } 8 | priority_queue> maxheap; 9 | for(auto ele : freq){ 10 | maxheap.push({ele.second , ele.first}); 11 | } 12 | string ans = ""; 13 | while(!maxheap.empty()){ 14 | auto [val,key] = maxheap.top(); 15 | maxheap.pop(); 16 | ans.append(val,key); 17 | } 18 | return ans; 19 | } 20 | }; -------------------------------------------------------------------------------- /0476-number-complement/0476-number-complement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findComplement(int num) { 3 | String bS = Integer.toBinaryString(num); 4 | StringBuilder ansSt = new StringBuilder(); 5 | int len = bS.length(); 6 | 7 | for(int i=0;i& nums) { 4 | int mx = 0; 5 | int c = 0; 6 | for(int i=0;i485. Max Consecutive Ones

Easy


Given a binary array nums, return the maximum number of consecutive 1's in the array.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [1,1,0,1,1,1]
 8 | Output: 3
 9 | Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: nums = [1,0,1,1,0,1]
16 | Output: 2
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • 1 <= nums.length <= 105
  • 24 |
  • nums[i] is either 0 or 1.
  • 25 |
26 | -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/0491-non-decreasing-subsequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void backtrack(int[] nums, int index, List>finalAnswer,Listans){ 3 | if(ans.size() >= 2) finalAnswer.add(new ArrayList<>(ans)); 4 | HashSet set = new HashSet<>(); 5 | for(int i=index;i> findSubsequences(int[] nums) { 15 | List>finalAnswer = new ArrayList<>(); 16 | Listans = new ArrayList<>(); 17 | backtrack(nums,0,finalAnswer,ans); 18 | return finalAnswer; 19 | } 20 | } -------------------------------------------------------------------------------- /0491-non-decreasing-subsequences/README.md: -------------------------------------------------------------------------------- 1 |

491. Non-decreasing Subsequences

Medium


Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [4,6,7,7]
 8 | Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
 9 | 
10 | 11 |

Example 2:

12 | 13 |
14 | Input: nums = [4,4,3,2,1]
15 | Output: [[4,4]]
16 | 
17 | 18 |

 

19 |

Constraints:

20 | 21 |
    22 |
  • 1 <= nums.length <= 15
  • 23 |
  • -100 <= nums[i] <= 100
  • 24 |
25 | -------------------------------------------------------------------------------- /0493-reverse-pairs/README.md: -------------------------------------------------------------------------------- 1 |

493. Reverse Pairs

Hard


Given an integer array nums, return the number of reverse pairs in the array.

2 | 3 |

A reverse pair is a pair (i, j) where:

4 | 5 |
    6 |
  • 0 <= i < j < nums.length and
  • 7 |
  • nums[i] > 2 * nums[j].
  • 8 |
9 | 10 |

 

11 |

Example 1:

12 | 13 |
14 | Input: nums = [1,3,2,3,1]
15 | Output: 2
16 | Explanation: The reverse pairs are:
17 | (1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
18 | (3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1
19 | 
20 | 21 |

Example 2:

22 | 23 |
24 | Input: nums = [2,4,3,5,1]
25 | Output: 3
26 | Explanation: The reverse pairs are:
27 | (1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
28 | (2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
29 | (3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • 1 <= nums.length <= 5 * 104
  • 37 |
  • -231 <= nums[i] <= 231 - 1
  • 38 |
39 | -------------------------------------------------------------------------------- /0509-fibonacci-number/0509-fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fibo(vector& dp , int n){ 4 | if(n==0 || n==1) return n; 5 | if(dp[n]!=-1) return dp[n]; 6 | dp[n] = fibo(dp,n-1)+fibo(dp,n-2); 7 | return dp[n]; 8 | } 9 | int fib(int n) { 10 | vector dp(n+1 , -1); 11 | int ans = fibo(dp , n); 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /0509-fibonacci-number/README.md: -------------------------------------------------------------------------------- 1 |

509. Fibonacci Number

Easy


The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

2 | 3 |
 4 | F(0) = 0, F(1) = 1
 5 | F(n) = F(n - 1) + F(n - 2), for n > 1.
 6 | 
7 | 8 |

Given n, calculate F(n).

9 | 10 |

 

11 |

Example 1:

12 | 13 |
14 | Input: n = 2
15 | Output: 1
16 | Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
17 | 
18 | 19 |

Example 2:

20 | 21 |
22 | Input: n = 3
23 | Output: 2
24 | Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
25 | 
26 | 27 |

Example 3:

28 | 29 |
30 | Input: n = 4
31 | Output: 3
32 | Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
33 | 
34 | 35 |

 

36 |

Constraints:

37 | 38 |
    39 |
  • 0 <= n <= 30
  • 40 |
41 | -------------------------------------------------------------------------------- /0532-k-diff-pairs-in-an-array/0532-k-diff-pairs-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector& nums, int k) { 4 | // if k is less than 0 5 | if(k < 0 ) return 0; 6 | 7 | unordered_map mp; 8 | // add all elements on map for record -> ele count 9 | for(auto ele : nums){ 10 | mp[ele]++; 11 | } 12 | 13 | int count = 0; 14 | 15 | // if k is 0 , then find those numbres whose frequency are greater than 1. Ex - 1-1 = 0 , and count of 1 is 2 times 16 | if(k==0){ 17 | for(auto it : mp){ 18 | if(it.second>1){ 19 | count ++; 20 | } 21 | } 22 | return count; 23 | } 24 | 25 | // arr[i] - arr[j] = k ==>>> arr[i] - k = arr[j] . 26 | for(auto it : mp){ 27 | if(mp.count(it.first-k)){ 28 | count ++; 29 | } 30 | } 31 | return count; 32 | } 33 | }; -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/0540-single-element-in-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | int n = nums.size(); 5 | if(n==1) return nums[0]; 6 | if(nums[0]!=nums[1]) return nums[0]; 7 | if(nums[n-1]!=nums[n-2]) return nums[n-1]; 8 | int l = 1; 9 | int r = n-2; 10 | while(l <= r){ 11 | int m = (l+r)/2; 12 | if(nums[m]!=nums[m+1] && nums[m]!=nums[m-1]){ 13 | return nums[m]; 14 | } 15 | if( (m%2==1 && nums[m]==nums[m-1]) 16 | || (m%2==0 && nums[m]==nums[m+1])){ 17 | l = m+1; 18 | }else{ 19 | r = m-1; 20 | } 21 | } 22 | return -1; 23 | } 24 | }; -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

540. Single Element in a Sorted Array

Medium


You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

2 | 3 |

Return the single element that appears only once.

4 | 5 |

Your solution must run in O(log n) time and O(1) space.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,1,2,3,3,4,4,8,8]
10 | Output: 2
11 | 

Example 2:

12 |
Input: nums = [3,3,7,7,10,11,11]
13 | Output: 10
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= nums.length <= 105
  • 20 |
  • 0 <= nums[i] <= 105
  • 21 |
22 | -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/0543-diameter-of-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int height(TreeNode* r , int& dm){ 15 | if(r==NULL) return 0; 16 | int lh = height(r->left,dm); 17 | int rh = height(r->right,dm); 18 | dm = max(dm,lh+rh); 19 | return max(lh,rh)+1; 20 | } 21 | 22 | int diameterOfBinaryTree(TreeNode* root) { 23 | int dm = 0; 24 | height(root,dm); 25 | return dm; 26 | } 27 | }; -------------------------------------------------------------------------------- /0543-diameter-of-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

543. Diameter of Binary Tree

Easy


Given the root of a binary tree, return the length of the diameter of the tree.

2 | 3 |

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

4 | 5 |

The length of a path between two nodes is represented by the number of edges between them.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: root = [1,2,3,4,5]
12 | Output: 3
13 | Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: root = [1,2]
20 | Output: 1
21 | 
22 | 23 |

 

24 |

Constraints:

25 | 26 |
    27 |
  • The number of nodes in the tree is in the range [1, 104].
  • 28 |
  • -100 <= Node.val <= 100
  • 29 |
30 | -------------------------------------------------------------------------------- /0560-subarray-sum-equals-k/0560-subarray-sum-equals-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector& nums, int k) { 4 | int c = 0; 5 | int n = nums.size(); 6 | for(int i=0;i560. Subarray Sum Equals K

Medium


Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.

2 | 3 |

A subarray is a contiguous non-empty sequence of elements within an array.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [1,1,1], k = 2
 8 | Output: 2
 9 | 

Example 2:

10 |
Input: nums = [1,2,3], k = 3
11 | Output: 2
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 2 * 104
  • 18 |
  • -1000 <= nums[i] <= 1000
  • 19 |
  • -107 <= k <= 107
  • 20 |
21 | -------------------------------------------------------------------------------- /0583-delete-operation-for-two-strings/0583-delete-operation-for-two-strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int n = word1.length(); 5 | int m = word2.length(); 6 | 7 | vector>dp(n+1,vector(m+1,0)); 8 | 9 | for(int i=1;i<=n;i++){ 10 | for(int j=1;j<=m;j++){ 11 | if(word1[i-1] == word2[j-1]){ 12 | dp[i][j] = 1+dp[i-1][j-1]; 13 | }else{ 14 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 15 | } 16 | } 17 | } 18 | int lcs = dp[n][m]; 19 | int ans = m-lcs+n-lcs; 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /0583-delete-operation-for-two-strings/README.md: -------------------------------------------------------------------------------- 1 |

583. Delete Operation for Two Strings

Medium


Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.

2 | 3 |

In one step, you can delete exactly one character in either string.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: word1 = "sea", word2 = "eat"
10 | Output: 2
11 | Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: word1 = "leetcode", word2 = "etco"
18 | Output: 4
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= word1.length, word2.length <= 500
  • 26 |
  • word1 and word2 consist of only lowercase English letters.
  • 27 |
28 | -------------------------------------------------------------------------------- /0637-average-of-levels-in-binary-tree/0637-average-of-levels-in-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector averageOfLevels(TreeNode* root) { 15 | vector ans; 16 | queue q; 17 | q.push(root); 18 | 19 | while(!q.empty()){ 20 | int n = q.size(); 21 | double sum = 0; 22 | for(int i=0;ileft != NULL) q.push(temp->left); 27 | if(temp->right != NULL) q.push(temp->right); 28 | 29 | sum+=temp->val; 30 | } 31 | ans.push_back(sum/n); 32 | } 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /0637-average-of-levels-in-binary-tree/README.md: -------------------------------------------------------------------------------- 1 |

637. Average of Levels in Binary Tree

Easy


Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted. 2 |

 

3 |

Example 1:

4 | 5 |
 6 | Input: root = [3,9,20,null,null,15,7]
 7 | Output: [3.00000,14.50000,11.00000]
 8 | Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
 9 | Hence return [3, 14.5, 11].
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: root = [3,9,20,15,7]
16 | Output: [3.00000,14.50000,11.00000]
17 | 
18 | 19 |

 

20 |

Constraints:

21 | 22 |
    23 |
  • The number of nodes in the tree is in the range [1, 104].
  • 24 |
  • -231 <= Node.val <= 231 - 1
  • 25 |
26 | -------------------------------------------------------------------------------- /0645-set-mismatch/0645-set-mismatch.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | vector ans(2,0); 5 | map mp; 6 | for(int ele : nums){ 7 | mp[ele]++; 8 | } 9 | 10 | for(int i=1;i<=nums.size();i++){ 11 | if(mp[i]==2) ans[0] = i; 12 | if(mp[i]==0) ans[1] = i; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /0645-set-mismatch/README.md: -------------------------------------------------------------------------------- 1 |

645. Set Mismatch

Easy


You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

2 | 3 |

You are given an integer array nums representing the data status of this set after the error.

4 | 5 |

Find the number that occurs twice and the number that is missing and return them in the form of an array.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,2,2,4]
10 | Output: [2,3]
11 | 

Example 2:

12 |
Input: nums = [1,1]
13 | Output: [1,2]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 104
  • 20 |
  • 1 <= nums[i] <= 104
  • 21 |
22 | -------------------------------------------------------------------------------- /0661-image-smoother/0661-image-smoother.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] imageSmoother(int[][] img) { 3 | int r = img.length; 4 | int c = img[0].length; 5 | 6 | int ans[][] = new int[r][c]; 7 | 8 | int neighbour[][] = { {-1,-1} , {-1,0},{-1,1}, 9 | {0,-1} , {0,0} , {0,1}, 10 | {1,-1} , {1,0} , {1,1} }; 11 | 12 | for(int i=0;i=0 && b>=0 && aval!=val){ 16 | if(val <= root->val){ 17 | root = root->left; 18 | }else{ 19 | root = root->right; 20 | } 21 | } 22 | return root; 23 | } 24 | }; -------------------------------------------------------------------------------- /0700-search-in-a-binary-search-tree/README.md: -------------------------------------------------------------------------------- 1 |

700. Search in a Binary Search Tree

Easy


You are given the root of a binary search tree (BST) and an integer val.

2 | 3 |

Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: root = [4,2,7,1,3], val = 2
10 | Output: [2,1,3]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: root = [4,2,7,1,3], val = 5
17 | Output: []
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the tree is in the range [1, 5000].
  • 25 |
  • 1 <= Node.val <= 107
  • 26 |
  • root is a binary search tree.
  • 27 |
  • 1 <= val <= 107
  • 28 |
29 | -------------------------------------------------------------------------------- /0704-binary-search/0704-binary-search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bs(vector n,int t,int l,int h){ 4 | if(l > h) return -1; 5 | int m = l+(h-l)/2; 6 | if(n[m] == t) return m; 7 | else if(n[m] < t) return bs(n,t,m+1,h); 8 | else return bs(n,t,l,m-1); 9 | 10 | } 11 | int search(vector& nums, int target) { 12 | return bs(nums,target,0,nums.size()-1); 13 | } 14 | }; -------------------------------------------------------------------------------- /0704-binary-search/README.md: -------------------------------------------------------------------------------- 1 |

704. Binary Search

Easy


Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

2 | 3 |

You must write an algorithm with O(log n) runtime complexity.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: nums = [-1,0,3,5,9,12], target = 9
10 | Output: 4
11 | Explanation: 9 exists in nums and its index is 4
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: nums = [-1,0,3,5,9,12], target = 2
18 | Output: -1
19 | Explanation: 2 does not exist in nums so return -1
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 104
  • 27 |
  • -104 < nums[i], target < 104
  • 28 |
  • All the integers in nums are unique.
  • 29 |
  • nums is sorted in ascending order.
  • 30 |
31 | -------------------------------------------------------------------------------- /0744-find-smallest-letter-greater-than-target/0744-find-smallest-letter-greater-than-target.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector& letters, char target) { 4 | int l = 0; 5 | int r = letters.size() - 1; 6 | while (l <= r) { 7 | int mid = l + (r - l) / 2; 8 | if (letters[mid] <= target) { 9 | l = mid + 1; 10 | } else { 11 | r = mid - 1; 12 | } 13 | } 14 | return letters[l % letters.size()]; 15 | } 16 | }; -------------------------------------------------------------------------------- /0746-min-cost-climbing-stairs/0746-min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mincost(vector& c , int n , vector& dp){ 4 | if(n==0) return 0; 5 | if(n==1) return 0; 6 | if(dp[n]!=-1){ 7 | return dp[n]; 8 | } 9 | if(n==2) return dp[n] = min(c[0],c[1]); 10 | 11 | return dp[n] = min((mincost(c,n-1,dp)+c[n-1]) , (mincost(c,n-2,dp)+c[n-2])); 12 | } 13 | int minCostClimbingStairs(vector& cost) { 14 | int n = cost.size(); 15 | vector dp(n+1,-1); 16 | int ans = mincost(cost,n,dp); 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0784-insert-into-a-binary-search-tree/0784-insert-into-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* insertIntoBST(TreeNode* root, int val) { 15 | if(root==NULL) return new TreeNode(val); 16 | if(root->val < val){ 17 | root->right = insertIntoBST(root->right,val); 18 | } 19 | else if(root->val > val){ 20 | root->left = insertIntoBST(root->left,val); 21 | } 22 | return root; 23 | } 24 | }; -------------------------------------------------------------------------------- /0796-rotate-string/0796-rotate-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool rotateString(string s, string goal) { 4 | 5 | if(s.length() != goal.length()){ 6 | return false; 7 | } 8 | 9 | for(int i=0;i796. Rotate String

Easy


Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.

2 | 3 |

A shift on s consists of moving the leftmost character of s to the rightmost position.

4 | 5 |
    6 |
  • For example, if s = "abcde", then it will be "bcdea" after one shift.
  • 7 |
8 | 9 |

 

10 |

Example 1:

11 |
Input: s = "abcde", goal = "cdeab"
12 | Output: true
13 | 

Example 2:

14 |
Input: s = "abcde", goal = "abced"
15 | Output: false
16 | 
17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= s.length, goal.length <= 100
  • 22 |
  • s and goal consist of lowercase English letters.
  • 23 |
24 | -------------------------------------------------------------------------------- /0875-koko-eating-bananas/0875-koko-eating-bananas.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long totalHour(vector p, int k){ 4 | int n = p.size(); 5 | long long sum = 0; 6 | for(int i=0;i p){ 16 | int max = p[0]; 17 | int n = p.size(); 18 | for(int i=0;i max){ 20 | max = p[i]; 21 | } 22 | } 23 | return max; 24 | } 25 | 26 | int minEatingSpeed(vector& piles, int h) { 27 | int l = 1; 28 | int r = maxele(piles); 29 | int k = 0; 30 | while(l <= r){ 31 | int m = l+(r-l)/2; 32 | long long thr = totalHour(piles,m); 33 | if(thr <= h){ 34 | k = m; 35 | r = m-1; 36 | }else{ 37 | l = m+1; 38 | } 39 | } 40 | return k; 41 | } 42 | }; -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/0876-middle-of-the-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* middleNode(ListNode* head) { 14 | ListNode* slow = head; 15 | ListNode* fast = head; 16 | while(fast!=NULL && fast->next!=NULL){ 17 | fast = fast->next->next; 18 | slow = slow->next; 19 | } 20 | return slow; 21 | } 22 | }; -------------------------------------------------------------------------------- /0876-middle-of-the-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

876. Middle of the Linked List

Easy


Given the head of a singly linked list, return the middle node of the linked list.

2 | 3 |

If there are two middle nodes, return the second middle node.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: head = [1,2,3,4,5]
10 | Output: [3,4,5]
11 | Explanation: The middle node of the list is node 3.
12 | 
13 | 14 |

Example 2:

15 | 16 |
17 | Input: head = [1,2,3,4,5,6]
18 | Output: [4,5,6]
19 | Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • The number of nodes in the list is in the range [1, 100].
  • 27 |
  • 1 <= Node.val <= 100
  • 28 |
29 | -------------------------------------------------------------------------------- /0977-squares-of-a-sorted-array/0977-squares-of-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortedSquares(vector& nums) { 4 | int n = nums.size(); 5 | vector ans(n); 6 | for(int i=0;i977. Squares of a Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: nums = [-4,-1,0,3,10]
 8 | Output: [0,1,9,16,100]
 9 | Explanation: After squaring, the array becomes [16,1,0,9,100].
10 | After sorting, it becomes [0,1,9,16,100].
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: nums = [-7,-3,2,3,11]
17 | Output: [4,9,9,49,121]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 1 <= nums.length <= 104
  • 25 |
  • -104 <= nums[i] <= 104
  • 26 |
  • nums is sorted in non-decreasing order.
  • 27 |
28 | 29 |

 

30 | Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? -------------------------------------------------------------------------------- /0980-unique-paths-iii/0980-unique-paths-iii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int path(int i,int j, int[][] g,int zc){ 3 | if(i<0 || j<0 || i>=g.length || j>=g[0].length || g[i][j]==-1) return 0; 4 | 5 | if(g[i][j] == 2 && zc==0) return 1; 6 | 7 | int temp = g[i][j]; 8 | g[i][j] = -1; 9 | int nzc = (temp==0) ? zc-1 : zc; 10 | int ans = path(i+1,j,g,nzc)+ path(i-1,j,g,nzc)+ path(i,j+1,g,nzc)+ path(i,j-1,g,nzc); 11 | g[i][j] = temp; 12 | return ans; 13 | } 14 | 15 | public int uniquePathsIII(int[][] grid) { 16 | int zc = 0; 17 | for(int i=0;i &w,int cap){ 4 | int day = 1; 5 | int load = 0; 6 | for(int i=0;i cap){ 8 | day++; 9 | load = w[i]; 10 | }else{ 11 | load +=w[i]; 12 | } 13 | } 14 | return day; 15 | } 16 | int sumallele(vector w){ 17 | int n = w.size(); 18 | int sum = 0; 19 | for(int i=0;i& w, int days) { 25 | int l = *max_element(w.begin(),w.end()); 26 | int r = sumallele(w); 27 | int ans = -1; 28 | while(l <= r){ 29 | int m = l+(r-l)/2; 30 | int noofdays = findday(w,m); 31 | if(noofdays <= days){ 32 | ans = m; 33 | r = m-1; 34 | }else{ 35 | l = m+1; 36 | } 37 | } 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /1021-remove-outermost-parentheses/1021-remove-outermost-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeOuterParentheses(string s) { 4 | int bc = 0; 5 | int n = s.length(); 6 | string ans=""; 7 | for(int i=0;i 0) ans += "("; 10 | bc++; 11 | }else{ 12 | bc--; 13 | if(bc > 0) ans += ")"; 14 | } 15 | } 16 | return ans; 17 | } 18 | }; -------------------------------------------------------------------------------- /1025-divisor-game/1025-divisor-game.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool divisorGame(int n) { 4 | if(n <= 1) return false; 5 | for(int x=1;x<=n;x++){ 6 | if(n%x==0){ 7 | return !divisorGame(n-x); 8 | } 9 | } 10 | return false; 11 | } 12 | }; -------------------------------------------------------------------------------- /1025-divisor-game/README.md: -------------------------------------------------------------------------------- 1 |

1025. Divisor Game

Easy


Alice and Bob take turns playing a game, with Alice starting first.

2 | 3 |

Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of:

4 | 5 |
    6 |
  • Choosing any x with 0 < x < n and n % x == 0.
  • 7 |
  • Replacing the number n on the chalkboard with n - x.
  • 8 |
9 | 10 |

Also, if a player cannot make a move, they lose the game.

11 | 12 |

Return true if and only if Alice wins the game, assuming both players play optimally.

13 | 14 |

 

15 |

Example 1:

16 | 17 |
18 | Input: n = 2
19 | Output: true
20 | Explanation: Alice chooses 1, and Bob has no more moves.
21 | 
22 | 23 |

Example 2:

24 | 25 |
26 | Input: n = 3
27 | Output: false
28 | Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
29 | 
30 | 31 |

 

32 |

Constraints:

33 | 34 |
    35 |
  • 1 <= n <= 1000
  • 36 |
37 | -------------------------------------------------------------------------------- /1143-longest-common-subsequence/1143-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | int n = text1.length(); 5 | int m = text2.length(); 6 | 7 | vector>dp(n+1,vector(m+1,0)); 8 | 9 | for(int i=1;i<=n;i++){ 10 | for(int j=1;j<=m;j++){ 11 | if(text1[i-1] == text2[j-1]){ 12 | dp[i][j] = 1+dp[i-1][j-1]; 13 | }else{ 14 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 15 | } 16 | } 17 | } 18 | return dp[n][m]; 19 | } 20 | }; -------------------------------------------------------------------------------- /1217-minimum-cost-to-move-chips-to-the-same-position/1217-minimum-cost-to-move-chips-to-the-same-position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostToMoveChips(vector& position) { 4 | int o=0,e=0; 5 | for(int i=0;i n,int m){ 4 | int sum = 0; 5 | for(int i=0;i& nums, int threshold) { 15 | int l = 1; 16 | int r = *max_element(nums.begin(),nums.end()); 17 | int ans = -1; 18 | while(l <= r){ 19 | int mid = l+(r-l)/2; 20 | int div = divisor(nums,mid); 21 | if(div <= threshold){ 22 | ans = mid; 23 | r = mid-1; 24 | }else{ 25 | l = mid+1; 26 | } 27 | } 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /1539-kth-missing-positive-number/1539-kth-missing-positive-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthPositive(vector& arr, int k) { 4 | int l = 0; 5 | int r = arr.size()-1; 6 | while(l <= r){ 7 | int m = l+(r-l)/2; 8 | int missingele = arr[m] - (m+1); 9 | if(missingele < k){ 10 | l = m+1; 11 | }else{ 12 | r = m-1; 13 | } 14 | } 15 | return l+k; 16 | } 17 | }; -------------------------------------------------------------------------------- /1614-maximum-nesting-depth-of-the-parentheses/1614-maximum-nesting-depth-of-the-parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(string s) { 4 | int c = 0; 5 | int ans = 0; 6 | for(int i=0;i& words) { 4 | unordered_set us(allowed.begin() , allowed.end()); 5 | int c = 0; 6 | for(const string word : words){ 7 | bool isExist = true; 8 | for(char c : word){ 9 | if(us.find(c)==us.end()){ 10 | isExist = false; 11 | break; 12 | } 13 | } 14 | if(isExist) c++; 15 | } 16 | return c; 17 | } 18 | }; -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/1721-swapping-nodes-in-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* swapNodes(ListNode* head, int k) { 14 | if(head==NULL || head->next==NULL){ 15 | return head; 16 | } 17 | ListNode* temp = head; 18 | vector arr; 19 | while(temp!=NULL){ 20 | arr.push_back(temp->val); 21 | temp = temp->next; 22 | } 23 | swap(arr[k-1],arr[arr.size()-k]); 24 | temp = head; 25 | for(int i=0;ival = arr[i]; 27 | temp = temp->next; 28 | } 29 | return head; 30 | } 31 | }; -------------------------------------------------------------------------------- /1721-swapping-nodes-in-a-linked-list/README.md: -------------------------------------------------------------------------------- 1 |

1721. Swapping Nodes in a Linked List

Medium


You are given the head of a linked list, and an integer k.

2 | 3 |

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

4 | 5 |

 

6 |

Example 1:

7 | 8 |
 9 | Input: head = [1,2,3,4,5], k = 2
10 | Output: [1,4,3,2,5]
11 | 
12 | 13 |

Example 2:

14 | 15 |
16 | Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
17 | Output: [7,9,6,6,8,7,3,0,9,5]
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • The number of nodes in the list is n.
  • 25 |
  • 1 <= k <= n <= 105
  • 26 |
  • 0 <= Node.val <= 100
  • 27 |
28 | -------------------------------------------------------------------------------- /1752-check-if-array-is-sorted-and-rotated/1752-check-if-array-is-sorted-and-rotated.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean check(int[] nums) { 3 | int n = nums.length; 4 | int c = 0; 5 | if(nums[n-1] > nums[0]) c++; 6 | for(int i=1; i nums[i]) c++; 8 | } 9 | if(c <= 1) return true; 10 | else return false; 11 | } 12 | } -------------------------------------------------------------------------------- /1903-largest-odd-number-in-string/1903-largest-odd-number-in-string.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestOddNumber(string num) { 4 | int n = num.length(); 5 | for(int i=n-1 ; i>=0 ; i--){ 6 | if((num[i]-'0')%2 != 0){ 7 | return num.substr(0,i+1); 8 | } 9 | } 10 | return ""; 11 | } 12 | }; -------------------------------------------------------------------------------- /2095-delete-the-middle-node-of-a-linked-list/2095-delete-the-middle-node-of-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteMiddle(ListNode* head) { 14 | if(head==NULL || head->next==NULL) return NULL; 15 | ListNode* prev = NULL; 16 | ListNode* slow = head; 17 | ListNode* fast = head; 18 | while(fast!=NULL && fast->next!=NULL){ 19 | prev = slow; 20 | slow = slow->next; 21 | fast = fast->next->next; 22 | } 23 | prev->next = slow->next; 24 | delete slow; 25 | return head; 26 | } 27 | }; -------------------------------------------------------------------------------- /2149-rearrange-array-elements-by-sign/2149-rearrange-array-elements-by-sign.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector rearrangeArray(vector& nums) { 4 | int n = nums.size(); 5 | vector ans(n); 6 | int pidx = 0 , nidx = 1; 7 | for(int ele : nums){ 8 | if(ele >= 0){ 9 | ans[pidx] = ele; 10 | pidx += 2; 11 | } 12 | if(ele < 0){ 13 | ans[nidx] = ele; 14 | nidx += 2; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /2216-delete-the-middle-node-of-a-linked-list/2216-delete-the-middle-node-of-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* deleteMiddle(ListNode* head) { 14 | if(!head || !head->next) return nullptr; 15 | ListNode*slow = head; 16 | ListNode*fast = head->next->next; 17 | while(fast !=nullptr && fast->next != nullptr){ 18 | fast = fast->next->next; 19 | slow = slow->next; 20 | } 21 | ListNode*delnode = slow->next; 22 | slow->next =slow->next->next; 23 | delete delnode; 24 | return head; 25 | } 26 | }; -------------------------------------------------------------------------------- /2305-fair-distribution-of-cookies/2305-fair-distribution-of-cookies.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dCookies(vector& cookies, vector& ch, int k, int c) { 4 | if (c == cookies.size()) { 5 | int unfairness = 0; 6 | for (int i : ch) { 7 | unfairness = max(unfairness, i); 8 | } 9 | return unfairness; 10 | } 11 | 12 | int minUnfairness = INT_MAX; 13 | for (int j = 0; j < k; j++) { 14 | ch[j] += cookies[c]; 15 | minUnfairness = min(minUnfairness, dCookies(cookies, ch, k, c + 1)); 16 | ch[j] -= cookies[c]; 17 | } 18 | return minUnfairness; 19 | } 20 | 21 | int distributeCookies(vector& cookies, int k) { 22 | vector children(k); 23 | return dCookies(cookies, children, k, 0); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /2461-maximum-sum-of-distinct-subarrays-with-length-k/2461-maximum-sum-of-distinct-subarrays-with-length-k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long maximumSubarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | long long maxsum = 0; 6 | unordered_map freq; 7 | 8 | 9 | long long windowsum=0; 10 | for(int i=0;i=k){ 17 | windowsum -= nums[i-k]; 18 | freq[nums[i-k]]--; 19 | if(freq[nums[i-k]]==0){ 20 | freq.erase(nums[i-k]); 21 | } 22 | } 23 | 24 | // Maximum maxsum 25 | if(i >=k-1 && freq.size()==k){ 26 | maxsum = max(maxsum,windowsum); 27 | } 28 | } 29 | return maxsum; 30 | } 31 | }; -------------------------------------------------------------------------------- /2914-minimum-number-of-changes-to-make-binary-string-beautiful/2914-minimum-number-of-changes-to-make-binary-string-beautiful.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minChanges(string s) { 4 | int n = s.length(); 5 | int count = 0; 6 | for(int i=0;i