class Solution {
public:
int arrangeCoins(int n) {
int res = 0;
while(n > res)
{
res ++;
n = n - res;
}
return res;
}
};
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i = 0; i < nums.size(); i ++)
{
if(nums[abs(nums[i]) - 1] < 0)
res.push_back(abs(nums[i]));
else
nums[abs(nums[i]) - 1] *= -1;
}
return res;
}
};
class Solution {
public:
int compress(vector<char>& chars) {
int size = 0;
for(int left = 0, right = 0; left < chars.size(); left = right)
{
while(right < chars.size() && chars[left] == chars[right])
right ++;
chars[size++] = chars[left];
//cout << right << ' ' << left << endl;
if(right - left > 1)
{
string s = to_string(right - left);
for(auto c : s)
chars[size ++] = c;
}
left = right;
}
return size;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> st1, st2, res;
while(l1) {st1.push(l1->val); l1 = l1->next;}
while(l2) {st2.push(l2->val); l2 = l2->next;}
int cnt = 0; //进位
while(!st1.empty() || !st2.empty())
{
int x = st1.empty() ? 0 : st1.top();
int y = st2.empty() ? 0 : st2.top();
res.push((x + y + cnt) % 10);
cnt = (x + y + cnt) / 10;
if(!st1.empty()) st1.pop();
if(!st2.empty()) st2.pop();
}
if(cnt == 1)
res.push(1);
ListNode* dummy = new ListNode(-1);
ListNode* cur = dummy;
while(!res.empty())
{
cur->next = new ListNode(res.top());
res.pop();
cur = cur->next;
}
return dummy->next;
}
};
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& A) {
vector<unordered_map<long long, int>>dp(A.size());
int res = 0;
for(int i = 1; i < A.size(); i ++)
for(int j = 0; j < i; j ++)
{
long long d = (long long)A[i] - (long long)A[j];
if(dp[j].find(d) != dp[j].end())
{
dp[i][d] += dp[j][d] + 1;
res += dp[j][d];
}
else
dp[i][d] += 1;
}
return res;
}
};
class Solution {
public:
int numberOfBoomerangs(vector<vector<int>>& points) {
int res = 0;
//map<double, int>map;
unordered_map<double, int>map;
for(int i = 0; i < points.size(); i ++)
{
map.clear();//关键
for(int j = 0; j < points.size(); j ++)
{
if(i != j)
{
double d = pow(points[i][0] - points[j][0], 2) + pow(points[i][1] - points[j][1], 2);
map[d] ++;
if(map[d] > 1)
res += 2 * (map[d] - 1);
}
}
}
return res;
}
};
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
for(int i = 0; i < nums.size(); i ++)
nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]);
for(int i = 0; i < nums.size(); i ++)
if(nums[i] > 0)
res.push_back(i + 1);
return res;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root == nullptr) return {};
string str;
queue<TreeNode*>q;
q.push(root);
while(!q.empty())
{
int cnt = q.size();
while(cnt --)
{
TreeNode* node = q.front();
q.pop();
if(node == nullptr)
str += '#';
else
{
str += to_string(node->val) + '!';
q.push(node->left);
q.push(node->right);
}
}
}
return str;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if(data.empty()) return NULL;
int i = 0, j = 0;
while(j < data.size() && data[++ j] != '!');
TreeNode *head = new TreeNode(stoi(data.substr(i, j - i)) );
queue<TreeNode*>q;
q.push(head);
while(i < data.size())
{
TreeNode* node = q.front();
q.pop();
if(node == NULL) continue;
//左节点
i = ++j;
if(i >= data.size()) break;
if(data[i] == '#')
node->left = nullptr;
else{
while(j < data.size() && data[++j] != '!');
node->left = new TreeNode(stoi(data.substr(i, j - i)));
}
q.push(node->left);
//右节点
i = ++ j;
if(i >= data.size()) break;
if(data[i] == '#')
node->right = nullptr;
else{
while(j < data.size() && data[++j] != '!');
node->right = new TreeNode(stoi(data.substr(i, j - i)));
}
q.push(node->right);
}
return head;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root) return 0;
if(key < root->val) //待删节点在左子树
{
root->left = deleteNode(root->left, key);
return root;
}else if(key > root->val)
{
root->right = deleteNode(root->right, key);
return root;
}else{
if(root->left == NULL)
return root->right;
else if(root->right == NULL)
return root->left;
else //左右子树都存在,返回后继节点(右子树最左叶子)作为新的根节点
{
TreeNode* successor = min(root->right);
successor->right = deleteMin(root->right);
successor->left = root->left;
return successor;
}
}
}
TreeNode* min(TreeNode* node)
{
if(node->left == NULL)
return node;
return min(node->left);
}
TreeNode* deleteMin(TreeNode* node)
{
if(node->left == NULL)
return node->right;
node->left = deleteMin(node->left);
return node;
}
};