0261-0270

261. Graph Valid Tree $\star\star$

262. Trips and Users $\star\star\star$

263. Ugly Number $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
 public:
  bool isUgly(int num) {
    if (num == 0) return false;

    vector<int> factors{2, 3, 5};

    for (int factor : factors)
      while (num % factor == 0) num /= factor;

    return num == 1;
  }
};

264. Ugly Number 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
class Solution {
 public:
  int nthUglyNumber(int n) {
    vector<int> nums{1};
    int i2 = 0;
    int i3 = 0;
    int i5 = 0;

    while (nums.size() < n) {
      int next2 = nums[i2] * 2;
      int next3 = nums[i3] * 3;
      int next5 = nums[i5] * 5;
      int next = min(next2, min(next3, next5));
      if (next == next2) ++i2;
      if (next == next3) ++i3;
      if (next == next5) ++i5;
      nums.push_back(next);
    }

    return nums.back();
  }
};

265. Paint House II $\star\star\star$

266. Palindrome Permutation $\star$

267. Palindrome Permutation II $\star\star$

268. Missing Number $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int missingNumber(vector<int>& nums) {
    int ans = nums.size();

    for (int i = 0; i < nums.size(); ++i) ans ^= i ^ nums[i];

    return ans;
  }
};

269. Alien Dictionary $\star\star\star$

270. Closest Binary Search Tree Value $\star$