repo_name
stringlengths 5
122
| path
stringlengths 3
232
| text
stringlengths 6
1.05M
|
---|---|---|
ooooo-youwillsee/leetcode | 0236-Lowest-Common-Ancestor-of-a-Binary-Tree/cpp_0236/Solution1.h | //
// Created by ooooo on 2019/11/3.
//
#ifndef CPP_0236_SOLUTION1_H
#define CPP_0236_SOLUTION1_H
#include "TreeNode.h"
class Solution {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
if (!root) return NULL;
if (root == p || root == q) return root;
TreeNode *res1 = lowestCommonAncestor(root->left, p, q);
TreeNode *res2 = lowestCommonAncestor(root->right, p, q);
if (res1) {
if (res2) return root;
return res1;
}
if (res2) return res2;
return NULL;
}
};
#endif //CPP_0236_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0415-Add-Strings/cpp_0415/Solution1.h | //
// Created by ooooo on 2020/1/25.
//
#ifndef CPP_0415__SOLUTION1_H_
#define CPP_0415__SOLUTION1_H_
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string addStrings(string num1, string num2) {
string ans = "";
int c1, c2, more = 0;
for (int i = num1.size() - 1, j = num2.size() - 1; i >= 0 || j >= 0; --i, --j) {
c1 = i < 0 ? 0 : num1[i] - '0';
c2 = j < 0 ? 0 : num2[j] - '0';
// % -> 余数
int a = (c1 + c2 + more) % 10;
// / -> 进位
more = (c1 + c2 + more) / 10;
ans += to_string(a);
}
if (more) ans += to_string(more);
reverse(ans.begin(), ans.end());
return ans;
}
};
#endif //CPP_0415__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1760-Minimum Limit of Balls in a Bag/cpp_1760/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/4/14 20:21
*/
#ifndef CPP_1760__SOLUTION1_H_
#define CPP_1760__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minimumSize(vector<int> &nums, int maxOperations) {
int l = 1, r = *max_element(nums.begin(), nums.end());
int ans = 0;
while (l <= r) {
int mid = l + (r - l) / 2;
int cnt = 0;
for (int i = 0; i < nums.size(); i++) {
// 如果nums[i] % mid = 0, 比如9%3=0, 只要分两次就可以了
cnt += (nums[i] - 1) / mid;
}
if (cnt <= maxOperations) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
};
#endif //CPP_1760__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0437-Path-Sum-III/cpp_0437/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/2.
//
#ifndef CPP_0437_SOLUTION1_H
#define CPP_0437_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
#include <queue>
class Solution {
public:
int pathSum(TreeNode *root, int sum) {
if (!root) return 0;
queue<TreeNode *> q;
queue<vector<int>> prevSums;
int count = 0;
q.push(root);
prevSums.push({});
while (!q.empty()) {
vector<int> temp;
TreeNode *node = q.front();
vector<int> sums = prevSums.front();
q.pop();
prevSums.pop();
if (node->val == sum) count++;
temp.push_back(node->val);
for (auto item: sums) {
int num = item + node->val;
if (num == sum) count++;
temp.push_back(num);
}
if (node->left) {
q.push(node->left);
prevSums.push(temp);
}
if (node->right) {
q.push(node->right);
prevSums.push(temp);
}
}
return count;
}
};
#endif //CPP_0437_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0075-Sort-Colors/cpp_0075/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/14.
//
#ifndef CPP_0075__SOLUTION2_H_
#define CPP_0075__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 双指针
*/
class Solution {
public:
void sortColors(vector<int> &nums) {
if (nums.size() <= 1) return;
int i = 0, j = nums.size() - 1, k = 0;
while (k <= j) {
if (nums[k] == 0) {
swap(nums[i++], nums[k++]);
} else if (nums[k] == 1) {
k++;
} else {
swap(nums[j--], nums[k]);
}
}
}
};
#endif //CPP_0075__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution4.h | /**
* @author ooooo
* @date 2020/9/14 18:19
*/
#ifndef CPP_0094__SOLUTION4_H_
#define CPP_0094__SOLUTION4_H_
#include "TreeNode.h"
#include <stack>
/**
* color marker
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
stack<pair<TreeNode *, bool>> s;
s.push({root, false});
vector<int> ans;
while (!s.empty()) {
auto node = s.top().first;
auto flag = s.top().second;
s.pop();
if (flag) {
ans.push_back(node->val);
} else {
if (node->right) {
s.push({node->right, false});
}
s.push({node, true});
if (node->left) {
s.push({node->left, false});
}
}
}
return ans;
}
};
#endif //CPP_0094__SOLUTION4_H_
|
ooooo-youwillsee/leetcode | 0559-Maximum Depth of N-ary Tree/cpp_0559/Solution1.h | //
// Created by ooooo on 2019/12/29.
//
#ifndef CPP_0599_SOLUTION1_H
#define CPP_0599_SOLUTION1_H
#include "Node.h"
class Solution {
private:
int max = 0;
void dfs(Node *node, int level) {
if (!node) return;
max = std::max(max, level);
for (int i = 0; i < node->children.size(); ++i) {
dfs(node->children[i], level + 1);
}
}
public:
int maxDepth(Node *root) {
max = 0;
dfs(root, 1);
return max;
}
};
#endif //CPP_0599_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1603-Design Parking System/cpp_1603/Solution1.h | <filename>1603-Design Parking System/cpp_1603/Solution1.h
/**
* @author ooooo
* @date 2021/3/19 19:25
*/
#ifndef CPP_1603__SOLUTION1_H_
#define CPP_1603__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class ParkingSystem {
public:
int big;
int medium;
int small;
ParkingSystem(int big, int medium, int small) {
this->big= big;
this->medium = medium;
this->small = small;
}
bool addCar(int carType) {
if (carType == 1) {
if (big == 0) return false;
big--;
}else if (carType == 2) {
if (medium == 0) return false;
medium--;
}else {
if (small == 0) return false;
small--;
}
return true;
}
};
#endif //CPP_1603__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0883-Projection-Area-of-3D-Shapes/cpp_0883/Solution1.h | //
// Created by ooooo on 2020/1/15.
//
#ifndef CPP_0883__SOLUTION1_H_
#define CPP_0883__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/**
* 计数 等于一次的单词
*/
class Solution {
public:
vector<string> split(string s) {
vector<string> ans;
s += " ";
int i = 0;
int j = s.find_first_of(" ", i);
while (j != -1) {
ans.push_back(s.substr(i, j - i));
i = j + 1;
j = s.find_first_of(" ", i);
}
return ans;
}
vector<string> uncommonFromSentences(string A, string B) {
unordered_map<string, int> m;
for (auto &word: split(A)) {
m[word]++;
}
for (auto &word: split(B)) {
m[word]++;
}
vector<string> ans;
for (auto &entry: m) {
if (entry.second ==1) ans.push_back(entry.first);
}
return ans;
}
};
#endif //CPP_0883__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1725-Number Of Rectangles That Can Form The Largest Square/cpp_1725/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/1/24 17:06
*/
#ifndef CPP_1725__SOLUTION1_H_
#define CPP_1725__SOLUTION1_H_
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <numeric>
using namespace std;
class Solution {
public:
int largestAltitude(vector<int> &gain) {
int ans = 0, cur = 0;
for (int i = 0; i < gain.size(); ++i) {
cur += gain[i];
ans = max(ans, cur);
}
return ans;
}
};
#endif //CPP_1725__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0788-Rotated-Digits/cpp_0788/Solution2.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/30.
//
#ifndef CPP_0788__SOLUTION2_H_
#define CPP_0788__SOLUTION2_H_
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool valid(int v) {
bool valid = false;
while (v) {
int mod = v % 10;
if (mod == 3 || mod == 4 || mod == 7) return false;
// 有一个数字可以旋转, 说明是有效的
else if (mod == 2 || mod == 5 || mod == 6 || mod == 9) valid = true;
v /= 10;
}
return valid;
}
int rotatedDigits(int N) {
int ans = 0;
for (int i = 1; i <= N; ++i) {
if (valid(i)) ans++;
}
return ans;
}
};
#endif //CPP_0788__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0394-Decode-String/cpp_0394/Solution2.h | <filename>0394-Decode-String/cpp_0394/Solution2.h
//
// Created by ooooo on 2020/2/27.
//
#ifndef CPP_0394__SOLUTION2_H_
#define CPP_0394__SOLUTION2_H_
#include <iostream>
using namespace std;
/**
* dfs
*/
class Solution {
public:
string multipleStr(string s, int count) {
string ans = "";
for (int i = 0; i < count; ++i) {
ans += s;
}
return ans;
}
pair<string, int> dfs(string &s, int i) {
int multi = 0;
string ans = "";
while (i < s.size()) {
if (isdigit(s[i])) {
multi = multi * 10 + s[i] - '0';
} else if (s[i] == '[') {
auto res = dfs(s, i + 1);
ans += multipleStr(res.first, multi);
i = res.second;
multi = 0;
} else if (s[i] == ']') {
return {ans, i};
} else {
ans.push_back(s[i]);
}
i++;
}
return {ans, i};
}
string decodeString(string s) {
return dfs(s, 0).first;
}
};
#endif //CPP_0394__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1734-Decode XORed Permutation/cpp_1734/Solution1.h | /**
* @author ooooo
* @date 2021/3/22 19:55
*/
#ifndef CPP_1734__SOLUTION1_H_
#define CPP_1734__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> decode(vector<int> &encoded) {
int n = encoded.size();
int sum = 0;
for (int i = 1; i <= n + 1; ++i) {
sum ^= i;
}
for (int i = 0; i < n; i += 2) {
sum ^= encoded[i];
}
vector<int> ans(n + 1);
ans[n] = sum;
for (int i = n - 1; i >= 0; --i) {
ans[i] = encoded[i] ^ ans[i + 1];
}
return ans;
}
};
#endif //CPP_1734__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_013/cpp_013/Solution1.h | //
// Created by ooooo on 2020/3/10.
//
#ifndef CPP_013__SOLUTION1_H_
#define CPP_013__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int calc_num(int i, int j) {
int sum = 0;
while (i || j) {
sum += i % 10 + j % 10;
i /= 10;
j /= 10;
}
return sum;
}
bool is_valid(int i, int j) {
return i >= 0 && i < m && j >= 0 && j < n && !marked[i][j] && calc_num(i, j) <= k;
}
int dfs(int i, int j) {
if (!is_valid(i, j)) return 0;
int count = 1;
marked[i][j] = true;
for (int p = 0; p < 4; ++p) {
int dx = i + dx_dy[p][0], dy = j + dx_dy[p][1];
count += dfs(dx, dy);
}
return count;
}
int m, n, k;
vector<vector<int>> dx_dy = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<vector<bool>> marked;
int movingCount(int m, int n, int k) {
this->m = m;
this->n = n;
this->k = k;
this->marked.assign(m, vector<bool>(n, false));
return dfs(0, 0);
}
};
#endif //CPP_013__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0078-Subsets/cpp_0078/Solution2.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/2/14.
//
#ifndef CPP_0078__SOLUTION2_H_
#define CPP_0078__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 回溯
*/
class Solution {
public:
void dfs(int i) {
ans.push_back(num);
for (; i < nums.size(); ++i) {
num.push_back(nums[i]);
dfs(i + 1);
num.pop_back();
}
}
vector<vector<int>> ans;
vector<int> nums, num;
vector<vector<int>> subsets(vector<int> &nums) {
this->nums = nums;
dfs(0);
return ans;
}
};
#endif //CPP_0078__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0112-Path-Sum/cpp_0112/Solution1.h | //
// Created by ooooo on 2019/12/29.
//
#ifndef CPP_0112_SOLUTION1_H
#define CPP_0112_SOLUTION1_H
#include "TreeNode.h"
class Solution {
private:
bool dfs(TreeNode *node, int currentSum, int sum) {
if (!node) return false;
currentSum += node->val;
if (!node->left && !node->right && currentSum == sum) {
return true;
}
return dfs(node->left, currentSum, sum) || dfs(node->right, currentSum, sum);
}
public:
bool hasPathSum(TreeNode *root, int sum) {
return dfs(root, 0, sum);
}
};
#endif //CPP_0112_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0022-Generate-Parentheses/cpp_0022/Solution1.h | //
// Created by ooooo on 2019/11/5.
//
#ifndef CPP_0022_SOLUTION1_H
#define CPP_0022_SOLUTION1_H
#include <vector>
#include <iostream>
#include <string>
using namespace std;
class Solution {
private:
void gen(vector<string> &vec, int left, int right, int n, string result) {
if (left == n && right == n) {
vec.push_back(result);
}
if (left < n) gen(vec, left + 1, right, n, result + "(");
if (right < left) gen(vec, left, right + 1, n, result + ")");
}
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
if (n <= 0) return res;
gen(res, 0, 0, n, "");
return res;
}
};
#endif //CPP_0022_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0947-Most Stones Removed with Same Row or Column/cpp_0947/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/1/15 19:27
*/
#ifndef CPP_0947__SOLUTION2_H_
#define CPP_0947__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
int removeStones(vector<vector<int>> &stones) {
vector<int> p(20002);
for (int i = 0; i < p.size(); ++i) {
p[i] = i;
}
for (int i = 0; i < stones.size(); ++i) {
int x = stones[i][0], y = stones[i][1];
connect(p, x + 10001, y)
}
unordered_set<int> set;
for (auto &stone: stones) {
set.insert(find(p, stone[0] + 10001));
}
return stones.size() - set.size();
}
int find(vector<int> &p, int i) {
if (p[i] == i) return i;
return p[i] = find(p, p[i]);
}
int connect(vector<int> &p, int i, int j) {
int pi = find(p, i), pj = find(p, j);
if (pi == pj) return pi;
p[pi] = pj;
return pj;
}
};
#endif //CPP_0947__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1649-Create Sorted Array through Instructions/cpp_1649/Solution2.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/3/20 18:52
*/
#ifndef CPP_1649__SOLUTION2_H_
#define CPP_1649__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
// 树状数组
class Solution {
public:
struct BIT {
vector<int> data, tree;
BIT(vector<int> data) : data(data.size()), tree(data.size() + 1) {
for (int i = 0; i < data.size(); ++i) {
this->data[i] = data[i];
}
for (int i = 0; i < data.size(); ++i) {
set(i, data[i]);
}
}
void set(int i, int v) {
i++;
while (i < tree.size()) {
tree[i] += v;
i += lowBit(i);
}
}
int query(int i) {
i++;
int sum = 0;
while (i >= 1) {
sum += tree[i];
i -= lowBit(i);
}
return sum;
}
int lowBit(int x) {
return x & -x;
}
};
static constexpr int MOD = 1e9 + 7;
int createSortedArray(vector<int> &instructions) {
vector<int> nums(1e5 + 1);
BIT tree(nums);
long long ans = 0;
for (int i = 0; i < instructions.size(); ++i) {
int less = tree.query(instructions[i] - 1);
int greater = i - nums[instructions[i]] - less;
ans = (ans + min(less, greater)) % MOD;
nums[instructions[i]]++;
tree.set(instructions[i], 1);
}
return ans;
}
}
#endif //CPP_1649__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0189-Rotate-Array/cpp_0189/Solution2.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2019/12/5.
//
#ifndef CPP_0189_SOLUTION2_H
#define CPP_0189_SOLUTION2_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void rotate(vector<int> &nums, int k) {
if (nums.size() == 0) return;
k %= nums.size();
vector<int> res(nums.size());
for (int i = 0; i < nums.size(); ++i) {
int j = (i + k) % nums.size();
res[j] = nums[i];
}
for (int l = 0; l < res.size(); ++l) {
nums[l] = res[l];
}
}
};
#endif //CPP_0189_SOLUTION2_H
|
ooooo-youwillsee/leetcode | lcof_066/cpp_066/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/4/25.
//
#ifndef CPP_066__SOLUTION1_H_
#define CPP_066__SOLUTION1_H_
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> constructArr(vector<int> &a) {
vector<int> ans(a.size(), 0);
int num = 1;
for (int i = 0; i < a.size(); ++i) {
ans[i] = num;
num *= a[i];
}
num = 1;
for (int i = a.size() - 1; i >= 0; --i) {
ans[i] *= num;
num *= a[i];
}
return ans;
}
};
#endif //CPP_066__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0690-Employee-Importance/cpp_0690/Solution2.h | <filename>0690-Employee-Importance/cpp_0690/Solution2.h
//
// Created by ooooo on 2020/1/13.
//
#ifndef CPP_0690__SOLUTION2_H_
#define CPP_0690__SOLUTION2_H_
#include "Employee.h"
#include <unordered_map>
/**
* dfs
*/
class Solution {
public:
int dfs(int id, unordered_map<int, Employee *> &map) {
int sum = map[id]->importance;
for (const auto &subId: map[id]->subordinates) {
sum += dfs(subId, map);
}
return sum;
}
int getImportance(vector<Employee *> employees, int id) {
unordered_map<int, Employee *> map;
for (const auto &item: employees) {
map[item->id] = item;
}
return dfs(id, map);
}
};
#endif //CPP_0690__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_030/cpp_030/Solution2.h | //
// Created by ooooo on 2020/3/19.
//
#ifndef CPP_030__SOLUTION2_H_
#define CPP_030__SOLUTION2_H_
#include <iostream>
#include <stack>
using namespace std;
/**
* 使用一个辅助栈:最小栈
*/
class MinStack {
private:
stack<int> s, min_s;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
if (min_s.empty()) {
min_s.push(x);
} else {
if (x < min_s.top()) {
min_s.push(x);
} else {
min_s.push(min_s.top());
}
}
s.push(x);
}
void pop() {
s.pop();
min_s.pop();
}
int top() {
return s.top();
}
int min() {
return min_s.top();
}
};
#endif //CPP_030__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1128-Number of Equivalent Domino Pairs/cpp_1128/Solution1.h |
/**
* @author ooooo
* @date 2021/1/26 13:53
*/
#ifndef CPP_1128__SOLUTION1_H_
#define CPP_1128__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
int numEquivDominoPairs(vector<vector<int>> &dominoes) {
int ans = 0;
for (int i = 0; i < dominoes.size(); ++i) {
sort(dominoes[i].begin(), dominoes[i].end());
}
sort(dominoes.begin(), dominoes.end());
int cur = 1;
for (int i = 1; i < dominoes.size(); i++) {
if (dominoes[i] == dominoes[i - 1]) {
cur++;
ans += cur - 1;
} else {
cur = 1;
}
}
return ans;
}
};
#endif //CPP_1128__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_055-2/cpp_055-2/Solution1.h | //
// Created by ooooo on 2020/4/11.
//
#ifndef CPP_055_2__SOLUTION1_H_
#define CPP_055_2__SOLUTION1_H_
#include "TreeNode.h"
#include <math.h>
#include <unordered_map>
class Solution {
public:
int getHeight(TreeNode *node) {
if (map.find(node) != map.end()) return map[node];
if (!node) return 0;
return map[node] = max(getHeight(node->left), getHeight(node->right)) + 1;
}
unordered_map<TreeNode *, int> map;
bool isBalanced(TreeNode *root) {
if (!root) return true;
if (abs(getHeight(root->left) - getHeight(root->right)) > 1) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
};
#endif //CPP_055_2__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0669-Trim-a-Binary-Search-Tree/cpp_0669/Solution1.h | //
// Created by ooooo on 2020/1/3.
//
#ifndef CPP_0669_SOLUTION1_H
#define CPP_0669_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
/**
* leetcode heap-use-after-free ???
*/
class Solution {
public:
TreeNode *minimum(TreeNode *node) {
if (!node->left) return node;
return minimum(node->left);
}
TreeNode *removeMin(TreeNode *node) {
//!!! return node->right
if (!node->left) {
TreeNode *right = node->right;
node->right = nullptr;
delete node;
return right;
}
node->left = removeMin(node->left);
return node;
}
TreeNode *trimBST(TreeNode *root, int L, int R) {
if (!root) return nullptr;
if (root->val < L || root->val > R) {
if (!root->left) {
return trimBST(root->right, L, R);
}
if (!root->right) {
return trimBST(root->left, L, R);
}
TreeNode *node = new TreeNode(minimum(root->right)->val);
node->right = removeMin(root->right);
node->left = root->left;
delete root;
return trimBST(node, L, R);
}
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};
#endif //CPP_0669_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0842-Split Array into Fibonacci Sequence/cpp_0842/Solution2.h | <filename>0842-Split Array into Fibonacci Sequence/cpp_0842/Solution2.h
/**
* @author ooooo
* @date 2020/12/8 20:07
*/
#ifndef CPP_0842__SOLUTION2_H_
#define CPP_0842__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class Solution {
public:
long max_v = (int) (pow(2, 31) - 1);
bool dfs(vector<int> &nums, string &s, int i) {
int n = s.size();
if (i >= n && nums.size() >= 3) return true;
long cur = 0;
for (int j = i; j < n; ++j) {
if (j > i && s[i] == '0') {
break;
}
cur = cur * 10 + s[j] - '0';
if (nums.size() >= 2) {
long next_num = (long) nums[nums.size() - 1] + (long) nums[nums.size() - 2];
if (next_num > max_v) break;
if (cur < next_num) {
continue;
} else if (cur > next_num) {
break;
}
}
nums.push_back(cur);
if (dfs(nums, s, j + 1)) return true;
nums.pop_back();
}
return false;
}
vector<int> splitIntoFibonacci(string s) {
vector<int> nums;
dfs(nums, s, 0);
return nums;
}
};
#endif //CPP_0842__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0532-K-diff-Pairs-in-an-Array/cpp_0532/Solution2.h | //
// Created by ooooo on 2020/1/8.
//
#ifndef CPP_0532_SOLUTION2_H
#define CPP_0532_SOLUTION2_H
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
/**
* k=0, map
*/
class Solution {
public:
int findPairs(vector<int> &nums, int k) {
if (nums.empty()) return 0;
int ans = 0;
if (k == 0) {
unordered_map<int, int> m;
for (auto &num: nums) {
if (m.count(num)) m[num] += 1;
else m[num] = 1;
}
for (auto &entry: m) {
if (entry.second >= 2) ans++;
}
return ans;
}
unordered_set<int> s(nums.begin(), nums.end());
nums.assign(s.begin(), s.end());
sort(nums.begin(), nums.end());
for (int j = 0; j < nums.size() - 1; ++j) {
for (int i = j + 1; i < nums.size(); ++i) {
int diff = nums[i] - nums[j];
if (diff == k) ans++;
else if (diff > k) break;
}
}
return ans;
}
};
#endif //CPP_0532_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0443-String-Compression/cpp_0443/Solution1.h | //
// Created by ooooo on 2020/1/26.
//
#ifndef CPP_0443__SOLUTION1_H_
#define CPP_0443__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <map>
using namespace std;
/**
* loop 相邻两项比较
*/
class Solution {
public:
int compress(vector<char> &chars) {
int count = 1, ans = 0;
for (int i = 0; i < chars.size(); ++i) {
// 相邻的两项比较可以在这里判断
if (i == chars.size() - 1 || chars[i] != chars[i + 1]) {
chars[ans++] = chars[i];
if (count > 1) {
for (auto c: to_string(count)) {
chars[ans++] = c;
}
}
count = 1;
} else {
count++;
}
}
return ans;
}
};
#endif //CPP_0443__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1721-Swapping Nodes in a Linked List/cpp_1721/Solution1.h | /**
* @author ooooo
* @date 2021/1/20 13:13
*/
#ifndef CPP_1721__SOLUTION1_H_
#define CPP_1721__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
using namespace std;
class Solution {
public:
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode *swapNodes(ListNode *head, int k) {
vector<ListNode *> nums;
ListNode *dummyHead = new ListNode(-1);
dummyHead->next = head;
ListNode *cur = dummyHead;
while (cur) {
nums.emplace_back(cur);
cur = cur->next;
}
if (nums.size() == 2) return nums[1];
ListNode *node1 = nums[k];
ListNode *node2 = nums[nums.size() - k];
int node1_val = node1->val, node2_val = node2->val;
node2->val = node1_val;
node1->val = node2_val;
return nums[1];
}
};
#endif //CPP_1721__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_016/cpp_016/Solution1.h | //
// Created by ooooo on 2020/3/12.
//
#ifndef CPP_016__SOLUTION1_H_
#define CPP_016__SOLUTION1_H_
#include <iostream>
using namespace std;
/**
* o(n) timeout
*/
class Solution {
public:
double myPow(double x, int n) {
double ans = 1.0;
if (n < 0) return 1.0 / myPow(x, -n);
while (n) {
ans *= x;
n--;
}
return ans;
}
};
#endif //CPP_016__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_014/cpp_014-2/Solution1.h | //
// Created by ooooo on 2020/3/11.
//
#ifndef CPP_014_2__SOLUTION1_H_
#define CPP_014_2__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
/**
* greed, 尽可能的划分为3 和 2 ==> 7 = 2*3 +1 = 3 + 2*2
*/
class Solution {
public:
int cuttingRope(int n) {
if (n <= 3) return n - 1;
int b = n % 3, p = 1000000007;
long rem = 1, x = 3;
for (int a = n / 3 - 1; a > 0; a /= 2) {
if (a % 2 == 1) rem = (rem * x) % p;
x = (x * x) % p;
}
if (b == 0) return (int) (rem * 3 % p);
if (b == 1) return (int) (rem * 4 % p);
return (int) (rem * 6 % p);
}
};
#endif //CPP_014_2__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0845-Longest Mountain in Array/cpp_0845/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/10/25 13:00
*/
#ifndef CPP_0845__SOLUTION2_H_
#define CPP_0845__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int longestMountain(vector<int> &A) {
if (A.size() < 3)return 0;
int m = A.size();
vector<int> left(m, 0);
for (int i = 1; i < A.size(); ++i) {
left[i] = A[i - 1] < A[i] ? left[i - 1] + 1 : 0;
}
vector<int> right(m);
for (int i = m - 2; i >= 0; --i) {
right[i] = A[i + 1] < A[i] ? right[i + 1] + 1 : 0;
}
int ans = 0;
for (int i = 0; i < m; ++i) {
if (left[i] > 0 && right[i] > 0) {
ans = max(ans, left[i] + right[i] + 1);
}
}
return ans;
}
};
#endif //CPP_0845__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0058-Length-of-Last-Word/cpp_0058/Solution2.h | //
// Created by ooooo on 2020/1/25.
//
#ifndef CPP_0058__SOLUTION2_H_
#define CPP_0058__SOLUTION2_H_
#include <iostream>
using namespace std;
/**
* 从尾遍历头
*/
class Solution {
public:
int lengthOfLastWord(string s) {
int end = s.size() - 1;
while (end >= 0 && s[end] == ' ') end--;
if (end < 0) return 0;
int start = end - 1;
while (start >= 0 && s[start] != ' ') start--;
return end - start;
}
};
#endif //CPP_0058__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1822-Sign of the Product of an Array/cpp_1822/Solution1.h | /**
* @author ooooo
* @date 2021/4/16 19:09
*/
#ifndef CPP_1822__SOLUTION1_H_
#define CPP_1822__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <stack>
#include <numeric>
#include <queue>
using namespace std;
class Solution {
public:
int arraySign(vector<int> &nums) {
int n = nums.size();
int x = 1;
for (int i = 0; i < n; i++) {
if (nums[i] < 0) {
x *= -1;
} else if (nums[i] == 0) {
return 0;
}
}
if (x < 0) {
return -1;
} else if (x > 0) {
return 1;
}
return 0;
}
};
#endif //CPP_1822__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0421-Maximum XOR of Two Numbers in an Array/cpp_0421/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/5/16 09:30
*/
#ifndef CPP_0421__SOLUTION1_H_
#define CPP_0421__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
struct Node {
vector<Node *> children;
Node() : children(2) {
}
};
Node *root;
void build(int num) {
Node *cur = root;
for (int i = 0; i < 32; i++) {
if (num & (1 << (31 - i))) {
if (!cur->children[1]) {
cur->children[1] = new Node();
}
cur = cur->children[1];
} else {
if (!cur->children[0]) {
cur->children[0] = new Node();
}
cur = cur->children[0];
}
}
}
int findMaximumXOR(vector<int> &nums) {
root = new Node();
for (auto num :nums) {
build(num);
}
int ans = 0;
for (auto num: nums) {
Node *cur = root;
int x = 0;
for (int i = 0; i < 32; i++) {
int bit = num & (1 << (31 - i));
if (bit) {
if (cur->children[0]) {
cur = cur->children[0];
x |= (1 << (31 - i));
} else {
cur = cur->children[1];
}
} else {
if (cur->children[1]) {
cur = cur->children[1];
x |= (1 << (31 - i));
} else {
cur = cur->children[0];
}
}
if (!cur) {
break;
}
}
ans = max(ans, x);
}
return ans;
}
};
#endif //CPP_0421__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0127-Word-Ladder/cpp_0127/Solution4.h | /**
* @author ooooo
* @date 2020/11/5 17:13
*/
#ifndef CPP_0127__SOLUTION4_H_
#define CPP_0127__SOLUTION4_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
using namespace std;
/**
* timeout
*/
class Solution {
public:
// 返回符合条件的字符串数组,是超时的
vector<string> diffOneLetter(string &s1, unordered_set<string> &wordSet, unordered_set<string> visited) {
vector<string> words;
for (int i = 0; i < s1.size(); ++i) {
char c = s1[i];
for (int j = 'a'; j <= 'z'; ++j) {
s1[i] = j;
if (!visited.count(s1) && wordSet.count(s1)) {
words.push_back(s1);
}
}
s1[i] = c;
}
return words;
}
int ladderLength(string beginWord, string endWord, vector<string> &wordList) {
unordered_set<string> visited, wordSet(wordList.begin(), wordList.end());
visited.insert(beginWord);
queue<string> q;
q.push(beginWord);
int level = 0;
while (!q.empty()) {
level += 1;
for (int i = q.size(); i > 0; --i) {
string u = q.front();
q.pop();
if (u == endWord) {
return level;
}
auto diffWordList = diffOneLetter(u, wordSet, visited);
for (auto &v :diffWordList) {
visited.insert(v);
q.push(v);
}
}
}
return 0;
}
};
#endif //CPP_0127__SOLUTION4_H_
|
ooooo-youwillsee/leetcode | 0012-Integer-to-Romanl/cpp_0012/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/5/14 18:17
*/
#ifndef CPP_0012__SOLUTION2_H_
#define CPP_0012__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string intToRoman(int num) {
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string reps[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string ans = "";
int i = 0;
while (i < sizeof(values) / sizeof(values[0])) {
while (num >= values[i]) {
num -= values[i];
ans += reps[i];
}
i++;
}
return ans;
}
};
#endif //CPP_0012__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/cpp_1290/Solution2.h | //
// Created by ooooo on 2020/1/23.
//
#ifndef CPP_1290__SOLUTION2_H_
#define CPP_1290__SOLUTION2_H_
#include "ListNode.h"
/**
* 二进制加法 i <<1 | xx
*/
class Solution {
public:
int getDecimalValue(ListNode *head) {
int ans = 0;
while (head) {
ans = ans << 1 | head->val;
head = head->next;
}
return ans;
}
};
#endif //CPP_1290__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0050-Pow-x-n/cpp_0050/Solution1.h | //
// Created by ooooo on 2019/11/4.
//
#ifndef CPP_0050_SOLUTION1_H
#define CPP_0050_SOLUTION1_H
class Solution {
private:
double fastPow(double x, long long n) {
if (n == 0) return 1.0;
if (n & 1) return x * fastPow(x, n - 1);
return fastPow(x * x, n / 2);
}
public:
double myPow(double x, int n) {
long long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
return fastPow(x, N);
}
};
#endif //CPP_0050_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1649-Create Sorted Array through Instructions/cpp_1649/Solution1.h | <filename>1649-Create Sorted Array through Instructions/cpp_1649/Solution1.h
/**
* @author ooooo
* @date 2021/3/20 12:34
*/
#ifndef CPP_1649__SOLUTION1_H_
#define CPP_1649__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
// 线段树
class Solution {
public:
struct SegTree {
vector<int> data, tree;
SegTree(vector<int> data) : data(data.size()), tree(data.size() * 4) {
for (int i = 0; i < data.size(); ++i) {
this->data[i] = data[i];
}
if (!data.empty()) {
build(0, 0, data.size() - 1);
}
}
void build(int root, int l, int r) {
if (l > r) return;
if (l == r) {
tree[root] = data[l];
return;
}
int mid = l + (r - l) / 2;
build(left(root), l, mid);
build(right(root), mid + 1, r);
tree[root] = merge(tree[left(root)], tree[right(root)]);
}
int query(int ql, int qr) {
return query(0, 0, data.size() - 1, ql, qr);
}
int query(int root, int l, int r, int ql, int qr) {
if (ql < l || r < qr) return 0;
if (l == ql && r == qr) {
return tree[root];
}
int mid = l + (r - l) / 2;
if (qr <= mid) {
return query(left(root), l, mid, ql, qr);
} else if (ql > mid) {
return query(right(root), mid + 1, r, ql, qr);
} else {
return merge(query(left(root), l, mid, ql, mid), query(right(root), mid + 1, r, mid + 1, qr));
}
}
void set(int i, int v) {
set(0, 0, data.size() - 1, i, v);
}
void set(int root, int l, int r, int i, int v) {
if (l == r && l == i) {
tree[root] = v;
return;
}
int mid = l + (r - l) / 2;
if (i <= mid) {
set(left(root), l, mid, i, v);
} else if (i > mid) {
set(right(root), mid + 1, r, i, v);
}
tree[root] = merge(tree[left(root)], tree[right(root)]);
}
int left(int root) {
return 2 * root + 1;
}
int right(int root) {
return 2 * root + 2;
}
int merge(int x, int y) {
return x + y;
}
};
static constexpr int MOD = 1e9 + 7;
int createSortedArray(vector<int> &instructions) {
vector<int> nums(1e5 + 1);
SegTree tree(nums);
long long ans = 0;
for (int i = 0; i < instructions.size(); ++i) {
int less = tree.query(0, instructions[i] - 1);
int greater = i - nums[instructions[i]] - less;
ans = (ans + min(less, greater)) % MOD;
nums[instructions[i]]++;
tree.set(instructions[i], nums[instructions[i]]);
}
return ans;
}
};
#endif //CPP_1649__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0859-Buddy-Strings/cpp_0859/Solution1.h | <filename>0859-Buddy-Strings/cpp_0859/Solution1.h
//
// Created by ooooo on 2020/1/31.
//
#ifndef CPP_0859__SOLUTION1_H_
#define CPP_0859__SOLUTION1_H_
#include <iostream>
#include <sstream>
#include <unordered_set>
using namespace std;
class Solution {
public:
string simplify(string s) {
stringstream ss;
bool added = true;
ss << s[0];
for (int i = 1; i < s.size(); ++i) {
if (s[i] != s[i - 1]) {
ss << s[i];
}
}
return ss.str();
}
bool buddyStrings(string A, string B) {
if (A.size() != B.size() || A.empty() || B.empty()) return false;
string simpleA = simplify(A);
string simpleB = simplify(B);
unordered_set<char> set;
int left = 0, right = simpleA.size() - 1, len = simpleA.size();
while (left < len && simpleA[left] == simpleB[left]) {
set.insert(simpleA[left]);
left++;
}
// 全部字符都相同, 有重复字符串就可以交换
if (left == len) return simpleA.size() != A.size() || set.size() != simpleA.size();
while (right >= 0 && simpleA[right] == simpleB[right]) {
right--;
}
// 不同的位置一样,无法交换
if (left == right) return false;
return simpleA[left] == simpleB[right] && simpleA[right] == simpleB[left];
}
};
#endif //CPP_0859__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0406-Queue-Reconstruction-by-Height/cpp_0406/Solution1.h | <filename>0406-Queue-Reconstruction-by-Height/cpp_0406/Solution1.h
//
// Created by ooooo on 2020/2/29.
//
#ifndef CPP_0406__SOLUTION1_H_
#define CPP_0406__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>> &people) {
sort(people.begin(), people.end(), [](vector<int> a, vector<int> b) {
return a[0] != b[0] ? a[0] > b[0] : a[1] < b[1];
});
vector<vector<int>> ans;
for (int i = 0; i < people.size(); ++i) {
ans.insert(ans.begin() + people[i][1], people[i]);
}
return ans;
}
};
#endif //CPP_0406__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0674-Longest Continuous Increasing Subsequence/cpp_0674/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/1/24 09:19
*/
#ifndef CPP_0674__SOLUTION1_H_
#define CPP_0674__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int findLengthOfLCIS(vector<int> &nums) {
if (nums.empty()) return 0;
int ans = 1, cur = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i - 1]) {
cur++;
} else {
cur = 1;
}
ans = max(ans, cur);
}
return ans;
}
};
#endif //CPP_0674__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0213-House Robber II/cpp_0213/Solution1.h | <filename>0213-House Robber II/cpp_0213/Solution1.h
/**
* @author ooooo
* @date 2021/4/15 14:11
*/
#ifndef CPP_0213__SOLUTION1_H_
#define CPP_0213__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int rob(vector<int> &nums, int l, int r) {
vector<vector<int>> dp(r - l + 2, vector<int>(2));
dp[0][1] = -1000;
for (int i = l; i <= r; i++) {
dp[i - l + 1][0] = max(dp[i - l][1], dp[i - l][0]);
dp[i - l + 1][1] = max(dp[i - l][0] + nums[i], dp[i - l][1]);
}
return dp[r - l + 1][1];
}
int rob(vector<int> &nums) {
if (nums.size() == 1) return nums[0];
if (nums.size() == 2) return max(nums[0], nums[1]);
int n = nums.size();
int ans = max(rob(nums, 0, n - 2), rob(nums, 1, n - 1));
return ans;
}
};
#endif //CPP_0213__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0424-Longest Repeating Character Replacement/cpp_0424/Solution1.h | /**
* @author ooooo
* @date 2021/2/27 12:18
*/
#ifndef CPP_0424__SOLUTION1_H_
#define CPP_0424__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int characterReplacement(string s, int k) {
int n = s.size();
int l = 0, r = 0;
int maxCnt = 0;
int ans = 0;
vector<int> m(26, 0);
while (r < n) {
m[s[r] - 'A']++;
maxCnt = max(maxCnt, m[s[r] - 'A']);
while (r - l + 1 - maxCnt > k) {
m[s[l] - 'A']--;
l++;
int cnt = 0;
for (int i = 0; i < 26; ++i) {
cnt = max(m[i], cnt);
}
maxCnt = cnt;
}
ans = max(ans, r - l + 1);
r++;
}
return ans;
}
};
#endif //CPP_0424__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0058-Length-of-Last-Word/cpp_0058/Solution1.h | //
// Created by ooooo on 2020/1/25.
//
#ifndef CPP_0058__SOLUTION1_H_
#define CPP_0058__SOLUTION1_H_
#include <iostream>
using namespace std;
/**
* 从头遍历尾
*/
class Solution {
public:
int lengthOfLastWord(string s) {
s += " ";
string ans = "";
int i = 0, j = s.find_first_of(" ");
while (j != -1) {
string word = s.substr(i, j - i);
if (word != "") ans = word;
i = j + 1;
j = s.find_first_of(" ", i);
}
return ans.size();
}
};
#endif //CPP_0058__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0062-Unique-Paths/cpp_0062/Solution2.h | <filename>0062-Unique-Paths/cpp_0062/Solution2.h
//
// Created by ooooo on 2020/2/13.
//
#ifndef CPP_0062__SOLUTION2_H_
#define CPP_0062__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dp[j] = dp[j] + dp[j-1]
* 当前行和上一行的 dp
*/
class Solution {
public:
// n 行 m 列
int uniquePaths(int m, int n) {
vector<int> dp(n, 1);
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
dp[j] += dp[j - 1];
}
}
return dp[n - 1];
}
};
#endif //CPP_0062__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0238-Product-of-Array-Except-Self/cpp_0238/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/22.
//
#ifndef CPP_0238__SOLUTION1_H_
#define CPP_0238__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* left 左边的乘积; right 右边的乘积
*/
class Solution {
public:
vector<int> productExceptSelf(vector<int> &nums) {
int n = nums.size(), num = 1;
vector<int> left(n), right(n);
for (int i = 0; i < n; ++i) {
left[i] = num;
num *= nums[i];
}
num = 1;
for (int j = n - 1; j >= 0; --j) {
right[j] = num;
num *= nums[j];
}
for (int i = 0; i < n; ++i) {
left[i] = left[i] * right[i];
}
return left;
}
};
#endif //CPP_0238__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0381-Insert Delete GetRandom O(1) - Duplicates allowed/cpp_0381/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/10/31 17:20
*/
#ifndef CPP_0381__SOLUTION1_H_
#define CPP_0381__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
class RandomizedCollection {
public:
vector<int> nums;
// key -> 值, value -> 值的下标
unordered_map<int, unordered_set<int>> map;
/** Initialize your data structure here. */
RandomizedCollection() {
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
bool insert(int val) {
bool ans = true;
if (map.find(val) != map.end()) {
ans = false;
}
nums.push_back(val);
map[val].insert(nums.size() - 1);
return ans;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
bool remove(int val) {
if (map.find(val) == map.end()) return false;
int i = *(map[val].begin());
map[val].erase(i);
int last_val = nums.back();
map[last_val].erase(nums.size() - 1);
if (i < nums.size() - 1) {
map[last_val].insert(i);
}
nums[i] = last_val;
nums.pop_back();
if (map[val].empty()) {
map.erase(val);
}
return true;
}
/** Get a random element from the collection. */
int getRandom() {
return nums[rand() % nums.size()];
}
};
#endif //CPP_0381__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_033/cpp_033/Solution2.h | <filename>lcof_033/cpp_033/Solution2.h
//
// Created by ooooo on 2020/3/21.
//
#ifndef CPP_033__SOLUTION2_H_
#define CPP_033__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool help(int l, int r) {
if (l >= r) return true;
int left = 0;
for (; left < r; ++left) {
if (postorder[left] > postorder[r]) break;
}
for (int right = left; right < r; ++right) {
if (postorder[right] < postorder[r]) return false;
}
return help(l, left - 1) && help(left, r - 1);
}
vector<int> postorder;
bool verifyPostorder(vector<int> &postorder) {
if (postorder.empty()) return true;
this->postorder = postorder;
return help(0, postorder.size() - 1);
}
};
#endif //CPP_033__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1768-Merge Strings Alternately/cpp_1768/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/28 13:16
*/
#ifndef INC_1768_MERGE_STRINGS_ALTERNATELY__SOLUTION1_H_
#define INC_1768_MERGE_STRINGS_ALTERNATELY__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string mergeAlternately(string word1, string word2) {
string ans = "";
int i = 0, j = 0;
while (i < word1.size() && j < word2.size()) {
ans += word1[i++];
ans += word2[j++];
}
while (i < word1.size()) {
ans += word1[i++];
}
while (j < word2.size()) {
ans += word2[j++];
}
return ans;
}
};
#endif //INC_1768_MERGE_STRINGS_ALTERNATELY__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1004-Max Consecutive Ones III/cpp_1004/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/19 09:26
*/
#ifndef CPP_1004__SOLUTION1_H_
#define CPP_1004__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 用队列存储位置,空间换时间
class Solution {
public:
int longestOnes(vector<int> &A, int K) {
queue<int> q;
int n = A.size();
int l = 0, r = 0;
int ans = 0;
while (r < n) {
if (A[r] == 0) {
if (K == 0) {
l = r + 1;
r++;
continue;
}
if (q.size() == K) {
l = q.front() + 1;
q.pop();
}
q.push(r);
}
ans = max(ans, r - l + 1);
r++;
}
return ans;
}
};
#endif //CPP_1004__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1673-Find the Most Competitive Subsequence/cpp_1673/Solution1.h | <reponame>ooooo-youwillsee/leetcode<filename>1673-Find the Most Competitive Subsequence/cpp_1673/Solution1.h<gh_stars>10-100
/**
* @author ooooo
* @date 2020/12/26 12:01
*/
#ifndef CPP_1673__SOLUTION1_H_
#define CPP_1673__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
vector<int> mostCompetitive(vector<int> &nums, int k) {
stack<int> stack;
int n = nums.size();
for (int i = 0; i < n; ++i) {
while (!stack.empty() && stack.top() > nums[i] && (n - i + stack.size() - 1) >= k) {
stack.pop();
}
stack.push(nums[i]);
if (stack.size() > k) {
stack.pop();
}
}
vector<int> ans(k);
for (int i = k - 1; i >= 0; --i) {
ans[i] = stack.top();
stack.pop();
}
return ans;
}
};
#endif //CPP_1673__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1669-Merge In Between Linked Lists/cpp_1669/Solution1.h | <filename>1669-Merge In Between Linked Lists/cpp_1669/Solution1.h<gh_stars>10-100
/**
* @author ooooo
* @date 2021/1/24 17:54
*/
#ifndef CPP_1669__SOLUTION1_H_
#define CPP_1669__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
using namespace std;
class Solution {
public:
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode *mergeInBetween(ListNode *list1, int a, int b, ListNode *list2) {
ListNode *dummyHead = new ListNode(-1), *cur = dummyHead;
dummyHead->next = list1;
while (a > 0) {
cur = cur->next;
a--;
b--;
}
ListNode *x = cur;
while (b >= 0) {
cur = cur->next;
b--;
}
ListNode *y = cur->next;
cur = x;
cur->next = list2;
while (cur->next) {
cur = cur->next;
}
cur->next = y;
return dummyHead->next;
}
};
#endif //CPP_1669__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0137-Single Number II/cpp_0137/Solution2.h | /**
* @author ooooo
* @date 2021/4/30 22:47
*/
#ifndef CPP_0137__SOLUTION2_H_
#define CPP_0137__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(vector<int> &nums) {
vector<int> bits(32, 0);
for (int i = 0; i < nums.size(); i++) {
int num = nums[i];
for (int j = 0; j < 32; j++) {
bits[31 - j] += num & 1;
num >>= 1;
}
}
int ans = 0;
for (int i = 0; i < 32; i++) {
int x = bits[i] % 3;
if (x) {
ans = ans | (1 << (31 - i));
}
}
return ans;
}
};
#endif //CPP_0137__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0834-Sum of Distances in Tree/cpp_0834/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/10/6 13:17
*/
#ifndef CPP_0834__SOLUTION2_H_
#define CPP_0834__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <queue>
#include <numeric>
using namespace std;
class Solution {
public:
// dp[i]:以i为根的距离总和
// sz[i]: 以i为根的子节点大小(包括自身)
vector<int> ans, dp, sz;
vector<vector<int>> g;
// 计算dp[0]
void dfs(int u, int end) {
dp[u] = 0; // 子节点到这个节点距离为0
sz[u] = 1; // 包含自身
for (auto &v: g[u]) {
if (v == end) continue;
dfs(v, u);
sz[u] += sz[v];
dp[u] += dp[v] + sz[v] * 1; // 每个节点共享一个
}
}
// 以dp[0]推出其他节点
void dfs2(int u, int end) {
ans[u] = dp[u];
for (auto &v : g[u]) {
if (v == end) continue;
int du = dp[u], dv = dp[v];
int su = sz[u], sv = sz[v];
dp[u] -= (dp[v] + sz[v]);
sz[u] -= sz[v];
dp[v] += (dp[u] + sz[u]);
sz[v] += sz[u];
dfs2(v, u);
dp[u] = du;
dp[v] = dv;
sz[u] = su;
sz[v] = sv;
}
}
vector<int> sumOfDistancesInTree(int N, vector<vector<int>> &edges) {
g.resize(N, {});
ans.resize(N, 0);
dp.resize(N, 0);
sz.resize(N, 0);
for (auto &e: edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
// 传入-1 防止环
dfs(0, -1);
dfs2(0, -1);
return ans;
}
};
#endif //CPP_0834__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1542-Find Longest Awesome Substring/cpp_1542/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/4/6 14:58
*/
#ifndef CPP_1542__SOLUTION1_H_
#define CPP_1542__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int longestAwesome(string s) {
int x = 0;
int n = s.size();
unordered_map<int, int> m = {{0, -1}};
int ans = 0;
for (int i = 0; i < n; i++) {
x ^= 1 << (s[i] - '0');
if (m.find(x) != m.end()) {
ans = max(ans, i - m[x]);
} else {
m[x] = i;
}
for (int j = 0; j < 10; j++) {
int y = x ^(1 << j);
if (m.find(y) != m.end()) {
ans = max(ans, i - m[y]);
}
}
}
return ans;
}
};
#endif //CPP_1542__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0494-Target-Sum/cpp_0494/Solution3.h | <filename>0494-Target-Sum/cpp_0494/Solution3.h
/**
* @author ooooo
* @date 2020/10/6 23:07
*/
#ifndef CPP_0494__SOLUTION3_H_
#define CPP_0494__SOLUTION3_H_
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class Solution {
public:
int findTargetSumWays(vector<int> &nums, int S) {
int n = nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum < S) return 0;
vector<vector<int>> dp(n + 1, vector<int>(2 * sum + 1, 0));
dp[0][nums[0] + sum] = 1;
dp[0][-nums[0] + sum] += 1;
for (int i = 1; i < n; i++) {
for (int j = -sum; j < sum + 1; j++) {
if (j + sum - nums[i] >= 0 && j + sum - nums[i] < 2 * sum + 1) {
dp[i][j + sum] += dp[i - 1][j + sum - nums[i]];
}
if (j + sum + nums[i] >= 0 && j + sum + nums[i] < 2 * sum + 1) {
dp[i][j + sum] += dp[i - 1][j + sum + nums[i]];
}
//cout << " i: " << i << " j: " << j << " :" << dp[i][j + sum] << "\t";
}
//cout << endl;
}
return dp[n - 1][S + sum];
}
};
#endif //CPP_0494__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0005-Longest Palindromic Substring/cpp_0005/Solution1.h | /**
* @author ooooo
* @date 2020/10/4 22:20
*/
#ifndef CPP_0005__SOLUTION1_H_
#define CPP_0005__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
vector<vector<bool>> dp(n + 1, vector<bool>(n + 1, false));
for (int i = 0; i < n; i++) {
dp[i][i] = true;
}
int l = 0, r = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j]) {
dp[i][j] = i + 1 >= j - 1 || dp[i + 1][j - 1];
if (dp[i][j] && j - i > r - l) {
l = i;
r = j;
}
}
}
}
return s.substr(l, r - l + 1);
}
};
#endif //CPP_0005__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_020/cpp_020/Solution1.h | //
// Created by ooooo on 2020/3/14.
//
#ifndef CPP_020__SOLUTION1_H_
#define CPP_020__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <strings.h>
using namespace std;
/**
* A[.[B]][e|EC]
*/
class Solution {
public:
bool scanUnsignedInteger(string &s, int &i) {
int j = i;
while (s[i] >= '0' && s[i] <= '9') i++;
return i > j;
}
bool scanInteger(string &s, int &i) {
if (s[i] == '+' || s[i] == '-') i++;
return scanUnsignedInteger(s, i);
}
bool isNumber(string s) {
if (s.empty()) return false;
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
int i = 0;
bool numeric = scanInteger(s, i);
if (s[i] == '.') {
i++;
numeric = scanUnsignedInteger(s, i) || numeric;
}
if (s[i] == 'e' || s[i] == 'E') {
i++;
numeric = numeric && scanInteger(s, i);
}
return numeric && i == s.size();
}
};
#endif //CPP_020__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0337-House-Robber-III/cpp_0337/Solution3.h | //
// Created by ooooo on 2020/2/25.
//
#ifndef CPP_0337__SOLUTION3_H_
#define CPP_0337__SOLUTION3_H_
#include "TreeNode.h"
#include <unordered_map>
using namespace std;
/**
* max money = max(根节点 + 四个孙子 , 两个儿子)
* A
* / \
* B B
* / \ / \
* C C C C
*
* dp: 0 表示不偷, 1 表示偷
*
* root[0] = max(root.left[0], root.left[1]) + max(root.right[0] , root.right[1])
* root[1] = root.val + root.left[0] + root.right[0]
*/
class Solution {
public:
vector<int> help(TreeNode *node) {
vector<int> ans(2, 0);
if (!node) return ans;
vector<int> l = help(node->left);
vector<int> r = help(node->right);
ans[0] = max(l[0], l[1]) + max(r[0], r[1]);
ans[1] = node->val + l[0] + r[0];
return ans;
}
int rob(TreeNode *root) {
vector<int> ans = help(root);
return max(ans[0], ans[1]);
}
};
#endif //CPP_0337__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0094-Binary-Tree-Inorder-Traversal/cpp_0094/Solution2.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/2/15.
//
#ifndef CPP_0094__SOLUTION2_H_
#define CPP_0094__SOLUTION2_H_
#include "TreeNode.h"
#include <stack>
#include <unordered_set>
/**
* iteration
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
if (!root) return {};
vector<int> ans;
stack<TreeNode *> stack;
unordered_set<TreeNode *> set;
stack.push(root);
TreeNode *curNode;
while (!stack.empty()) {
curNode = stack.top();
while (!set.count(curNode) && curNode->left) {
set.insert(curNode);
stack.push(curNode->left);
curNode = curNode->left;
}
curNode = stack.top();
stack.pop();
ans.push_back(curNode->val);
if (curNode->right) {
stack.push(curNode->right);
}
}
return ans;
}
};
#endif //CPP_0094__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0077-Combinations/cpp_0077/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/9/8 12:06
*/
#ifndef CPP_0077__SOLUTION1_H_
#define CPP_0077__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
vector<vector<int>> ans;
void dfs(vector<int> &vec, int i, int n, int k) {
if (vec.size() == k) {
ans.push_back(vec);
return;
}
while (i <= n) {
vec.push_back(i);
dfs(vec, i + 1, n, k);
vec.pop_back();
i++;
}
}
vector<vector<int>> combine(int n, int k) {
vector<int> vec;
dfs(vec, 1, n, k);
return ans;
}
};
#endif //CPP_0077__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0686-Repeated-String-Match/cpp_0686/Solution1.h | <filename>0686-Repeated-String-Match/cpp_0686/Solution1.h
//
// Created by ooooo on 2020/1/29.
//
#ifndef CPP_0686__SOLUTION1_H_
#define CPP_0686__SOLUTION1_H_
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int i = B.size() % A.size() == 0 ? B.size() / A.size() : B.size() / A.size() + 1;
string s = "";
for (int j = 0; j <= i + 1; ++j) {
// 在循环体内,需要加+1,比较最后一次
if (j >= i && s.find(B) != -1) {
return j;
}
s += A;
}
return -1;
}
};
#endif //CPP_0686__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0131-Palindrome Partitioning/cpp_0131/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/3/7 07:55
*/
#ifndef CPP_0131__SOLUTION1_H_
#define CPP_0131__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<string>> dfs(string &s, int i) {
if (i == s.size()) return {};
vector<vector<string>> ans;
for (int len = 1; len <= s.size() - i; ++len) {
if (dp[i][i + len - 1]) {
vector<vector<string>> res = dfs(s, i + len);
if (res.empty()) {
ans.push_back({s.substr(i, i + len)});
}
for (auto &row : res) {
ans.push_back({s.substr(i, len)});
ans.back().insert(ans.back().end(), row.begin(), row.end());
}
}
}
return ans;
}
vector<vector<bool>> dp;
vector<vector<string>> partition(string s) {
// 动态规划判断回文串
int n = s.size();
dp.assign(n, vector<bool>(n));
for (int i = 0; i < n; ++i) {
dp[i][i] = true;
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
if (j - i > 1) {
dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j];
} else {
dp[i][j] = s[i] == s[j];
}
}
}
return dfs(s, 0);
}
};
#endif //CPP_0131__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0771-Jewels-and-Stones/cpp_0771/SolutIon1.h | //
// Created by ooooo on 2020/1/15.
//
#ifndef CPP_0771__SOLUTION1_H_
#define CPP_0771__SOLUTION1_H_
#include <iostream>
#include <unordered_set>
#include <numeric>
using namespace std;
class Solution {
public:
int numJewelsInStones(string J, string S) {
unordered_set<char> s(J.begin(), J.end());
return accumulate(S.begin(), S.end(), 0, [&](int x, int y) { return s.count(y) ? x + 1 : x; });
}
};
#endif //CPP_0771__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0148-Sort-List/cpp_0148/Solution2.h | <filename>0148-Sort-List/cpp_0148/Solution2.h
//
// Created by ooooo on 2020/2/18.
//
#ifndef CPP_0148__SOLUTION2_H_
#define CPP_0148__SOLUTION2_H_
#include "ListNode.h"
/**
* 复杂度要求 O(nlogn) ==> 归并排序
* 自底向上
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
ListNode *cur = head;
int size = 0;
while (cur) {
cur = cur->next;
size++;
}
ListNode *res = new ListNode(0);
res->next = head;
for (int i = 1; i < size; i *= 2) {
ListNode *prev = res, *h = res->next;
int j = 0;
while (h) {
ListNode *h1 = h;
j = i;
while (j && h) {
h = h->next;
j--;
}
// 如果 j 不为 0,说明没有后面的元素,不用比较
if (j) break;
ListNode *h2 = h;
j = i;
while (j && h) {
h = h->next;
j--;
}
int c1 = i, c2 = i - j;
while (c1 && c2) {
if (h1->val <= h2->val) {
prev->next = h1;
h1 = h1->next;
c1--;
} else {
prev->next = h2;
h2 = h2->next;
c2--;
}
prev = prev->next;
}
prev->next = c1 ? h1 : h2;
while (c1 > 0 || c2 > 0) {
prev = prev->next;
c1--;
c2--;
}
prev->next = h;
}
}
return res->next;
}
};
#endif //CPP_0148__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0057-Insert Interval/cpp_0057/Solution1.h | /**
* @author ooooo
* @date 2020/11/4 17:26
*/
#ifndef CPP_0057__SOLUTION1_H_
#define CPP_0057__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>> &intervals, vector<int> &newInterval) {
if (intervals.empty()) return {newInterval};
vector<vector<int>> ans;
int left = newInterval[0], right = newInterval[1];
bool inserted = false;
for (int i = 0; i < intervals.size(); ++i) {
int l = intervals[i][0], r = intervals[i][1];
if (right < l) {
if (!inserted) {
ans.push_back({left, right});
inserted = true;
}
ans.push_back(intervals[i]);
} else if (left > r) {
ans.push_back(intervals[i]);
} else {
left = min(left, l);
right = max(right, r);
}
}
if (!inserted) {
ans.push_back({left, right});
}
return ans;
}
};
#endif //CPP_0057__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0081-Search in Rotated Sorted Array II/cpp_0081/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/4/7 10:16
*/
#ifndef CPP_0081__SOLUTION1_H_
#define CPP_0081__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool search(vector<int> &nums, int target) {
int n = nums.size();
int l = 0, r = n - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[mid] < nums[r]) {
if (target >= nums[mid] && target <= nums[r]) {
auto it = lower_bound(nums.begin() + mid, nums.begin() + r + 1, target);
return it != nums.end() && *it == target;
} else {
r = mid;
}
} else if (nums[mid] > nums[l]) {
if (target >= nums[l] && target <= nums[mid]) {
auto it = lower_bound(nums.begin() + l, nums.begin() + mid + 1, target);
return it != nums.end() && *it == target;
} else {
l = mid;
}
} else {
while (l <= r) {
if (nums[l] == target) {
return true;
}
l++;
}
}
}
return false;
}
};
#endif //CPP_0081__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0040-Combination-Sum-II/cpp_0040/Solution1.h | /**
* @author ooooo
* @date 2020/9/10 14:39
*/
#ifndef CPP_0040__SOLUTION1_H_
#define CPP_0040__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
struct Vec_Hash {
size_t operator()(vector<int> vec) const {
size_t ret = 0;
for (int i = 0; i < vec.size(); ++i) {
ret += hash<int>()(vec[i]);
}
return ret;
}
};
vector<int> candidates;
unordered_set<vector<int>, Vec_Hash> ans;
void dfs(vector<int> &vec, int i, int target) {
if (target == 0) {
ans.insert(vec);
return;
}
while (i < candidates.size()) {
if (target < candidates[i]) return;
vec.push_back(candidates[i]);
dfs(vec, i + 1, target - candidates[i]);
vec.pop_back();
i++;
}
}
vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
this->candidates = candidates;
vector<int> vec;
dfs(vec, 0, target);
vector<vector<int>> nums;
for (auto &num: ans) nums.push_back(num);
return nums;
}
};
#endif //CPP_0040__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0897-Increasing-Order-Search-Tree/cpp_0897/Solution2.h | /**
* @author ooooo
* @date 2021/4/25 09:47
*/
#ifndef CPP_0897__SOLUTION2_H_
#define CPP_0897__SOLUTION2_H_
#include "TreeNode.h"
class Solution {
public:
TreeNode *increasingBST(TreeNode *root) {
if (!root) return nullptr;
if (!root->left && !root->right) return root;
TreeNode *l = increasingBST(root->left);
TreeNode *r = increasingBST(root->right);
root->left = nullptr;
root->right = r;
if (!l) return root;
TreeNode *node = l;
while (node->right) {
node = node->right;
}
node->right = root;
return l;
}
};
#endif //CPP_0897__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0120-Triangle/cpp_0120/Solution1.h | //
// Created by ooooo on 2019/12/16.
//
#ifndef CPP_0120_SOLUTION1_H
#define CPP_0120_SOLUTION1_H
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
/**
* 可能会超出时间限制
*/
class Solution {
private:
int min = INT_MAX;
void dfs(vector<vector<int>> &vec, int row, int col, int sum) {
if (col >= vec[row].size()) return;
sum += vec[row][col];
if (row == vec.size() - 1) {
min = min < sum ? min : sum;
} else {
dfs(vec, row + 1, col, sum);
dfs(vec, row + 1, col + 1, sum);
}
}
public:
int minimumTotal(vector<vector<int>> &triangle) {
dfs(triangle, 0, 0, 0);
return min;
}
};
#endif //CPP_0120_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0021-Merge-Two-Sorted-Lists/cpp_0021/Solution1.h | //
// Created by ooooo on 2020/1/23.
//
#ifndef CPP_0021__SOLUTION1_H_
#define CPP_0021__SOLUTION1_H_
#include "ListNode.h"
/**
* 每个节点重新new
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode *dummyHead = new ListNode(0), *cur = dummyHead;
while (l1 && l2) {
if (l1->val == l2->val) {
cur->next = new ListNode(l1->val);
cur = cur->next;
cur->next = new ListNode(l2->val);
cur = cur->next;
l1 = l1->next;
l2 = l2->next;
} else if (l1->val < l2->val) {
cur->next = new ListNode(l1->val);
cur = cur->next;
l1 = l1->next;
} else {
cur->next = new ListNode(l2->val);
cur = cur->next;
l2 = l2->next;
}
}
while (l1) {
cur->next = new ListNode(l1->val);
cur = cur->next;
l1 = l1->next;
}
while (l2) {
cur->next = new ListNode(l2->val);
cur = cur->next;
l2 = l2->next;
}
return dummyHead->next;
}
};
#endif //CPP_0021__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0987-Vertical Order Traversal of a Binary Tree/cpp_0987/Solution2.h | /**
* @author ooooo
* @date 2021/2/17 17:21
*/
#ifndef CPP_0987__SOLUTION2_H_
#define CPP_0987__SOLUTION2_H_
#include "TreeNode.h"
class Solution {
public:
void dfs(TreeNode *root, int col, int row) {
if (!root) {
return;
}
m[col][row].push_back(root->val);
dfs(root->left, col - 1, row + 1);
dfs(root->right, col + 1, row + 1);
}
map<int, map<int, vector<int>>> m;
vector<vector<int>> verticalTraversal(TreeNode *root) {
if (!root) return {};
dfs(root, 0, 0);
vector<vector<int>> ans;
for (auto &[c, col]:m) {
vector<int> tmp;
for (auto &[r, row] :col) {
sort(row.begin(), row.end());
tmp.insert(tmp.end(), row.begin(), row.end());
}
ans.emplace_back(tmp);
}
return ans;
}
};
#endif //CPP_0987__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0392-Is-Subsequence/cpp_0392/Solution2.h | //
// Created by ooooo on 2020/1/18.
//
#ifndef CPP_0392__SOLUTION2_H_
#define CPP_0392__SOLUTION2_H_
#include <iostream>
using namespace std;
/**
* find_first_of
*/
class Solution {
public:
bool isSubsequence(string s, string t) {
int i = 0;
for (auto c:s) {
int j = t.find_first_of(c, i);
if (j == -1) return false;
i = j + 1;
}
return true;
}
};
#endif //CPP_0392__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_063/cpp_063/Solution2.h | //
// Created by ooooo on 2020/4/24.
//
#ifndef CPP_063__SOLUTION2_H_
#define CPP_063__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.empty()) return 0;
int n = prices.size(), ans = 0, min = prices[0];
for (int i = 1; i < n; ++i) {
if (prices[i] < min) min = prices[i];
ans = max(ans, prices[i] - min);
}
return ans;
}
};
#endif //CPP_063__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0110-Balanced Binary Tree/cpp_0110/Solution1.h | <filename>0110-Balanced Binary Tree/cpp_0110/Solution1.h<gh_stars>10-100
//
// Created by ooooo on 2019/12/28.
//
#ifndef CPP_0110_SOLUTION1_H
#define CPP_0110_SOLUTION1_H
#include "TreeNode.h"
class Solution {
private:
int depth(TreeNode *node) {
if (!node) return 0;
return max(depth(node->left), depth(node->right)) + 1;
}
public:
bool isBalanced(TreeNode *root) {
if (!root) return true;
return abs(depth(root->left) - depth(root->right)) <= 1 &&
isBalanced(root->left) && isBalanced(root->right);
}
};
#endif //CPP_0110_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0447-Number-of-Boomerangs/cpp_0447/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/11.
//
#ifndef CPP_0447__SOLUTION1_H_
#define CPP_0447__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <cmath>
#include <unordered_map>
using namespace std;
class Solution {
public:
int distance(vector<int> a, vector<int> b) {
return pow(b[0] - a[0], 2) + pow(b[1] - a[1], 2);
}
int numberOfBoomerangs2(vector<vector<int>> &points) {
unordered_map<int, int> m;
int ans = 0;
for (int i = 0; i < points.size(); ++i) {
m.clear();
for (int j = 0; j < points.size(); ++j) {
if (i == j) continue;
m[distance(points[i], points[j])]++;
}
for (auto &entry: m) {
if (entry.second >= 2) {
ans += entry.second * (entry.second - 1);
}
}
}
return ans;
}
int numberOfBoomerangs(vector<vector<int>> &points) {
unordered_map<int, int> m;
int ans = 0;
for (int i = 0; i < points.size(); ++i) {
m.clear();
for (int j = 0; j < points.size(); ++j) {
if (i == j) continue;
int dis = distance(points[i], points[j]);
m[dis]++;
if (m[dis] > 1) {
// 每添加一个节点,就是添加n-1种排列,再加上可以两两交换( *2 )
ans += 2 * (m[dis] - 1);
}
}
}
return ans;
}
};
#endif //CPP_0447__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1002-Find-Common-Characters/cpp_1002/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/16.
//
#ifndef CPP_1002__SOLUTION1_H_
#define CPP_1002__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<string> commonChars(vector<string> &A) {
vector<unordered_map<char, int>> wordMaps(A.size());
for (int i = 0; i < A.size(); ++i) {
for (auto &c: A[i]) {
wordMaps[i][c]++;
}
}
vector<string> ans;
for (auto &c: A[0]) {
bool find = true;
for (auto &m: wordMaps) {
if (!m[c]) {
find = false;
break;
}
m[c]--;
}
if (find) {
ans.push_back(string(1, c));
}
}
return ans;
}
};
#endif //CPP_1002__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_039/cpp_039/Solution2.h | //
// Created by ooooo on 2020/3/24.
//
#ifndef CPP_039__SOLUTION2_H_
#define CPP_039__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* partition timeout
*/
class Solution {
public:
int partition(int i, int j, vector<int> &nums) {
int k = i, item = nums[i];
for (int p = i + 1; p <= j; p++) {
if (nums[p] <= item) swap(nums[++k], nums[p]);
}
swap(nums[k], nums[i]);
return k;;
}
int majorityElement(vector<int> &nums) {
int start = 0, end = nums.size() - 1;
auto k = partition(start, end, nums);
int mid = nums.size() / 2;
while (k != mid) {
if (k < mid) {
start = k + 1;
k = partition(start, end, nums);
} else {
end = k - 1;
k = partition(start, end, nums);
}
}
return nums[k];
}
};
#endif //CPP_039__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_047/cpp_047/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/3.
//
#ifndef CPP_047__SOLUTION1_H_
#define CPP_047__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dfs timeout
*/
class Solution {
public:
void dfs(int i, int j, int sum) {
if (i >= m || j >= n) return;
sum += grid[i][j];
if (i == m - 1 && j == n - 1) {
ans = max(ans, sum);
return;
}
dfs(i, j + 1, sum);
dfs(i + 1, j, sum);
}
vector<vector<int>> grid;
int m = 0, n = 0, ans = 0;
int maxValue(vector<vector<int>> &grid) {
if (grid.empty()) return 0;
this->grid = grid;
this->m = grid.size();
this->n = grid[0].size();
dfs(0, 0, 0);
return ans;
}
};
#endif //CPP_047__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0065-Valid Number/cpp_0065/Solution1.h | <filename>0065-Valid Number/cpp_0065/Solution1.h
//
// Created by ooooo on 6/17/2021.
//
#ifndef CPP_0065_SOLUTION1_H
#define CPP_0065_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
/**
* 直接复制的
*/
class Solution {
public:
enum State {
STATE_INITIAL,
STATE_INT_SIGN,
STATE_INTEGER,
STATE_POINT,
STATE_POINT_WITHOUT_INT,
STATE_FRACTION,
STATE_EXP,
STATE_EXP_SIGN,
STATE_EXP_NUMBER,
STATE_END
};
enum CharType {
CHAR_NUMBER,
CHAR_EXP,
CHAR_POINT,
CHAR_SIGN,
CHAR_ILLEGAL
};
CharType toCharType(char ch) {
if (ch >= '0' && ch <= '9') {
return CHAR_NUMBER;
} else if (ch == 'e' || ch == 'E') {
return CHAR_EXP;
} else if (ch == '.') {
return CHAR_POINT;
} else if (ch == '+' || ch == '-') {
return CHAR_SIGN;
} else {
return CHAR_ILLEGAL;
}
}
bool isNumber(string s) {
unordered_map <State, unordered_map<CharType, State>> transfer{
{
STATE_INITIAL, {
{CHAR_NUMBER, STATE_INTEGER},
{CHAR_POINT, STATE_POINT_WITHOUT_INT},
{CHAR_SIGN, STATE_INT_SIGN}
}
},
{
STATE_INT_SIGN, {
{CHAR_NUMBER, STATE_INTEGER},
{CHAR_POINT, STATE_POINT_WITHOUT_INT}
}
},
{
STATE_INTEGER, {
{CHAR_NUMBER, STATE_INTEGER},
{CHAR_EXP, STATE_EXP},
{CHAR_POINT, STATE_POINT}
}
},
{
STATE_POINT, {
{CHAR_NUMBER, STATE_FRACTION},
{CHAR_EXP, STATE_EXP}
}
},
{
STATE_POINT_WITHOUT_INT, {
{CHAR_NUMBER, STATE_FRACTION}
}
},
{
STATE_FRACTION,
{
{CHAR_NUMBER, STATE_FRACTION},
{CHAR_EXP, STATE_EXP}
}
},
{
STATE_EXP,
{
{CHAR_NUMBER, STATE_EXP_NUMBER},
{CHAR_SIGN, STATE_EXP_SIGN}
}
},
{
STATE_EXP_SIGN, {
{CHAR_NUMBER, STATE_EXP_NUMBER}
}
},
{
STATE_EXP_NUMBER, {
{CHAR_NUMBER, STATE_EXP_NUMBER}
}
}
};
int len = s.length();
State st = STATE_INITIAL;
for (int i = 0; i < len; i++) {
CharType typ = toCharType(s[i]);
if (transfer[st].find(typ) == transfer[st].end()) {
return false;
} else {
st = transfer[st][typ];
}
}
return st == STATE_INTEGER || st == STATE_POINT || st == STATE_FRACTION || st == STATE_EXP_NUMBER ||
st == STATE_END;
}
};
#endif //CPP_0065_SOLUTION1_H
|
ooooo-youwillsee/leetcode | lcof_058-1/cpp_058-1/Solution1.h | //
// Created by ooooo on 2020/4/18.
//
#ifndef CPP_058_1__SOLUTION1_H_
#define CPP_058_1__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class Solution {
public:
int findFirstLetter(string &s, int i) {
for (; i < s.size(); i++) {
if (s[i] != ' ') return i;
}
return -1;
}
string reverseWords(string s) {
s += " ";
vector<string> str_s;
int i = 0, j = 0;
while ((i = findFirstLetter(s, i)) != -1) {
j = s.find(" ", i);
if (j == -1) break;
str_s.emplace_back(s.substr(i, j - i));
i = j + 1;
}
stringstream ss;
for (i = str_s.size() - 1; i >= 0; --i) {
ss << str_s[i];
if (i != 0) ss << " ";
}
return ss.str();
}
};
#endif //CPP_058_1__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0113-Path-Sum-II/cpp_0113/TreeNode.h | <filename>0113-Path-Sum-II/cpp_0113/TreeNode.h
/**
* @author ooooo
* @date 2020/9/26 00:12
*/
#ifndef CPP_0113__TREENODE_H_
#define CPP_0113__TREENODE_H_
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
#endif //CPP_0113__TREENODE_H_
|
ooooo-youwillsee/leetcode | 0657-Robot-Return-to-Origin/cpp_0657/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/28.
//
#ifndef CPP_0657__SOLUTION1_H_
#define CPP_0657__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
bool judgeCircle(string moves) {
int i = 0, j = 0;
for (auto move: moves) {
if (move == 'U') i++;
else if(move == 'D') i--;
else if (move == 'R') j++;
else j--;
}
return i == 0 && j == 0;
}
};
#endif //CPP_0657__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0142-Linked-List-Cycle-II/cpp_0142/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/17.
//
#ifndef CPP_0142__SOLUTION2_H_
#define CPP_0142__SOLUTION2_H_
#include "LIstNode.h"
/**
* 快慢指针
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *low = head, *fast = head;
bool hasCycle = false;
while (fast && fast->next) {
low = low->next;
fast = fast->next->next;
if (low == fast) {
hasCycle = true;
break;
}
}
if (!hasCycle) return nullptr;
low = head;
while (low != fast) {
low = low->next;
fast = fast->next;
}
return low;
}
};
#endif //CPP_0142__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0645-Set-Mismatch/cpp_0645/Solution1.h | //
// Created by ooooo on 2020/1/13.
//
#ifndef CPP_0645__SOLUTION1_H_
#define CPP_0645__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
vector<int> findErrorNums(vector<int> &nums) {
unordered_set<int> s;
int i = 0, sum = 0;
for (auto &num: nums) {
if (s.count(num)) {
// i 为重复值
i = num;
} else s.insert(num);
sum += num;
}
int j = (1 + nums.size()) * nums.size() / 2 + i - sum;
return {i, j};
}
};
#endif //CPP_0645__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution1.h | <filename>0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution1.h
//
// Created by ooooo on 2020/2/16.
//
#ifndef CPP_0114__SOLUTION1_H_
#define CPP_0114__SOLUTION1_H_
#include "TreeNode.h"
class Solution {
public:
TreeNode *help(TreeNode *node) {
if (!node) return nullptr;
TreeNode *left = help(node->left);
TreeNode *right = help(node->right);
// 指针的引用置空
node->left = nullptr;
node->right = nullptr;
if (!left && !right) {
return node;
} else if (!left) {
node->right = right;
} else if (!right) {
node->right = left;
} else {
node->right = left;
TreeNode *cur = node;
while (cur->right) {
cur = cur->right;
}
cur->right = right;
}
return node;
}
void flatten(TreeNode *root) {
help(root);
}
};
#endif //CPP_0114__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0023-Merge-k-Sorted-Lists/cpp_0023/Solution1.h | <filename>0023-Merge-k-Sorted-Lists/cpp_0023/Solution1.h<gh_stars>10-100
/**
* @author ooooo
* @date 2020/9/25 22:36
*/
#ifndef CPP_0023__SOLUTION1_H_
#define CPP_0023__SOLUTION1_H_
#endif //CPP_0023__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
struct Comp {
bool operator()(const ListNode * __x, const ListNode * __y) const { return __x->val >= __y->val; }
};
ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode *, vector<ListNode *>, Comp> q;
for (int i = 0; i < lists.size(); ++i) {
if (lists[i]) {
q.push(lists[i]);
}
}
ListNode *dummyHead = new ListNode(), *cur = dummyHead;
while (!q.empty()) {
auto node = q.top();
q.pop();
cur->next = node;
cur = cur->next;
if (node->next)
q.push(node->next);
}
return dummyHead->next;
}
};
|
ooooo-youwillsee/leetcode | 0599-Minimum-Index-Sum-of-Two-Lists/cpp_0599/Solution1.h | //
// Created by ooooo on 2020/1/12.
//
#ifndef CPP_0599__SOLUTION1_H_
#define CPP_0599__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<string> findRestaurant(vector<string> &list1, vector<string> &list2) {
unordered_map<string, int> m;
vector<string> ans;
int min = INT_MAX;
for (int i = 0; i < list1.size(); ++i) {
m[list1[i]] = i;
}
for (int i = 0; i < list2.size(); ++i) {
if (m.count(list2[i])) {
int sum = i + m[list2[i]];
if (sum < min) {
// 新的最小索引值
ans.clear();
min = sum;
ans.push_back(list2[i]);
} else if (sum == min) {
ans.push_back(list2[i]);
}
}
}
return ans;
}
};
#endif //CPP_0599__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0530-Minimum-Absolute-Difference-in-BST/cpp_0530/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/2.
//
#ifndef CPP_0530_SOLUTION1_H
#define CPP_0530_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
#include <climits>
class Solution {
public:
void dfs(TreeNode *node, vector<int> &vec) {
if (!node) return;
dfs(node->left, vec);
vec.push_back(node->val);
dfs(node->right, vec);
}
int getMinimumDifference(TreeNode *root) {
vector<int> vec;
dfs(root, vec);
int min = INT_MAX;
for (int i = vec.size() - 1; i > 0; --i) {
int diff = vec[i] - vec[i - 1];
min = diff < min ? diff : min;
}
return min == INT_MAX ? 0 : min;
}
};
#endif //CPP_0530_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0671-Second-Minimum-Node-In-a-Binary-Tree/cpp_0671/Solution2.h | //
// Created by ooooo on 2020/1/4.
//
#ifndef CPP_0671_SOLUTION2_H
#define CPP_0671_SOLUTION2_H
#include "TreeNode.h"
#include <queue>
class Solution {
public:
int findSecondMinimumValue(TreeNode *root) {
if (!root) return -1;
queue<TreeNode *> q;
q.push(root);
// flag 定义最小值是否有INT_MAX
int res = INT_MAX, flag = 0;
while (!q.empty()) {
TreeNode *node = q.front();
q.pop();
if (node->val > root->val) {
res = std::min(res, node->val);
flag = node->val == INT_MAX ? 1 : flag;
}
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
return res == INT_MAX && !flag ? -1 : res;
}
};
#endif //CPP_0671_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1006-Clumsy Factorial/cpp_1006/Solution2.h | <reponame>ooooo-youwillsee/leetcode<filename>1006-Clumsy Factorial/cpp_1006/Solution2.h
/**
* @author ooooo
* @date 2021/4/1 18:03
*/
#ifndef CPP_1006__SOLUTION2_H_
#define CPP_1006__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int clumsy(int N) {
if (N == 1) {
return N;
} else if (N == 2) {
return N * (N - 1);
} else if (N == 3) {
return N * (N - 1) / (N - 2);
}
int sum = N * (N - 1) / (N - 2);
N -= 3;
while (N >= 4) {
sum += N - (N - 1) * (N - 2) / (N - 3);
N -= 4;
}
return sum + (N > 0 ? 1 : 0);
}
};
#endif //CPP_1006__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0150-Evaluate Reverse Polish Notation/cpp_0150/Solution1.h | /**
* @author ooooo
* @date 2021/3/20 12:13
*/
#ifndef CPP_0150__SOLUTION1_H_
#define CPP_0150__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
for (int i = 0; i < tokens.size(); ++i) {
if (tokens[i] == "+") {
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(y + x);
} else if (tokens[i] == "-") {
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(y - x);
} else if (tokens[i] == "*") {
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(y * x);
} else if (tokens[i] == "/") {
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(y / x);
} else {
s.push(stoi(tokens[i]));
}
}
return s.top();
}
};
#endif //CPP_0150__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcci_10_03/cpp_10_03/Solution1.h | /**
* @author ooooo
* @date 2021/4/10 11:48
*/
#ifndef CPP_10_03__SOLUTION1_H_
#define CPP_10_03__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int search(vector<int> &arr, int target) {
int n = arr.size();
int l = 0, r = n - 1;
int ans = INT_MAX;
while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[l] < arr[mid]) {
if (arr[l] <= target && target <= arr[mid]) {
auto it = lower_bound(arr.begin() + l, arr.begin() + mid + 1, target);
if (it != arr.end() && *it == target) {
ans = min(ans, (int) (it - arr.begin()));
}
}
l = mid + 1;
} else if (arr[mid] < arr[r]) {
if (arr[mid] <= target && target <= arr[r]) {
auto it = lower_bound(arr.begin() + mid, arr.begin() + r + 1, target);
if (it != arr.end() && *it == target) {
ans = min(ans, (int) (it - arr.begin()));
}
}
r = mid;
} else {
while (l <= r) {
if (arr[l] == target) {
ans = min(l, ans);
}
l++;
}
}
}
return ans == INT_MAX ? -1 : ans;
}
};
#endif //CPP_10_03__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0929-Unique-Email-Addresses/cpp_0929/Solution1.h | //
// Created by ooooo on 2020/2/1.
//
#ifndef CPP_0929__SOLUTION1_H_
#define CPP_0929__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <sstream>
using namespace std;
class Solution {
public:
int numUniqueEmails(vector<string> &emails) {
unordered_set<string> set;
stringstream ss;
for (auto &s: emails) {
int i = 0;
bool hasPlus = false;
for (; i < s.size(); ++i) {
if (s[i] == '+') hasPlus = true;
else if (s[i] == '@') break;
else if (s[i] != '.' && !hasPlus) ss << s[i];
}
ss << s.substr(i, s.size() - i);
set.insert(ss.str());
ss.str("");
// 清空状态,当ss.eof()
ss.clear();
}
return set.size();
}
};
#endif //CPP_0929__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_014/cpp_014-1/Solution1.h | <reponame>ooooo-youwillsee/leetcode<filename>lcof_014/cpp_014-1/Solution1.h
//
// Created by ooooo on 2020/3/11.
//
#ifndef CPP_014_1__SOLUTION1_H_
#define CPP_014_1__SOLUTION1_H_
#include <iostream>
#include <unordered_map>
using namespace std;
/**
* dfs + memo
*/
class Solution {
public:
/**
* @param flag false 表示不能切
* @return
*/
int dfs(int l, int r, bool flag) {
if (memo.count(r - l)) return memo[r - l];
if (l + 1 == r) return 1;
int ans = !flag ? 1 : r - l;
for (int i = l + 1; i < r; ++i) {
ans = max(ans, dfs(l, i, true) * dfs(i, r, true));
}
return memo[r - l] = ans;
}
unordered_map<int, int> memo;
int cuttingRope(int n) {
return dfs(0, n, false);
}
};
#endif //CPP_014_1__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0028-Implement-strStr/cpp_0028/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/7.
//
#ifndef CPP_0028_SOLUTION1_H
#define CPP_0028_SOLUTION1_H
#include <iostream>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle == "") return 0;
if (haystack == "" || haystack.size() < needle.size()) return -1;
int k = 0, count = 0;
for (; k <= haystack.size() - needle.size(); ++k) {
count = 0;
for (int i = 0; i < needle.size(); ++i) {
if (haystack[k + i] != needle[i]) {
break;
}
count++;
}
if (count == needle.size()) return k;
}
return -1;
}
};
#endif //CPP_0028_SOLUTION1_H
|
ooooo-youwillsee/leetcode | lcof_056-1/cpp_056-1/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/14.
//
#ifndef CPP_056_1__SOLUTION1_H_
#define CPP_056_1__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int findFirstBit(int num) {
int ans = 1;
while ((ans & num) == 0) {
ans <<= 1;
}
return ans;
}
vector<int> singleNumbers(vector<int> &nums) {
int num = 0;
for (auto &n : nums) num ^= n;
num = findFirstBit(num);
int a = 0, b = 0;
for (auto &n :nums) {
if ((num & n) == 0) a ^= n;
else b^= n;
}
return {a, b};
}
};
#endif //CPP_056_1__SOLUTION1_H_
|
Subsets and Splits