MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / isPossible

Function isPossible

STL/unordered_map.cpp:6–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4using namespace std;
5
6bool isPossible(vector<int>& nums)
7{
8 unordered_map<int, int> nc, tail;
9 // 遍历vector
10 for(int & num : nums) {
11 nc[num] ++;
12 }
13 // 遍历unordered_map
14 for(auto & it : nc) {
15 cout << "nc.first: " << it.first << " nc.second: " << it.second << endl;
16 }
17
18 // LeetCode 659:https://leetcode-cn.com/problems/split-array-into-consecutive-subsequences/
19 for(int & num : nums) {
20 if (nc[num] == 0) {
21 continue;
22 }
23 // 这个else if顺序不能调换
24 else if (nc[num] > 0 && nc[num+1] > 0 && nc[num+2] > 0) {
25 nc[num] --;
26 nc[num+1] --;
27 nc[num+2] --;
28 tail[num+2] ++;
29 } else if (nc[num] > 0 && tail[num-1] > 0) {
30 tail[num-1] --;
31 tail[num] ++;
32 nc[num] --;
33 } else {
34 return false;
35 }
36 }
37 // 遍历unordered_map
38 for(auto & it : tail) {
39 cout << "tail.first: " << it.first << " tail.second: " << it.second << endl;
40 }
41 return true;
42}
43
44int main()
45{

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected