Today-To-Do / coding-workbook

0 stars 0 forks source link

Example of daily coding problems and progress #1

Open Today-To-Do opened 5 years ago

Today-To-Do commented 5 years ago

Tasks

【cpp】Solution of 954 ```cpp class Solution { public: bool canReorderDoubled(vector& A) { sort(A.begin(), A.end()); unordered_map hash; for(int i = 0; i < A.size(); i++) { hash[A[i]] += 1; } int k = 0; for(k = 0; k < A.size(); k++) { if(A[k]>=0) break; } int cnt = 0; for(int i = k; i < A.size(); i++) { if(A[i] > 0 && hash[A[i]]) { if(hash[2*A[i]]) { cnt += 1; hash[A[i]] -= 1; hash[2*A[i]] -= 1; } else return false; } else { if(hash[A[i]] % 2 == 0) { cnt += hash[A[i]] / 2; hash[A[i]] = 0; } else return false; } } for(int i = k-1; i>=0; i--) { if(hash[A[i]]) { if(hash[2*A[i]]) { cnt += 1; hash[A[i]] -= 1; hash[2*A[i]] -= 1; } else return false; } } return true; } }; ```
Today-To-Do commented 5 years ago

To create a folded code block, edit the #title and fill your code in by the following format:

<details>
 <summary>#title</summary>

```cpp
# fill your code here

Today-To-Do commented 5 years ago

To Create your own TODO, use the following markdown syntax:

- [ ] [226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)

- [ ] [954. Array of Doubled Pairs](https://leetcode.com/problems/array-of-doubled-pairs/)