0441-0450

441. Arranging Coins $\star$

442. Find All Duplicates in an Array $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  vector<int> findDuplicates(vector<int>& nums) {
    vector<int> ans;

    for (int num : nums) {
      nums[abs(num) - 1] *= -1;
      if (nums[abs(num) - 1] > 0) ans.push_back(abs(num));
    }

    return ans;
  }
};

443. String Compression $\star$

444. Sequence Reconstruction $\star\star$

445. Add Two Numbers II $\star\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
class Solution {
 public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    stack<ListNode*> stack1;
    stack<ListNode*> stack2;

    while (l1) {
      stack1.push(l1);
      l1 = l1->next;
    }

    while (l2) {
      stack2.push(l2);
      l2 = l2->next;
    }

    ListNode* head = NULL;
    int carry = 0;

    while (carry || !stack1.empty() || !stack2.empty()) {
      if (!stack1.empty()) {
        carry += stack1.top()->val;
        stack1.pop();
      }
      if (!stack2.empty()) {
        carry += stack2.top()->val;
        stack2.pop();
      }
      ListNode* node = new ListNode(carry % 10);
      node->next = head;
      head = node;
      carry /= 10;
    }

    return head;
  }
};

446. Arithmetic Slices II - Subsequence $\star\star\star$

447. Number of Boomerangs $\star$

448. Find All Numbers Disappeared in an Array $\star$

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

    for (int num : nums) {
      int index = abs(num) - 1;
      nums[index] = -abs(nums[index]);
    }

    for (int i = 0; i < nums.size(); ++i)
      if (nums[i] > 0) ans.push_back(i + 1);

    return ans;
  }
};

449. Serialize and Deserialize BST $\star\star$

450. Delete Node in a BST $\star\star$