Posts

3. Longest Substring Without Repeating Characters

C++ | 100% Success | Simple Approach class Solution { public : int lengthOfLongestSubstring ( string s) { unordered_map < char , int > mp; int start= 0 ,maxlength= 0 ; for ( int i= 0 ;i<s.length();i++){ mp[s[i]]++; if (mp[s[i]]> 1 ){ while (start< s.length() && mp[s[i]]> 1 ) { mp[s[start]]--; start++; } } maxlength=max(maxlength,i-start+ 1 ); } return maxlength; } };

2. Add Two Numbers

C++ | Basic Approach | 100% success  class Solution { public:     ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {         ListNode *cur1{l1};         ListNode *cur2{l2};         ListNode *start = new ListNode(0);         ListNode *prev = start;         int carry = 0;         while (cur1 || cur2 || carry) {             int sum = (cur1 ? cur1->val : 0) + (cur2 ? cur2->val : 0) + carry;             carry = 0;             if (sum > 9) {                 sum -= 10;                 carry = 1;             }             ListNode *new_node = new ListNode(sum);             prev->next = new_no...

94. Binary Tree Inorder Traversal

C++ |  Beat 100% | Inorder | Recursion  /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}  * };  */ class Solution { public:     vector<int>v;     vector<int> inorderTraversal(TreeNode* root) {         if(root==NULL)             return v;         inorderTraversal(root->left);         v.push_back(root->val);         inorderTraversal(root->right);         return v;     } }...

103. Binary Tree Zigzag Level Order Traversal

C++ | Brute Force |100% Success  | Simple Explaination /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}  * };  */ class Solution { public:     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {         vector<vector<int>>result;                  if(root==NULL)             return result;                  queue<TreeNode*>q;         q.push(root);   ...

162. Find Peak Element

  C++ || Brute Force || 100% Success || Binary Search class Solution { public:     int findPeakElement(vector<int>& nums) {         int s=0;         int e = nums.size()-1;                  int m = s + (e-s)/2;                  while(s<e){             if(nums[m]<nums[m+1]){                 s = m+1;             }             else{                 e = m;             }             m = s+(e-s)/2;         }         return s;     } };

88. Merge Sorted Array

C++ | Simplest Way | Brute Force class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { int i = m- 1 , j = n- 1 , index = m+n- 1 ; while (i>= 0 && j>= 0 ){ if (nums1[i]>nums2[j]) nums1[index--] = nums1[i--]; else nums1[index--] = nums2[j--]; } while (i>= 0 ) nums1[index--] = nums1[i--]; while (j>= 0 ) nums1[index--] = nums2[j--]; } };

81. Search in Rotated Sorted Array II

// Approach 1 Linear Search  class Solution { public:     bool search(vector<int>& nums, int target) {                  for(int i=0;i<nums.size();i++){             if(nums[i]==target)                 return true;         } }; Approach 2 Binary Search First find pivot and select the portion where to move