0551-0560

551. Student Attendance Record I $\star$

552. Student Attendance Record II $\star\star\star$

553. Optimal Division $\star\star$

554. Brick Wall $\star\star$

555. Split Concatenated Strings $\star\star$

556. Next Greater Element III $\star\star$

557. Reverse Words in a String III $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  string reverseWords(string s) {
    int i = 0;
    int j = 0;

    while (i < s.length()) {
      while (i < j || i < s.length() && s[i] == ' ') ++i;
      while (j < i || j < s.length() && s[j] != ' ') ++j;
      reverse(s.begin() + i, s.begin() + j);
    }

    return s;
  }
};

558. Quad Tree Intersection $\star$

559. Maximum Depth of N-ary Tree $\star$

560. Subarray Sum Equals K $\star\star$

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

    for (int num : nums) {
      sum += num;
      ans += map[sum - k];
      ++map[sum];
    }

    return ans;
  }
};