0231-0240

231. Power of Two $\star$

232. Implement Queue using Stacks $\star$

233. Number of Digit One $\star\star\star$

234. Palindrome Linked List $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
 public:
  bool isPalindrome(ListNode* head) {
    if (!head || !head->next) return true;

    auto slow = head;
    auto fast = head;

    while (fast && fast->next) {
      slow = slow->next;
      fast = fast->next->next;
    }

    if (fast) slow = slow->next;
    slow = reverseList(slow);

    while (slow) {
      if (slow->val != head->val) return false;
      slow = slow->next;
      head = head->next;
    }

    return true;
  }

 private:
  ListNode* reverseList(ListNode* head) {
    ListNode* prev = NULL;
    ListNode* curr = head;

    while (curr) {
      ListNode* next = curr->next;
      curr->next = prev;
      prev = curr;
      curr = next;
    }

    return prev;
  }
};

235. Lowest Common Ancestor of a Binary Search Tree $\star$

236. Lowest Common Ancestor of a Binary Tree $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (!root || root == p || root == q) return root;

    TreeNode* left = lowestCommonAncestor(root->left, p, q);
    TreeNode* right = lowestCommonAncestor(root->right, p, q);

    if (!left) return right;
    if (!right) return left;

    return root;
  }
};

237. Delete Node in a Linked List $\star$

1
2
3
4
5
6
7
class Solution {
 public:
  void deleteNode(ListNode* node) {
    node->val = node->next->val;
    node->next = node->next->next;
  }
};

238. Product of Array Except Self $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
 public:
  vector<int> productExceptSelf(vector<int>& nums) {
    vector<int> ans(nums.size(), 0);
    ans[0] = 1;
    int r = 1;

    for (int i = 1; i < nums.size(); ++i) ans[i] = ans[i - 1] * nums[i - 1];

    for (int i = nums.size() - 1; i >= 0; --i) {
      ans[i] *= r;
      r *= nums[i];
    }

    return ans;
  }
};

239. Sliding Window Maximum $\star\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    vector<int> ans;
    deque<int> deque;

    for (int i = 0; i < nums.size(); ++i) {
      while (!deque.empty() && nums[i] > deque.back()) deque.pop_back();
      deque.push_back(nums[i]);
      if (i - k + 1 >= 0) {
        ans.push_back(deque.front());
        if (nums[i - k + 1] == deque.front()) deque.pop_front();
      }
    }

    return ans;
  }
};

240. Search a 2D Matrix II $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  bool searchMatrix(vector<vector<int>>& matrix, int target) {
    if (matrix.empty()) return false;

    int r = 0;
    int c = matrix[0].size() - 1;

    while (r < matrix.size() && c >= 0) {
      if (matrix[r][c] == target) return true;
      target < matrix[r][c] ? --c : ++r;
    }

    return false;
  }
};