repo_name
stringlengths 5
122
| path
stringlengths 3
232
| text
stringlengths 6
1.05M
|
---|---|---|
ooooo-youwillsee/leetcode | lcof_067/cpp_067/Solution1.h | //
// Created by ooooo on 2020/4/27.
//
#ifndef CPP_067__SOLUTION1_H_
#define CPP_067__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
int strToInt(string str) {
int i = 0;
for (; i < str.size() && str[i] == ' '; i++) {}
bool flag = true;
if (str[i] == '-' || str[i] == '+') flag = str[i++] == '+';
if (!isdigit(str[i])) return 0;
long ans = 0;
while (i < str.size()) {
if (isdigit(str[i])) {
ans = ans * 10 + str[i] - '0';
if (flag && ans > INT_MAX) return INT_MAX;
if (!flag && -ans < INT_MIN) return INT_MIN;
} else {
break;
}
i++;
}
return flag ? ans : -ans;
}
};
#endif //CPP_067__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_051/cpp_051/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/8.
//
#ifndef CPP_051__SOLUTION1_H_
#define CPP_051__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int __merge(int l, int mid, int r, vector<int> &tmp) {
int i = mid, j = r, count = 0, k = r;
for (int p = l; p <= r; ++p) {
tmp[p] = nums[p];
}
while (i >= l && j >= mid + 1) {
if (tmp[i] > tmp[j]) {
nums[k--] = tmp[i--];
count += j - mid;
} else {
nums[k--] = tmp[j--];
}
}
for (; i >= l; i--) {
nums[k--] = tmp[i];
}
for (; j >= mid + 1; j--) {
nums[k--] = tmp[j];
}
return count;
}
int merge(int l, int r, vector<int> &tmp) {
if (l >= r) {
return 0;
}
int mid = l + (r - l) / 2;
int left = merge(l, mid, tmp);
int right = merge(mid + 1, r, tmp);
return left + right + __merge(l, mid, r, tmp);
}
vector<int> nums;
int reversePairs(vector<int> &nums) {
this->nums = nums;
vector<int> tmp(nums.size(), 0);
return merge(0, nums.size() - 1, tmp);
}
};
#endif //CPP_051__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0374-Guess-Number-Higher-or-Lower/cpp_0374/Solution1.h | //
// Created by ooooo on 2020/1/18.
//
#ifndef CPP_0374__SOLUTION1_H_
#define CPP_0374__SOLUTION1_H_
#include <iostream>
using namespace std;
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num) {
return 6 - num;
}
class Solution {
public:
int guessNumber(int n) {
int left = 1, right = n;
while (left <= right) {
int mid = left + (right - left) / 2;
int res = guess(mid);
if (res == 0) {
return mid;
} else if (res > 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
};
#endif //CPP_0374__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0015-3Sum/cpp_0015/Solution1.h | //
// Created by ooooo on 2019/10/31.
//
#ifndef CPP_0015_SOLUTION1_H
#define CPP_0015_SOLUTION1_H
#include <vector>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
vector<vector<int>> result;
set<vector<int>> set;
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
map<int, bool> map;
for (int j = i + 1; j < nums.size(); ++j) {
if (map.find(-nums[i] - nums[j]) != map.end()) {
vector<int> vec = {nums[i], nums[j], -nums[i] - nums[j]};
set.insert(vec);
}
map[nums[j]] = true;
}
}
for (auto item : set) {
result.push_back(item);
}
return result;
}
};
#endif //CPP_0015_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0048-Rotate-Image/cpp_0048/Solution2.h | //
// Created by ooooo on 2020/2/11.
//
#ifndef CPP_0048__SOLUTION2_H_
#define CPP_0048__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int>> &matrix) {
int n = matrix.size();
for (int i = 0; i <= n / 2; i++)
for (int j = i; j < n - 1 - i; j++) {
int t = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = t;
}
}
};
#endif //CPP_0048__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0448-Find-All-Numbers-Disappeared-in-an-Array/cpp_0448/Solution3.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/2/6.
//
#ifndef CPP_0448__SOLUTION3_H_
#define CPP_0448__SOLUTION3_H_
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/**
* 从第一个数字开始,逐一置为-1
*/
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int> &nums) {
for (int i = 0; i < nums.size(); ++i) {
int num = nums[i];
if (num == -1) continue;
while (true) {
int j = nums[num - 1];
if (j != -1) {
nums[num - 1] = -1;
num = j;
} else {
break;
}
}
}
vector<int> ans;
for (int i = 1; i <= nums.size(); ++i) {
if (nums[i - 1] > 0) {
ans.push_back(i);
}
}
return ans;
}
};
#endif //CPP_0448__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0002-Add-Two-Numbers/cpp_0002/Solution1.h | /**
* @author ooooo
* @date 2020/10/4 12:43
*/
#ifndef CPP_0002__SOLUTION1_H_
#define CPP_0002__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
ListNode *addTwoNumbers2(ListNode *l1, ListNode *l2) {
ListNode *dummyHead = new ListNode(-1), *cur = dummyHead;
int carry = 0;
while (l1 || l2) {
int v1 = l1 ? l1->val : 0;
int v2 = l2 ? l2->val : 0;
cur->next = new ListNode((v1 + v2 + carry) % 10);
carry = (v1 + v2 + carry) / 10;
l1 = l1 ? l1->next : nullptr;
l2 = l2 ? l2->next : nullptr;
cur = cur->next;
}
if (carry) {
cur->next = new ListNode(carry);
}
return dummyHead->next;
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *dummyHead = new ListNode(-1), *cur = dummyHead;
int carry = 0;
while (l1 || l2) {
int v1 = l1 ? l1->val : 0;
int v2 = l2 ? l2->val : 0;
ListNode *node = l1 ? l1 : l2;
node->val = (v1 + v2 + carry) % 10;
cur->next = node;
carry = (v1 + v2 + carry) / 10;
l1 = l1 ? l1->next : nullptr;
l2 = l2 ? l2->next : nullptr;
cur = cur->next;
}
if (carry) {
cur->next = new ListNode(carry);
}
return dummyHead->next;
}
};
#endif //CPP_0002__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0977-Squares-of-a-Sorted-Arrays/cpp_0977/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/7.
//
#ifndef CPP_0977_SOLUTION2_H
#define CPP_0977_SOLUTION2_H
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/**
* 双指针
*/
class Solution {
public:
vector<int> sortedSquares(vector<int> &A) {
if (A.empty()) return A;
int i = 0; // 第一个正数index
while (i <= A.size() - 1 && A[i] < 0) i++;
int j = i - 1; // 第一个负数index
vector<int> ans;
while (i < A.size() || j >= 0) {
if (i == A.size()) {
ans.push_back(A[j] * A[j]);
j--;
continue;
}
if (j == -1) {
ans.push_back(A[i] * A[i]);
i++;
continue;
}
int a = A[i] * A[i];
int b = A[j] * A[j];
if (a > b) {
ans.push_back(b);
j--;
} else {
ans.push_back(a);
i++;
}
}
return ans;
}
};
#endif //CPP_0977_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0965-Univalued-Binary-Tree/cpp_0965/Solution2.h | //
// Created by ooooo on 2019/12/31.
//
#ifndef CPP_0965_SOLUTION2_H
#define CPP_0965_SOLUTION2_H
#include "TreeNode.h"
#include <queue>
class Solution {
public:
bool isUnivalTree(TreeNode *root) {
if (!root) return true;
int x = root->val;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
while (size--) {
TreeNode *node = q.front();
q.pop();
if (node->val != x) {
return false;
}
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
}
return true;
}
};
#endif //CPP_0965_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1074-Number of Submatrices That Sum to Target/cpp_1074/Solution2.h | <filename>1074-Number of Submatrices That Sum to Target/cpp_1074/Solution2.h
//
// Created by ooooo on 5/31/2021.
//
#ifndef CPP_1074_SOLUTION2_H
#define CPP_1074_SOLUTION2_H
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int numSubmatrixSumTarget(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[0].size();
int ans = 0;
for (int p = 0; p < m; p++) {
int cols[n];
memset(cols, 0, sizeof(cols));
for (int i = p; i < m; i++) {
vector<int> preSum(n+1);
unordered_map<int,int> m;
m[0] = 1;
for (int j = 0; j < n; j++) {
cols[j] += matrix[i][j];
preSum[j+1] = preSum[j] + cols[j];
if (m.find(preSum[j+1] - target) != m.end()) {
ans += m[preSum[j+1] - target];
}
m[preSum[j+1]]++;
}
}
}
return ans;
}
};
#endif //CPP_1074_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1449-Form Largest Integer With Digits That Add up to Target/cpp_1449/Solution1.h | //
// Created by ooooo on 6/15/2021.
//
#ifndef CPP_1449_SOLUTION1_H
#define CPP_1449_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
string largestNumber(vector<int> &cost, int target) {
int n = cost.size();
// 恰好为target,所以初始化为 INT_MIN
vector<int> dp(target + 1, INT_MIN);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = cost[i]; j <= target; j++) {
// 不能用二维数组,如果用的话,需要遍历i之前所有的状态
dp[j] = max(dp[j], dp[j - cost[i]] + 1);
}
}
if (dp[target] < 0) {
return "0";
}
string ans = "";
for (int i = n - 1; i >= 0; i--) {
while (target >= cost[i] && dp[target - cost[i]] == dp[target] - 1) {
ans += '0' + i + 1;
target -= cost[i];
}
}
return ans;
}
};
#endif //CPP_1449_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1438-Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/cpp_1438/Solution2.h | /**
* @author ooooo
* @date 2021/2/21 09:14
*/
#ifndef CPP_1438__SOLUTION2_H_
#define CPP_1438__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <set>
#include <queue>
using namespace std;
// 单调队列维护区间的最值
class Solution {
public:
int longestSubarray(vector<int> &nums, int limit) {
int l = 0, r = 0;
int ans = 0;
deque<int> maxQ, minQ;
while (r < nums.size()) {
// 维护最小值下标
while (!minQ.empty() && nums[minQ.back()] >= nums[r]) {
minQ.pop_back();
}
minQ.push_back(r);
// 维护最大值下标
while (!maxQ.empty() && nums[maxQ.back()] <= nums[r]) {
maxQ.pop_back();
}
maxQ.push_back(r);
while (nums[maxQ.front()] - nums[minQ.front()] > limit) {
if (maxQ.front() == l) {
maxQ.pop_front();
}
if (minQ.front() == l) {
minQ.pop_front();
}
l++;
}
r++;
ans = max(ans, r - l);
}
return ans;
}
};
#endif //CPP_1438__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0204-Count-Primes/cpp_0204/Solution1.h | //
// Created by ooooo on 2020/1/8.
//
#ifndef CPP_0204_SOLUTION1_H
#define CPP_0204_SOLUTION1_H
#include <iostream>
#include <cmath>
using namespace std;
class Solution {
public:
bool isPrime(int num) {
if (num == 2) return true;
if ((num & 1) == 0 || num == 1) {
return false;
}
int max = int(sqrt(num) + 1);
for (int i = 3; i <= max; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
int countPrimes(int n) {
int ans = 0;
for (int i = n - 1; i >= 2; i--) {
if (isPrime(i)) {
ans++;
}
}
return ans;
}
};
#endif //CPP_0204_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1790-Check if One String Swap Can Make Strings Equal/cpp_1790/Solution1.h | <filename>1790-Check if One String Swap Can Make Strings Equal/cpp_1790/Solution1.h<gh_stars>10-100
/**
* @author ooooo
* @date 2021/3/28 12:10
*/
#ifndef CPP_1790__SOLUTION1_H_
#define CPP_1790__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:
bool areAlmostEqual(string s1, string s2) {
int n = s1.size();
int swap1 = -1;
for (int i = 0; i < n; ++i) {
if (s1[i] != s2[i]) {
swap1 = i;
break;
}
}
int swap2 = -1;
for (int i = n - 1; i >= 0; --i) {
if (s1[i] != s2[i]) {
swap2 = i;
break;
}
}
if (swap1 != -1) {
swap(s1[swap1], s1[swap2]);
return s1 == s2;
}
return true;
}
};
#endif //CPP_1790__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0079-Word-Search/cpp_0079/Solution1.h | <reponame>ooooo-youwillsee/leetcode<filename>0079-Word-Search/cpp_0079/Solution1.h
//
// Created by ooooo on 2019/12/4.
//
#ifndef CPP_0079_SOLUTION1_H
#define CPP_0079_SOLUTION1_H
#include <vector>
#include <iostream>
using namespace std;
class Solution {
private:
vector<vector<bool>> marked;
int m; // 行
int n; // 列
string word;
vector<int> arri = {0, 0, 1, -1};
vector<int> arrj = {1, -1, 0, 0};
bool dfs(vector<vector<char>> &board, int i, int j, int start) {
if (start == word.size() - 1) {
return word[start] == board[i][j];
}
if (word[start] == board[i][j]) {
marked[i][j] = true;
for (int k = 0; k < 4; k++) {
i = i + arri[k];
j = j + arrj[k];
if (inArea(i, j) && !marked[i][j] && dfs(board, i, j, start + 1)) {
return true;
}
i = i - arri[k];
j = j - arrj[k];
}
marked[i][j] = false;
}
return false;
}
bool inArea(int i, int j) {
return i >= 0 && i < m && j >= 0 && j < n;
}
public:
bool exist(vector<vector<char>> &board, string word) {
if (board.empty()) return false;
m = board.size();
n = board[0].size();
this->word = word;
marked = vector<vector<bool>>(m, vector<bool>(n, false));
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
if (dfs(board, i, j, 0)) {
return true;
}
}
}
return false;
}
};
#endif //CPP_0079_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0859-Buddy-Strings/cpp_0859/Solution2.h | //
// Created by ooooo on 2020/1/31.
//
#ifndef CPP_0859__SOLUTION2_H_
#define CPP_0859__SOLUTION2_H_
#include <iostream>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.size() != B.size() || A.empty() || B.empty()) return false;
if (A == B) {
unordered_set<char> set(26);
for (auto &c: A) {
set.insert(c);
}
// 有重复字符
return set.size() != A.size();
} else {
int swap_one = -1, swap_second = -1;
for (int i = 0; i < A.size(); ++i) {
if (A[i] != B[i]) {
if (swap_one == -1) swap_one = i;
else if (swap_second == -1) swap_second = i;
else return false;
}
}
return swap_second != -1 && A[swap_one] == B[swap_second] && A[swap_second] == B[swap_one];
}
}
};
#endif //CPP_0859__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_035/cpp_035/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/3/22.
//
#ifndef CPP_035__SOLUTION1_H_
#define CPP_035__SOLUTION1_H_
#include "Node.h"
#include <unordered_map>
class Solution {
public:
Node *copyRandomList(Node *head) {
if (!head) return nullptr;
unordered_map<Node *, Node *> node_map;
Node *dummyHead = new Node(0), *cur = dummyHead, *srcHead = head;
while (head) {
cur->next = new Node(head->val);
node_map[head] = cur->next;
cur = cur->next;
head = head->next;
}
cur = dummyHead->next;
while (srcHead) {
if (srcHead->random) {
cur->random = node_map[srcHead->random];
}
cur = cur->next;
srcHead = srcHead->next;
}
return dummyHead->next;
}
};
#endif //CPP_035__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0973-K Closest Points to Origin/cpp_0973/Solution2.h | /**
* @author ooooo
* @date 2020/11/9 09:12
*/
#ifndef CPP_0973__SOLUTION2_H_
#define CPP_0973__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/*
* sort
*/
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>> &points, int K) {
sort(points.begin(), points.end(), [](auto x, auto y) {
return x[0] * x[0] + x[1] * x[1] < y[0] * y[0] + y[1] * y[1];
});
return {points.begin(), points.begin() + K};
}
};
#endif //CPP_0973__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0055-Jump-Game/cpp_0055/Solution3.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/12.
//
#ifndef CPP_0055__SOLUTION3_H_
#define CPP_0055__SOLUTION3_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 贪心
*/
class Solution {
public:
bool canJump(vector<int> &nums) {
// k 表示能跳的最远距离
int k = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i > k) return false;
k = max(k, i + nums[i]);
}
return true;
}
};
#endif //CPP_0055__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0013-Roman-to-Integer/cpp_0013/Solution1.h | //
// Created by ooooo on 2020/1/24.
//
#ifndef CPP_0013__SOLUTION1_H_
#define CPP_0013__SOLUTION1_H_
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
unordered_map<char, int> m = {
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}
};
unordered_map<string, int> m2 = {
{"IV", 4}, {"IX", 9}, {"XL", 40}, {"XC", 90},
{"CD", 400}, {"CM", 900}
};
int romanToInt(string s) {
int ans = 0;
for (int i = 0; i < s.length();) {
// 索引i不是最后一个字符,取两个字符判断
if (i != s.length() - 1 && m2.count(s.substr(i, 2))) {
ans += m2[s.substr(i, 2)];
i += 2;
continue;
}
ans += m[s[i]];
i += 1;
}
return ans;
}
};
#endif //CPP_0013__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_060/cpp_060/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/20.
//
#ifndef CPP_060__SOLUTION1_H_
#define CPP_060__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Solution {
public:
vector<double> twoSum(int n) {
int max_count = 6; // 最大的点数
vector<vector<int>> dp(n + 1, vector<int>(n * max_count + 1, 0));
for (int j = 1; j <= max_count; ++j) dp[1][j] = 1;
// i 表示几个骰子
for (int i = 2; i <= n; ++i) {
// j 表示可能的点数
for (int j = i; j <= i * max_count; ++j) {
for (int k = 1; k < j && k <= max_count; ++k) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
vector<double> ans;
double total = pow(6, n);
for (int i = n; i <= max_count * n; ++i) ans.push_back(dp[n][i] / total);
return ans;
}
};
#endif //CPP_060__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0350-Intersection-of-Two-Arrays-II/cpp_0350/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2019/12/29.
//
#ifndef CPP_0350_SOLUTION1_H
#define CPP_0350_SOLUTION1_H
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> intersect(vector<int> &nums1, vector<int> &nums2) {
unordered_map<int, int> map;
for (const auto &num : nums1) {
if (map.find(num) == map.end()) {
map[num] = 1;
} else {
map[num] = map[num] + 1;
}
}
vector<int> res;
for (const auto &num : nums2) {
if (map.find(num) != map.end()) {
res.push_back(num);
if (map[num] == 1) {
map.erase(num);
} else {
map[num] -= 1;
}
}
}
return res;
}
};
#endif //CPP_0350_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0164-Maximum Gap/cpp_0164/Solution1.h | /**
* @author ooooo
* @date 2020/11/26 16:28
*/
#ifndef CPP_0164__SOLUTION1_H_
#define CPP_0164__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maximumGap(vector<int> &nums) {
if (nums.size() < 2) return 0;
sort(nums.begin(), nums.end());
int diff = 0;
for (int i = 0; i < nums.size() - 1; ++i) {
diff = max(diff, nums[i + 1] - nums[i]);
}
return diff;
}
};
#endif //CPP_0164__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0441-Arranging-Coins/cpp_0441/Solution2.h | //
// Created by ooooo on 2020/1/19.
//
#ifndef CPP_0441__SOLUTION2_H_
#define CPP_0441__SOLUTION2_H_
#include <iostream>
using namespace std;
/**
* binary search
*/
class Solution {
public:
int arrangeCoins(int n) {
if (n <= 1) return n;
long left = 1, right = n, mid = 0, sum = 0;
while (left <= right) {
mid = left + (right - left) / 2;
sum = (1 + mid) * mid / 2;
// sum == n 或者 当前的sum小于n,但累计下一个值大于n
if (sum == n || (sum < n && sum + mid + 1 > n)) {
return mid;
} else if (sum > n) {
right = mid;
} else {
left = mid + 1;
}
}
return -1;
}
};
#endif //CPP_0441__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0330-Patching Array/cpp_0330/Solution1.h | /**
* @author ooooo
* @date 2020/12/29 17:36
*/
#ifndef CPP_0330__SOLUTION1_H_
#define CPP_0330__SOLUTION1_H_
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int minPatches(vector<int> &nums, int n) {
int patches = 0;
long long x = 1;
int length = nums.size(), index = 0;
while (x <= n) {
if (index < length && nums[index] <= x) {
x += nums[index];
index++;
} else {
x <<= 1;
patches++;
}
}
return patches;
}
};
#endif //CPP_0330__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0938-Range-Sum-of-BST/cpp_0938/TreeNode.h | //
// Created by ooooo on 2019/12/31.
//
#ifndef CPP_0938_TREENODE_H
#define CPP_0938_TREENODE_H
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
* @return left <= x <= right
*/
bool match(int left, int right, int x){
return left <= x && x <= right;
}
#endif //CPP_0938_TREENODE_H
|
ooooo-youwillsee/leetcode | lcof_018/cpp_018/Solution1.h | //
// Created by ooooo on 2020/3/13.
//
#ifndef CPP_018__SOLUTION1_H_
#define CPP_018__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
ListNode *deleteNode(ListNode *head, int val) {
ListNode *dummyHead = new ListNode(0), *prev = dummyHead;
dummyHead->next = head;
while (prev->next) {
if (prev->next->val == val) break;
prev = prev->next;
}
if (prev) {
ListNode *delNode = prev->next;
prev->next = delNode->next;
}
return dummyHead->next;
}
};
#endif //CPP_018__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0164-Maximum Gap/cpp_0164/Solution2.h | <reponame>ooooo-youwillsee/leetcode<filename>0164-Maximum Gap/cpp_0164/Solution2.h
/**
* @author ooooo
* @date 2020/11/26 19:19
*/
#ifndef CPP_0164__SOLUTION2_H_
#define CPP_0164__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maximumGap(vector<int> &nums) {
if (nums.size() < 2) return 0;
int n = nums.size();
vector<int> buf(n);
int a = 1, max_v = *max_element(nums.begin(), nums.end());
while (max_v >= a) {
vector<int> cnt(10), t(10);
for (int i = 0; i < n; ++i) {
int d = (nums[i] / a) % 10;
cnt[d]++;
}
for (int i = 1; i <= 9; ++i) {
t[i] = t[i - 1] + cnt[i - 1];
}
for (int i = 0; i < n; ++i) {
int d = (nums[i] / a) % 10;
buf[t[d]] = nums[i];
t[d]++;
}
copy(buf.begin(), buf.end(), nums.begin());
a *= 10;
}
int diff = 0;
for (int i = 1; i < n; ++i) {
diff = max(diff, nums[i] - nums[i - 1]);
}
return diff;
}
};
#endif //CPP_0164__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0494-Target-Sum/cpp_0494/Solution2.h | //
// Created by ooooo on 2020/3/3.
//
#ifndef CPP_0494__SOLUTION2_H_
#define CPP_0494__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
/**
* dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j+nums[i]]
*
* 背包问题
*
*/
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int target) {
int n = nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0);
if (target > sum) return 0;
int dp[n+1][2*sum+1];
memset(dp, 0, sizeof(dp));
dp[0][sum] = 1; // 组成0的有一种方式
// 0 -> sum-1 负数
// sum+1 -> 2*sum +1 正数
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2*sum+1; j++) {
if (j - nums[i] >= 0) {
dp[i+1][j]+= dp[i][j - nums[i]];
}
if (j + nums[i] < 2*sum +1) {
dp[i+1][j]+= dp[i][j + nums[i]];
}
}
}
return dp[n][sum + target];
}
};
#endif //CPP_0494__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0237-Delete-Node-in-a-Linked-List/cpp_0237/Solution1.h | //
// Created by ooooo on 2020/1/23.
//
#ifndef CPP_0237__SOLUTION1_H_
#define CPP_0237__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
void deleteNode(ListNode *node) {
ListNode *cur = node;
while (cur && cur->next) {
cur->val = cur->next->val;
if (!cur->next->next) {
cur->next = nullptr;
break;
}
cur = cur->next;
}
}
};
#endif //CPP_0237__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0021-Merge-Two-Sorted-Lists/cpp_0021/Solution3.h | //
// Created by ooooo on 2020/1/23.
//
#ifndef CPP_0021__SOLUTION3_H_
#define CPP_0021__SOLUTION3_H_
#include "ListNode.h"
/**
* recursion
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
#endif //CPP_0021__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0819-Most-Common-Word/cpp_0819/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/30.
//
#ifndef CPP_0819__SOLUTION1_H_
#define CPP_0819__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <regex>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/**
* timeout ???
*/
class Solution {
public:
string mostCommonWord(string paragraph, vector<string> &banned) {
unordered_map<string, int> m;
unordered_set<string> bannedSet(banned.begin(), banned.end());
regex reg("[A-Za-z]+");
for (int i = 0; i < paragraph.size(); ++i) {
paragraph[i] = tolower(paragraph[i]);
}
for (regex_iterator it(paragraph.begin(), paragraph.end(), reg); it->begin() < it->end(); ++it) {
if (bannedSet.count(it->str())) continue;
++m[it->str()];
}
int max = 0;
string ans;
for (auto entry: m) {
if (entry.second > max) {
ans = entry.first;
max = entry.second;
}
}
return ans;
}
};
#endif //CPP_0819__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0304-Range Sum Query 2D - Immutable/cpp_0304/Solution2.h | <filename>0304-Range Sum Query 2D - Immutable/cpp_0304/Solution2.h
/**
* @author ooooo
* @date 2021/3/2 16:41
*/
#ifndef CPP_0304__SOLUTION2_H_
#define CPP_0304__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class NumMatrix {
public:
vector<vector<int>> preSum;
NumMatrix(vector<vector<int>> &matrix) {
if (matrix.empty()) {
return;
}
int m = matrix.size(), n = matrix[0].size();
preSum.assign(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
preSum[i + 1][j + 1] = preSum[i][j + 1] + preSum[i + 1][j] - preSum[i][j] + matrix[i][j];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
if (preSum.empty()) {
return 0;
}
return preSum[row2 + 1][col2 + 1] - preSum[row1][col2 + 1] - preSum[row2 + 1][col1] + preSum[row1][col1];
}
};
#endif //CPP_0304__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1438-Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/cpp_1438/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/21 08:46
*/
#ifndef CPP_1438__SOLUTION1_H_
#define CPP_1438__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
int longestSubarray(vector<int> &nums, int limit) {
multiset<int> set;
int l = 0, r = 0;
int ans = 0;
while (r < nums.size()) {
set.insert(nums[r]);
while (*set.rbegin() - *set.begin() > limit) {
set.erase(set.find(nums[l]));
l++;
}
r++;
ans = max(ans, r - l);
}
return ans;
}
};
#endif //CPP_1438__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0198-House-Robber/cpp_0198/Solution1.h | //
// Created by ooooo on 2020/2/5.
//
#ifndef CPP_0198__SOLUTION1_H_
#define CPP_0198__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dp[i][1] = dp[i-1][0] + p
* dp[i][0] = max(dp[i-1][0], dp[i-1][1] )
*/
class Solution {
public:
int rob(vector<int> &nums) {
int ans = 0, n = nums.size();
vector<vector<int>> dp(n + 1, vector<int>(2));
for (int j = 0; j < n; ++j) {
int i = j + 1, p = nums[j];
dp[i][1] = dp[i - 1][0] + p;
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
}
return max(dp[n][1], dp[n][0]);
}
};
#endif //CPP_0198__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1640-Check Array Formation Through Concatenation/cpp_1640/Solution1.h | /**
* @author ooooo
* @date 2020/11/15 09:33
*/
#ifndef CPP_1640__SOLUTION1_H_
#define CPP_1640__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <numeric>
#include <stack>
#include <queue>
using namespace std;
class Solution {
public:
bool canFormArray(vector<int> &arr, vector<vector<int>> &pieces) {
unordered_map<int, vector<int>> map;
for (auto &piece: pieces) {
map[piece[0]] = piece;
}
for (int i = 0; i < arr.size();) {
if (map.find(arr[i]) == map.end()) return false;
for (auto &num :map[arr[i]]) {
if (arr[i] != num) return false;
i++;
}
}
return true;
}
}
#endif //CPP_1640__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0321-Create Maximum Number/cpp_0321/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/12/3 12:27
*/
#ifndef CPP_0321__SOLUTION1_H_
#define CPP_0321__SOLUTION1_H_
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
// 自定义比较函数, 处理情况 num1:[6,7] num2:[6,0,4] k=5
bool compare(vector<int> &nums1, int i, vector<int> &nums2, int j) {
int m = nums1.size(), n = nums2.size();
while (i < m && j < n) {
if (nums1[i] != nums2[j]) {
return nums1[i] > nums2[j];
}
i++;
j++;
}
return i != m;
}
vector<int> maxSubArray(vector<int> &nums, int k) {
vector<int> s;
for (int i = 0, n = nums.size(); i < n; ++i) {
int num = nums[i];
while (!s.empty() && num > s.back() && s.size() + n - 1 - i >= k) {
s.pop_back();
}
s.push_back(num);
}
return {s.begin(), s.begin() + k};
}
vector<int> merge(vector<int> &nums1, vector<int> &nums2) {
int i = 0, m = nums1.size(), j = 0, n = nums2.size();
vector<int> ans(m + n, 0);
int k = 0;
while (i < m && j < n) {
if (compare(nums1, i, nums2, j) > 0) {
ans[k++] = nums1[i++];
} else {
ans[k++] = nums2[j++];
}
}
while (i < m) {
ans[k++] = nums1[i++];
}
while (j < n) {
ans[k++] = nums2[j++];
}
return ans;
}
vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k) {
int m = nums1.size(), n = nums2.size();
vector<int> ans(k, 0);
for (int i = 0, j = min(k, m); i <= j; ++i) {
if (i > m || k - i > n) continue;
auto vec1 = maxSubArray(nums1, i);
auto vec2 = maxSubArray(nums2, k - i);
auto vec = merge(vec1, vec2);
ans = max(ans, vec);
}
return ans;
}
};
#endif //CPP_0321__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0047-Permutations-II/cpp_0047/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/9/18 09:18
*/
#ifndef CPP_0047__SOLUTION1_H_
#define CPP_0047__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void permuteUnique(vector<int> nums, int i) {
if (i == nums.size() - 1) {
ans.push_back(nums);
return;
}
for (int j = i; j < nums.size(); ++j) {
if (j != i && nums[j] == nums[i]) continue;
swap(nums[i], nums[j]);
permuteUnique(nums, i + 1);
}
}
vector<vector<int>> ans;
vector<vector<int>> permuteUnique(vector<int> &nums) {
sort(nums.begin(), nums.end());
permuteUnique(nums, 0);
return ans;
}
};
#endif //CPP_0047__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0338-Counting Bits/cpp_0338/Solution1.h | //
// Created by ooooo on 2019/12/5.
//
#ifndef CPP_0338_SOLUTION1_H
#define CPP_0338_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> countBits(int num) {
vector<int> res = vector<int>(num + 1, 0);
for (int i = 1; i <= num; i++) {
int n = i;
int count = 0;
while (n) {
count++;
n &= n - 1;
}
res[i] = count;
}
return res;
}
};
#endif //CPP_0338_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1689-Partitioning Into Minimum Number Of Deci-Binary Numbers/cpp_1689/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
/**
* @author ooooo
* @date 2020/12/20 12:59
*/
#ifndef CPP_1689__SOLUTION1_H_
#define CPP_1689__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minPartitions(string n) {
int ans = 0;
for (int i = 0; i < n.size(); ++i) {
ans = max(ans, n[i] - '0');
}
return ans;
}
};
#endif //CPP_1689__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0067-Add-Binary/cpp_0067/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/25.
//
#ifndef CPP_0067__SOLUTION1_H_
#define CPP_0067__SOLUTION1_H_
#include <iostream>
using namespace std;
/**
* 从尾遍历到头
*/
class Solution {
public:
string addBinary(string a, string b) {
string ans = "";
int i = a.size() - 1, j = b.size() - 1;
// flag表示是否需要进一
bool flag = false;
string aStr = "", bStr = "";
while (i >= 0 || j >= 0) {
aStr = i < 0 ? "0" : a.substr(i--, 1);
bStr = j < 0 ? "0" : b.substr(j--, 1);
if (flag) {
if (aStr == "0" && bStr == "0") {
flag = false;
ans.insert(0, "1");
} else if (aStr == "1" && bStr == "1") {
ans.insert(0, "1");
} else {
ans.insert(0, "0");
}
} else {
if (aStr == "0" && bStr == "0") {
ans.insert(0, "0");
} else if (aStr == "1" && bStr == "1") {
flag = true;
ans.insert(0, "0");
} else {
ans.insert(0, "1");
}
}
}
if (flag) {
ans.insert(0, "1");
}
return ans;
}
};
#endif //CPP_0067__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0036-Valid-Sudoku/cpp_0036/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2019/11/21.
//
#ifndef CPP_0036_SOLUTION1_H
#define CPP_0036_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution1 {
private:
public:
bool isValidSudoku(vector<vector<char>> &board) {
int row[9][9] = {0}, col[9][9] = {0}, matrix[9][9] = {0};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') continue;
int num = board[i][j] - '0' - 1;
row[i][num]++;
col[j][num]++;
int bIndex = (i / 3) * 3 + j / 3;
matrix[bIndex][num]++;
if (row[i][num] == 2 || col[j][num] == 2 ||
matrix[bIndex][num] == 2) {
return false;
}
}
}
return true;
}
};
#endif //CPP_0036_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1609-Even Odd Tree/cpp_1609/TreeNode.h | /**
* @author ooooo
* @date 2020/10/11 13:42
*/
#ifndef CPP_1609__TREENODE_H_
#define CPP_1609__TREENODE_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
#endif //CPP_1609__TREENODE_H_
|
ooooo-youwillsee/leetcode | 0019-Remove-Nth-Node-From-End-of-List/cpp_0019/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/8.
//
#ifndef CPP_0019__SOLUTION1_H_
#define CPP_0019__SOLUTION1_H_
#include "ListNode.h"
#include <vector>
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
vector<ListNode *> nodes;
ListNode *dummyNode = new ListNode(0), *cur = dummyNode;
dummyNode->next = head;
while (cur) {
nodes.push_back(cur);
cur = cur->next;
}
// 删除最后一个,应该指向NULL
nodes.push_back(nullptr);
nodes[nodes.size() - n - 1]->next = nodes[nodes.size() - n + 1];
return nodes[0]->next;
}
};
#endif //CPP_0019__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0011-Container-With-Most-Water/cpp_0011/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/6.
//
#ifndef CPP_0011__SOLUTION2_H_
#define CPP_0011__SOLUTION2_H_
#include <iostream>
#include <vector>
/**
* 双指针
*/
using namespace std;
class Solution {
public:
int maxArea(vector<int> &height) {
int ans = 0, left = 0, right = height.size() - 1;
while (left < right) {
int h = 0, w = right - left;
if (height[left] <= height[right]) {
h = height[left];
left++;
} else {
h = height[right];
right--;
}
ans = max(ans, h * w);
}
return ans;
}
};
#endif //CPP_0011__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0328-Odd Even Linked List/cpp_0328/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/11/13 21:07
*/
#ifndef CPP_0328__SOLUTION1_H_
#define CPP_0328__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
ListNode *oddEvenList(ListNode *head) {
if (!head || !head->next) return head;
ListNode *odd = head, *even = head->next, *even_head = head->next;
ListNode *prev_odd = nullptr;
while (even) {
odd->next = even->next;
prev_odd = odd;
odd = odd->next;
if (odd) {
even->next = odd->next;
even = even->next;
} else {
break;
}
}
if (odd) {
odd->next = even_head;
} else {
prev_odd->next = even_head;
}
return head;
}
};
#endif //CPP_0328__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_053-1/cpp_053-1/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/9.
//
#ifndef CPP_053_1__SOLUTION1_H_
#define CPP_053_1__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int search(vector<int> &nums, int target) {
int count = 0;
for (auto &num: nums) {
if (num == target) count++;
else if (num > target) break;
}
return count;
}
};
#endif //CPP_053_1__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1864-Minimum Number of Swaps to Make the Binary String Alternating/cpp_1864/Solution1.h | <filename>1864-Minimum Number of Swaps to Make the Binary String Alternating/cpp_1864/Solution1.h
/**
* @author ooooo
* @date 2021/5/25 22:16
*/
#ifndef CPP_1864__SOLUTION1_H_
#define CPP_1864__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int cntSwap(string &s, char startC) {
int diffCnt = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] != startC) {
diffCnt++;
}
startC = '1' == startC ? '0' : '1';
}
if (diffCnt % 2 == 1) {
return INT_MAX;
}
return max(0, diffCnt / 2);
}
int minSwaps(string s) {
if (s.size() <= 1) return 0;
int cnt0 = 0, cnt1 = 0;
for (auto c: s) {
if (c == '1') {
cnt1++;
} else {
cnt0++;
}
}
if (abs(cnt1 - cnt0) > 1) return -1;
int ans = min(cntSwap(s, '1'), cntSwap(s, '0'));
return ans;
}
};
#endif //CPP_1864__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0690-Employee-Importance/cpp_0690/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2020/1/13.
//
#ifndef CPP_0690__SOLUTION1_H_
#define CPP_0690__SOLUTION1_H_
#include "Employee.h"
#include <unordered_map>
/**
* loop
*/
class Solution {
public:
int getImportance(vector<Employee *> employees, int id) {
unordered_map<int, int> importanceMap;
unordered_map<int, vector<int>> subordinateMap;
for (const auto &item: employees) {
importanceMap[item->id] = item->importance;
subordinateMap[item->id] = item->subordinates;
}
int ans = importanceMap[id];
vector<int> subIds = subordinateMap[id];
while (!subIds.empty()) {
// 用于添加每一个直属下级
vector<int> vec;
for (auto &subId: subIds) {
ans += importanceMap[subId];
vec.insert(vec.end(), subordinateMap[subId].begin(), subordinateMap[subId].end());
}
subIds = vec;
}
return ans;
}
};
#endif //CPP_0690__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0205-Isomorphic-Strings/cpp_0205/Solution2.h | //
// Created by ooooo on 2020/1/9.
//
#ifndef CPP_0205_SOLUTION2_H
#define CPP_0205_SOLUTION2_H
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool help(string s, string t) {
unordered_map<char, char> m;
for (int i = 0; i < s.size(); ++i) {
if (m.count(s[i])) {
if (m[s[i]] != t[i]) return false;
} else {
m[s[i]] = t[i];
}
}
return true;
}
bool isIsomorphic(string s, string t) {
return help(s, t) && help(t, s);
}
};
#endif //CPP_0205_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0154-Find Minimum in Rotated Sorted Array II/cpp_0154/Solution1.h | /**
* @author ooooo
* @date 2021/4/9 13:08
*/
#ifndef CPP_0154__SOLUTION1_H_
#define CPP_0154__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int findMin(vector<int> &nums) {
int n = nums.size();
int l = 0, r = n - 1;
if (nums[l] < nums[r]) return nums[l];
int ans = nums[l];
while (l <= r) {
int mid = l + (r - l) / 2;
if (nums[l] < nums[mid]) {
ans = min(ans, nums[l]);
l = mid + 1;
} else if (nums[mid] < nums[r]) {
ans = min(ans, nums[mid]);
r = mid - 1;
} else {
while (l <= r) {
ans = min(ans, nums[l++]);
}
}
}
return ans;
}
};
#endif //CPP_0154__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0279-Perfect-Squares/cpp_0279/Solution4.h | //
// Created by ooooo on 2020/2/23.
//
#ifndef CPP_0279__SOLUTION4_H_
#define CPP_0279__SOLUTION4_H_
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
using namespace std;
/**
* bfs 这一题相当于 查找最短路径(n -> 0)
*/
class Solution {
public:
int numSquares(int n) {
unordered_set<int> set;
for (int i = int(sqrt(n)); i >= 1; --i) {
set.insert(i * i);
}
int level = 0;
queue<int> q;
q.push(n);
while (!q.empty()) {
level++;
for (int i = 0, len = q.size(); i < len; ++i) {
n = q.front();
q.pop();
if (set.count(n)) return level;
for (auto &num: set) {
if (n >= num) {
q.push(n - num);
}
}
}
}
return level;
}
};
#endif //CPP_0279__SOLUTION4_H_
|
ooooo-youwillsee/leetcode | lcof_011/cpp_011/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2020/3/9.
//
#ifndef CPP_011__SOLUTION1_H_
#define CPP_011__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* O(n)
*/
class Solution {
public:
int minArray(vector<int> &numbers) {
for (int i = 0, len = numbers.size() - 1; i < len; ++i) {
if (numbers[i] > numbers[i + 1]) return numbers[i + 1];
}
return numbers[0];
}
};
#endif //CPP_011__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0331-Verify Preorder Serialization of a Binary Tree/cpp_0331/Solution1.h | /**
* @author ooooo
* @date 2021/3/12 10:17
*/
#ifndef CPP_0331__SOLUTION1_H_
#define CPP_0331__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
struct Node {
Node *left, *right;
int value;
Node(int value) : value(value) {
this->left = this->right = nullptr;
}
};
Node *dfs(string &preorder) {
if (i >= preorder.size() || !pass) return nullptr;
if (preorder[i] == '#') {
i += 2;
return new Node(-1);
}
int num = 0;
while (i < preorder.size() && preorder[i] >= '0' && preorder[i] <= '9') {
num = num * 10 + (preorder[i] - '0');
i++;
}
i++;
Node *root = new Node(num);
root->left = dfs(preorder);
root->right = dfs(preorder);
if (!root->left || !root->right) {
pass = false;
}
return root;
}
int i = 0;
bool pass = false;
bool isValidSerialization(string preorder) {
i = 0;
pass = true;
dfs(preorder);
return i == preorder.size() && pass;
}
};
#endif //CPP_0331__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0590-N-ary-Tree-Postorder-Transversal/cpp_0590/Solution2.h | //
// Created by ooooo on 2020/1/3.
//
#ifndef CPP_0590_SOLUTION2_H
#define CPP_0590_SOLUTION2_H
#include "Node.h"
#include <stack>
class Solution {
public:
vector<int> postorder(Node *root) {
if(!root) return {};
stack<Node *> s;
s.push(root);
vector<int> res;
while (!s.empty()) {
Node *node = s.top();
s.pop();
for (auto child: node->children) {
s.push(child);
}
res.push_back(node->val);
}
reverse(res.begin(), res.end());
return res;
}
};
#endif //CPP_0590_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1202-Smallest String With Swaps/cpp_1202/Solution1.h | /**
* @author ooooo
* @date 2021/1/11 09:14
*/
#ifndef CPP_1202__SOLUTION1_H_
#define CPP_1202__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
// sort
class Solution {
public:
string smallestStringWithSwaps(string s, vector<vector<int>> &pairs) {
int n = s.size();
vector<int> p(n);
for (int i = 0; i < n; ++i) {
p[i] = i;
}
unordered_map<int, priority_queue<char, vector<char>, greater<char>>> map;
for (int i = 0; i < pairs.size(); ++i) {
int x = pairs[i][0], y = pairs[i][1];
connect(p, x, y);
}
for (int i = 0; i < n; ++i) {
int pi = findP(p, i);
map[pi].push(s[i]);
}
string ans = "";
for (int i = 0; i < n; ++i) {
int pi = findP(p, i);
char c = map[pi].top();
map[pi].pop();
ans += c;
}
return ans;
}
int findP(vector<int> &p, int i) {
if (i == p[i]) return i;
auto pi = findP(p, p[i]);
p[i] = pi;
return pi;
}
int connect(vector<int> &p, int i, int j) {
int pi = findP(p, i), pj = findP(p, j);
if (pi == pj) return pi;
p[pi] = pj;
return pj;
}
};
#endif //CPP_1202__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_052/cpp_052/Solution1.h | //
// Created by ooooo on 2020/4/9.
//
#ifndef CPP_052__SOLUTION1_H_
#define CPP_052__SOLUTION1_H_
#include "ListNode.h"
#include <unordered_set>
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_set<ListNode *> nodes;
for (; headA; headA = headA->next) nodes.insert(headA);
for (; headB; headB = headB->next) {
if (nodes.count(headB)) return headB;
}
return nullptr;
}
};
#endif //CPP_052__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0111-Minimum-Depth-of-Binary-Tree/cpp_0111/Solution2.h | //
// Created by ooooo on 2019/11/4.
//
#ifndef CPP_0111_SOLUTION2_H
#define CPP_0111_SOLUTION2_H
#include "TreeNode.h"
class Solution {
public:
int minDepth(TreeNode *root) {
if (!root) return 0;
if (!root->left) return 1 + minDepth(root->right);
if (!root->right) return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};
#endif //CPP_0111_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1006-Clumsy Factorial/cpp_1006/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/4/1 18:03
*/
#ifndef CPP_1006__SOLUTION1_H_
#define CPP_1006__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
vector<string> ops = {"*", "/", "+", "-"};
void calc(stack<int> &s, string &op) {
int x = s.top();
s.pop();
int y = s.top();
s.pop();
if (op == "*") {
s.push(y * x);
} else if (op == "/") {
s.push(y / x);
} else if (op == "+") {
s.push(y + x);
} else {
s.push(y - x);
}
}
int clumsy(int N) {
int i = 0;
stack<int> s;
stack<string> op;
s.push(N);
while (N > 1) {
N--;
if (ops[i] == "*" || ops[i] == "/") {
s.push(N);
calc(s, ops[i]);
} else if (ops[i] == "-") {
op.push("+");
s.push(-1 * N);
} else {
op.push("+");
s.push(N);
}
i = (i + 1) % ops.size();
}
while (s.size() > 1) {
string p = op.top();
op.pop();
calc(s, p);
}
return s.top();
}
};
#endif //CPP_1006__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1657-Determine if Two Strings Are Close/cpp_1657/Solution1.h | /**
* @author ooooo
* @date 2021/1/24 18:00
*/
#ifndef CPP_1657__SOLUTION1_H_
#define CPP_1657__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
using namespace std;
class Solution {
public:
vector<int> calcCounts(string word) {
vector<int> counts(26, 0);
for (auto &c : word) {
counts[c - 'a']++;
}
return counts;
}
bool closeStrings(string word1, string word2) {
vector<int> count1 = calcCounts(word1);
vector<int> count2 = calcCounts(word2);
for (int i = 0; i < 26; ++i) {
if (count1[i] != count2[i]) {
if (count2[i] == 0 || count1[i] == 0) return false;
bool find = false;
for (int j = i + 1; j < 26; ++j) {
if (count2[j] == count1[i]) {
swap(count2[j], count2[i]);
find = true;
break;
}
}
if (!find) {
return false;
}
}
}
return true;
}
};
#endif //CPP_1657__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0810-Chalkboard XOR Game/cpp_0810/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/5/22 11:15
*/
#ifndef CPP_0810__SOLUTION1_H_
#define CPP_0810__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool xorGame(vector<int> &nums) {
if (nums.size() % 2 == 0) {
return true;
}
int ans = 0;
for (auto num: nums) {
ans ^= num;
}
return ans == 0; // Alice 第一手时,就能获胜
}
};
#endif //CPP_0810__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1170-Compare-Strings-by-Frequency-of-the-Smallest-Character/cpp_1170/Solution1.h | //
// Created by ooooo on 2020/2/2.
//
#ifndef CPP_1170__SOLUTION1_H_
#define CPP_1170__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
int countFreq(string &s) {
char c = s[0];
int count = 1;
for (int i = 1; i < s.size(); ++i) {
if (s[i] < c) {
count = 1;
c = s[i];
} else if (s[i] == c) count += 1;
}
return count;
}
vector<int> numSmallerByFrequency(vector<string> &queries, vector<string> &words) {
vector<int> ans, counts;
for (auto &word: words) {
counts.push_back(countFreq(word));
}
sort(counts.begin(), counts.end(), greater<int>());
for (int i = 0; i < queries.size(); ++i) {
int res = countFreq(queries[i]);
int j = 0; // 计数
for (auto count: counts) {
if (count > res) j += 1;
else break;
}
ans.push_back(j);
}
return ans;
}
};
#endif //CPP_1170__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_026/cpp_026/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2020/3/17.
//
#ifndef CPP_026__SOLUTION1_H_
#define CPP_026__SOLUTION1_H_
#include "TreeNode.h"
class Solution {
public:
bool isSameTree(TreeNode *node1, TreeNode *node2) {
if (!node2) return true;
if (!node1) return false;
return node1->val == node2->val && isSameTree(node1->left, node2->left) && isSameTree(node1->right, node2->right);
}
bool isSubStructure(TreeNode *A, TreeNode *B) {
if (!A || !B) return false;
if (A->val != B->val) {
return isSubStructure(A->left, B) || isSubStructure(A->right, B);
} else {
return isSameTree(A->left, B->left) && isSameTree(A->right, B->right);
}
}
};
#endif //CPP_026__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_006/cpp_006/Solution3.h | //
// Created by ooooo on 2020/3/7.
//
#ifndef CPP_006__SOLUTION3_H_
#define CPP_006__SOLUTION3_H_
#include "ListNode.h"
/**
* O(n)
*/
class Solution {
public:
void dfs(ListNode *node) {
if (!node) return;
dfs(node->next);
ans.emplace_back(node->val);
}
vector<int> ans;
vector<int> reversePrint(ListNode *head) {
dfs(head);
return ans;
}
};
#endif //CPP_006__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0543-Diameter-of-Binary-Tree/cpp_0543/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/3.
//
#ifndef INC_0543_DIAMETER_OF_BINARY_TREE_SOLUTION1_H
#define INC_0543_DIAMETER_OF_BINARY_TREE_SOLUTION1_H
#include "TreeNode.h"
/**
* 所谓的直径 就是 节点个数 - 1
*/
class Solution {
public:
int max = INT_MIN;
int maxDepth(TreeNode *node) {
if (!node) return 0;
int leftDepth = maxDepth(node->left);
int rightDepth = maxDepth(node->right);
if (leftDepth == 0 || rightDepth == 0) {
max = std::max(std::max(leftDepth, rightDepth), max);
} else {
max = std::max(max, leftDepth + rightDepth);
}
return std::max(leftDepth, rightDepth) + 1;
}
int diameterOfBinaryTree(TreeNode *root) {
maxDepth(root);
return max == INT_MIN ? 0 : max;
}
};
#endif //INC_0543_DIAMETER_OF_BINARY_TREE_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0493-Reverse Pairs/cpp_0493/Solution1.h | /**
* @author ooooo
* @date 2020/11/28 09:11
*/
#ifndef CPP_0493__SOLUTION1_H_
#define CPP_0493__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<long long> tmp;
int ans = 0;
void count(vector<int> &nums, int l, int mid, int r) {
int i = l, j = mid + 1;
while (i <= mid && j <= r) {
if ((long)nums[i] > (long)2 * nums[j]) {
j++;
ans += (mid - i + 1);
} else {
i++;
}
}
}
void merge(vector<int> &nums, int l, int mid, int r) {
count(nums, l, mid, r);
copy(nums.begin() + l, nums.begin() + r + 1, tmp.begin() + l);
int i = l, j = mid + 1;
int k = l;
while (i <= mid && j <= r) {
if (tmp[i] < tmp[j]) {
nums[k] = tmp[i];
i++;
} else {
nums[k] = tmp[j];
j++;
}
k++;
}
while (i <= mid) {
nums[k] = tmp[i];
i++;
k++;
}
while (j <= r) {
nums[k] = tmp[j];
j++;
k++;
}
}
void mergeSort(vector<int> &nums, int l, int r) {
if (l >= r) return;
int mid = l + (r - l) / 2;
mergeSort(nums, l, mid);
mergeSort(nums, mid + 1, r);
merge(nums, l, mid, r);
}
int reversePairs(vector<int> &nums) {
int n = nums.size();
tmp.assign(n, 0);
mergeSort(nums, 0, n - 1);
//for (int i = 0; i < n; ++i) {
// cout << nums[i] << " ";
//}
//cout << endl;
return ans;
}
};
#endif //CPP_0493__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0123-Best Time to Buy and Sell Stock III/cpp_0123/Solution1.h | /**
* @author ooooo
* @date 2021/1/9 12:23
*/
#ifndef CPP_0123__SOLUTION1_H_
#define CPP_0123__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dp[i][j][k]
*/
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
int k = 2;
int v = -100001;
vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(k + 1, vector<int>(2)));
for (int i = 1; i <= k; ++i) {
dp[0][i][0] = dp[0][i][1] = v;
}
dp[0][0][1] = v;
int ans = 0;
for (int i = 0; i < n; ++i) {
int p = prices[i];
for (int j = 1; j <= k; ++j) {
dp[i + 1][j][1] = max(dp[i][j - 1][0] - p, dp[i][j][1]);
dp[i + 1][j][0] = max(dp[i][j][1] + p, dp[i][j][0]);
ans = max(ans, dp[i + 1][j][0]);
}
}
return ans;
}
};
#endif //CPP_0123__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0137-Single Number II/cpp_0137/Solution1.h | /**
* @author ooooo
* @date 2021/4/30 20:02
*/
#ifndef CPP_0137__SOLUTION1_H_
#define CPP_0137__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int singleNumber(vector<int> &nums) {
unordered_map<int, int> m;
for (auto num: nums) {
m[num]++;
}
for (auto &e: m) {
if (e.second == 1) return e.first;
}
return -1;
}
};
#endif //CPP_0137__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_053-2/cpp_053-2/Solution2.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/4/10.
//
#ifndef CPP_053_2__SOLUTION2_H_
#define CPP_053_2__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int missingNumber(vector<int> &nums) {
int len = nums.size();
for (int i = 0; i < len; ++i) {
if (i != nums[i]) return i;
}
return len;
}
};
#endif //CPP_053_2__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0781-Rabbits in Forest/cpp_0781/Solution1.h | <filename>0781-Rabbits in Forest/cpp_0781/Solution1.h
/**
* @author ooooo
* @date 2021/4/4 09:04
*/
#ifndef CPP_0781__SOLUTION1_H_
#define CPP_0781__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int numRabbits(vector<int> &answers) {
unordered_map<int, int> m;
int ans = 0;
for (auto i: answers) {
m[i]++;
}
for (auto &e: m) {
int cnt = 0;
if (e.second % (e.first + 1) == 0) {
cnt = e.second / (e.first + 1);
} else {
cnt = e.second / (e.first + 1) + 1;
}
ans += cnt * (e.first + 1);
}
return ans;
}
};
#endif //CPP_0781__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0389-Find-the-Difference/cpp_0389/Solution3.h | //
// Created by ooooo on 2020/1/11.
//
#ifndef CPP_0389__SOLUTION3_H_
#define CPP_0389__SOLUTION3_H_
#include <iostream>
#include <unordered_map>
using namespace std;
/**
* 位运算 a^a = 0 ; a^0=a 最快
*/
class Solution {
public:
char findTheDifference(string s, string t) {
s += t;
int ch = 0;
for (auto &c: s) {
ch ^= c;
}
return ch;
}
};
#endif //CPP_0389__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0452-Minimum Number of Arrows to Burst Balloons/cpp_0452/Solution1.h | /**
* @author ooooo
* @date 2020/11/23 20:23
*/
#ifndef CPP_0452__SOLUTION1_H_
#define CPP_0452__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int findMinArrowShots(vector<vector<int>> &points) {
if (points.empty()) return 0;
sort(points.begin(), points.end(), [](const vector<int>& u, const vector<int>& v) {
return u[1] < v[1];
});
int ans = 1, cur_pos = points[0][1];
for (int i = 1; i < points.size(); ++i) {
int start = points[i][0], end = points[i][1];
if (cur_pos <= end && cur_pos >= start) {
continue;
}
ans++;
cur_pos = end;
}
return ans;
}
};
#endif //CPP_0452__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0538-Convert-BST-to_Greater-Tree/cpp_0538/Solution1.h | <filename>0538-Convert-BST-to_Greater-Tree/cpp_0538/Solution1.h<gh_stars>10-100
//
// Created by ooooo on 2020/1/3.
//
#ifndef CPP_0538_SOLUTION1_H
#define CPP_0538_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
/**
* vector 存放所有的TreeNode节点(inOrder),倒序遍历。
*/
class Solution {
public:
void inOrder(TreeNode *node, vector<TreeNode *> &vec) {
if (!node) return;
inOrder(node->left, vec);
vec.push_back(node);
inOrder(node->right, vec);
}
TreeNode *convertBST(TreeNode *root) {
vector<TreeNode *> vec;
inOrder(root, vec);
int num = 0;
for (int i = vec.size() - 1; i >= 0; --i) {
vec[i]->val += num;
num = vec[i]->val;
}
return root;
}
};
#endif //CPP_0538_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1736-Latest Time by Replacing Hidden Digits/cpp_1736/Solution1.h | /**
* @author ooooo
* @date 2021/2/28 13:09
*/
#ifndef CPP_1736__SOLUTION1_H_
#define CPP_1736__SOLUTION1_H_
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <numeric>
using namespace std;
class Solution {
public:
string maximumTime(string time) {
if (time[0] == '?') {
if (time[1] == '?' || ('0' <= time[1] && time[1] <= '3')) {
time[0] = '2';
} else {
time[0] = '1';
}
}
if (time[1] == '?') {
time[1] = time[0] == '2' ? '3' : '9';
}
if (time[3] == '?') {
time[3] = '5';
}
if (time[4] == '?') {
time[4] = '9';
}
return time;
}
};
#endif //CPP_1736__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0367-Valid-Perfect-Square/cpp_0367/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/17.
//
#ifndef CPP_0367__SOLUTION2_H_
#define CPP_0367__SOLUTION2_H_
#include <iostream>
using namespace std;
class Solution {
public:
bool isPerfectSquare(int num) {
if (num < 2) return true;
long x = num / 2;
while (x * x > num) {
x = (x + num / x) / 2;
}
return x * x == num;
}
};
#endif //CPP_0367__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0007-Reverse Integer/cpp_0007/Solution1.h | /**
* @author ooooo
* @date 2021/5/3 18:23
*/
#ifndef CPP_0007__SOLUTION1_H_
#define CPP_0007__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int reverse(int x) {
int sum = 0;
while (x) {
if (sum < INT_MIN / 10 || sum > INT_MAX / 10) {
return 0;
}
sum = sum * 10 + x % 10;
x = x / 10;
}
return sum;
}
};
#endif //CPP_0007__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1078-Occurrences-After-Bigram/cpp_1078/Solution1.h | //
// Created by ooooo on 2020/1/16.
//
#ifndef CPP_1078__SOLUTION1_H_
#define CPP_1078__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> split(string s) {
vector<string> ans;
int i = 0, 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);
}
ans.push_back(s.substr(i, s.size() - i));
return ans;
}
vector<string> findOcurrences(string text, string first, string second) {
vector<string> ans, words = split(text);
for (int i = 0; i < words.size() - 2; ) {
if (words[i] == first && words[i+1] == second) {
ans.push_back(words[i + 2]);
i += 2;
}else {
i++;
}
}
return ans;
}
};
#endif //CPP_1078__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_040/cpp_040/Solution3.h | //
// Created by ooooo on 2020/3/25.
//
#ifndef CPP_040__SOLUTION3_H_
#define CPP_040__SOLUTION3_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int partition(int i, int j, vector<int> &arr) {
int k = i, item = arr[i];
for (int p = i + 1; p <= j; ++p) {
if (arr[p] <= item) {
k++;
swap(arr[k], arr[p]);
}
}
swap(arr[k], arr[i]);
return k;
}
vector<int> getLeastNumbers(vector<int> &arr, int k) {
if (k == 0) return {};
if (k == arr.size()) return arr;
int start = 0, end = arr.size() - 1;
int p = partition(start, end, arr);
while (p != k) {
if (p < k) {
start = p + 1;
} else {
end = p - 1;
}
p = partition(start, end, arr);
}
return vector<int>(arr.begin(), arr.begin() + k);
}
};
#endif //CPP_040__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0383-Ransom-Note/cpp_0383/Solution2.h | <filename>0383-Ransom-Note/cpp_0383/Solution2.h
//
// Created by ooooo on 2020/1/25.
//
#ifndef CPP_0383__SOLUTION2_H_
#define CPP_0383__SOLUTION2_H_
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
if (ransomNote.size() > magazine.size()) return false;
int arr[26] = {0};
for (auto c: magazine) {
++arr[c - 'a'];
}
for (auto c: ransomNote) {
if (arr[c - 'a'] == 0) return false;
--arr[c - 'a'];
}
return true;
}
};
#endif //CPP_0383__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0146-LRU-Cache/cpp_0146/Solution2.h | //
// Created by ooooo on 2020/2/18.
//
#ifndef CPP_0146__SOLUTION2_H_
#define CPP_0146__SOLUTION2_H_
#include <iostream>
#include <unordered_map>
using namespace std;
/**
* 要删除的节点在头结点
*/
class LRUCache {
private:
struct Node {
Node *prev, *next;
int key, value;
Node() {}
Node(int key, int value) : key(key), value(value), prev(nullptr), next(nullptr) {}
};
unordered_map<int, Node *> map;
int size, capacity;
Node *head, *tail;
public:
LRUCache(int capacity) : capacity(capacity), size(0) {
size = 0;
head = new Node(-1, -1);
tail = new Node(-1, -1);
head->next = tail;
tail->prev = head;
}
int get(int key) {
Node *n = visited(key);
return n ? n->value : -1;
}
// m -> n -> p
Node *visited(int key) {
if (!map.count(key)) return nullptr;
Node *n = map[key], *m = n->prev, *p = n->next;
m->next = p;
p->prev = m;
n->prev = n->next = nullptr;
moveTail(n);
return n;
}
void moveTail(Node *n) {
tail->prev->next = n;
n->prev = tail->prev;
n->next = tail;
tail->prev = n;
}
void put(int key, int value) {
Node *n = visited(key);
if (!n) {
n = new Node(key, value);
size++;
map[key] = n;
moveTail(n);
} else {
n->value = value;
}
if (size > capacity) {
// remove
Node *r = head->next;
r->next->prev = head;
head->next = r->next;
r->next = r->prev = nullptr;
map.erase(r->key);
size--;
}
}
};
#endif //CPP_0146__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0037-Sudoku-Solver/cpp_0037/Solution2.h | /**
* @author ooooo
* @date 2020/9/15 11:13
*/
#ifndef CPP_0037__SOLUTION2_H_
#define CPP_0037__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution2 {
public:;
vector<unordered_set<char>> groups = vector<unordered_set<char>>(9);
vector<unordered_set<char>> rows = vector<unordered_set<char>>(9);
vector<unordered_set<char>> cols = vector<unordered_set<char>>(9);
bool dfs(int i, vector<vector<char>> &board) {
if (i == 81) return true;
int row = i / 9, col = i % 9;
char c = board[row][col];
if (c != '.') {
return dfs(i + 1, board);
}
for (char k = '1'; k <= '9'; ++k) {
if (rows[row].count(k) || cols[col].count(k) || groups[row / 3 * 3 + col / 3].count(k)) continue;
rows[row].insert(k);
cols[col].insert(k);
groups[row / 3 * 3 + col / 3].insert(k);
board[row][col] = k;
if (dfs(i + 1, board)) return true;
rows[row].erase(k);
cols[col].erase(k);
groups[row / 3 * 3 + col / 3].erase(k);
board[row][col] = '.';
}
return false;
}
void solveSudoku(vector<vector<char>> &board) {
for (int row = 0; row < 9; ++row) {
for (int col = 0; col < 9; ++col) {
char c = board[row][col];
rows[row].insert(c);
cols[col].insert(c);
groups[row / 3 * 3 + col / 3].insert(c);
}
}
dfs(0, board);
}
};
#endif //CPP_0037__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0134-Gas Station/cpp_0134/Solution1.h | /**
* @author ooooo
* @date 2020/11/19 09:02
*/
#ifndef CPP_0134__SOLUTION1_H_
#define CPP_0134__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int i = 0, n = gas.size();
while (i < n) {
int g = 0, c = 0, cnt = 0;
while (cnt < n) {
int j = (i + cnt) % n;
g += gas[j];
c += cost[j];
// 需要汽油多
if (c > g) {
break;
}
cnt++;
}
if (cnt == n) {
return i;
}
i = i + cnt + 1;
}
return -1;
}
};
#endif //CPP_0134__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0953-Verifying-an-Alien-Dictionary/cpp_0953/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/16.
//
#ifndef CPP_0953__SOLUTION1_H_
#define CPP_0953__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool check(string s1, string s2, int m[]) {
// 比较最小的长度
for (int i = 0; i < min(s1.size(), s2.size()); ++i) {
int j = m[s1[i] - 'a'], k = m[s2[i] - 'a'];
if (j == k) continue;
// 只比较第一个不相同的值
else return j < k;
}
return s1.size() <= s2.size();
}
bool isAlienSorted(vector<string> &words, string order) {
int m[26] = {0};
for (int i = 0; i < order.size(); ++i) {
m[order[i] - 'a'] = i;
}
for (int i = 0; i < words.size() - 1; ++i) {
if (!check(words[i], words[i + 1], m)) return false;
}
return true;
}
};
#endif //CPP_0953__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_035/cpp_035/Node.h | <gh_stars>10-100
//
// Created by ooooo on 2020/3/22.
//
#ifndef CPP_035__NODE_H_
#define CPP_035__NODE_H_
#include <iostream>
using namespace std;
class Node {
public:
int val;
Node *next;
Node *random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
#endif //CPP_035__NODE_H_
|
ooooo-youwillsee/leetcode | 0589-N-ary-Tree-Preorder-Traversal/cpp_0589/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/3.
//
#ifndef CPP_0589_SOLUTION2_H
#define CPP_0589_SOLUTION2_H
#include "Node.h"
#include <stack>
class Solution {
public:
vector<int> preorder(Node *root) {
if (!root) return {};
vector<int> res;
stack<Node *> s;
s.push(root);
while (!s.empty()) {
Node *node = s.top();
s.pop();
res.push_back(node->val);
for (int i = node->children.size() - 1; i >= 0; --i) {
s.push(node->children[i]);
}
}
return res;
}
};
#endif //CPP_0589_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0363-Max Sum of Rectangle No Larger Than K/cpp_0363/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/4/22 17:12
*/
#ifndef CPP_0363__SOLUTION1_H_
#define CPP_0363__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
int maxSumSubmatrix(vector<vector<int>> &matrix, int k) {
int m = matrix.size(), n = matrix[0].size();
int ans = INT_MIN;
for (int i = 0; i < m; i++) {
vector<int> nums(n);
for (int j = i; j >= 0; j--) {
for (int p = 0; p < n; p++) {
nums[p] += matrix[j][p];
}
int sum = 0;
set<int> s;
// 重要!!!, 前缀和
s.insert(0);
for (auto num: nums) {
sum += num;
auto it = s.lower_bound(sum - k);
if (it != s.end()) {
ans = max(ans, sum - *it);
}
s.insert(sum);
}
}
}
return ans;
}
};
#endif //CPP_0363__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0220-Contains Duplicate III/cpp_0220/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/4/17 09:59
*/
#ifndef CPP_0220__SOLUTION1_H_
#define CPP_0220__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int> &nums, int k, int t) {
if (nums.size() < 2) return false;
int n = nums.size();
multiset<long long> set;
for (int i = 0; i < n; i++) {
auto it = set.lower_bound((long long) nums[i] - (long long) t);
if (it != set.end() && *it <= (long long) nums[i] + (long long) t) {
return true;
}
set.insert(nums[i]);
if (set.size() > k) {
set.erase(set.find(nums[i - k]));
}
}
return false;
}
};
#endif //CPP_0220__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0001-Two-Sum/cpp_0001/Solution2.h | #include <vector>
#include <iostream>
#include <map>
using namespace std;
/**
* hash表
*/
class Solution2 {
public:
static vector<int> twoSum(vector<int> &nums, int target) {
vector<int> result;
map<int, int> map;
for (int i = 0; i < nums.size(); ++i) {
map[nums[i]] = i;
if (map.find(target - nums[i]) != map.end()) {
result.push_back(i);
result.push_back(map[target - nums[i]]);
}
}
return result;
}
}; |
ooooo-youwillsee/leetcode | 1160-Find-Words-That-Can-Be-Formed-by-Characters/cpp_1160/Solution1.h | //
// Created by ooooo on 2020/1/16.
//
#ifndef CPP_1160__SOLUTION1_H_
#define CPP_1160__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int countCharacters(vector<string> &words, string chars) {
unordered_map<char, int> m;
for_each(chars.begin(), chars.end(), [&m](char c) { m[c]++; });
int ans = 0;
for (auto &item: words) {
unordered_map<char, int> wordMap;
for_each(item.begin(), item.end(), [&wordMap](char c) { wordMap[c]++; });
bool find = true;
for (auto &entry: wordMap) {
if (m[entry.first] < entry.second) {
find = false;
break;
}
}
if (find) {
ans += item.size();
}
}
return ans;
}
};
#endif //CPP_1160__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0146-LRU-Cache/cpp_0146/Solution1.h | <filename>0146-LRU-Cache/cpp_0146/Solution1.h
//
// Created by ooooo on 2020/2/18.
//
#ifndef CPP_0146__SOLUTION1_H_
#define CPP_0146__SOLUTION1_H_
#include <iostream>
#include <unordered_map>
using namespace std;
/**
* 要删除的节点在尾节点
*/
class LRUCache {
private:
struct Node {
Node *prev, *next;
int key, value;
Node() : key(0), value(0), prev(nullptr), next(nullptr) {}
Node(int key, int value) : key(key), value(value), prev(nullptr), next(nullptr) {}
};
unordered_map<int, Node *> map;
int size, capacity;
Node *head, *tail;
public:
LRUCache(int capacity) : capacity(capacity), size(0) {
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
}
void addNode(Node *node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void removeNode(Node *node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}
void moveToHead(Node *node) {
removeNode(node);
addNode(node);
}
Node *popTail() {
Node *node = tail->prev;
removeNode(node);
return node;
}
int get(int key) {
Node *node = map[key];
if (!node) return -1;
moveToHead(node);
return node->value;
}
void put(int key, int value) {
Node *node = map[key];
if (node) {
node->value = value;
moveToHead(node);
} else {
node = new Node(key, value);
map[key] = node;
addNode(node);
++size;
if (size > capacity) {
size--;
map.erase(popTail()->key);
}
}
}
};
#endif //CPP_0146__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp_0309/Solution1.h | //
// Created by ooooo on 2020/2/25.
//
#ifndef CPP_0309__SOLUTION1_H_
#define CPP_0309__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 第 i 天, j (0:不持有一股, 1:持有一股)
*
* dp[i][0] = max(dp[i-1][0], dp[i-1][1] + p)
* dp[i][1] = max(dp[i-1][1], dp[i-2][0] - p)
*/
class Solution {
public:
int maxProfit(vector<int> &prices) {
int n = prices.size();
vector<vector<int>> dp(n + 1, vector<int>(2, 0));
dp[0][0] = 0;
dp[0][1] = INT_MIN;
for (int i = 0; i < n; ++i) {
int j = i + 1, p = prices[i];
dp[j][0] = max(dp[j - 1][0], dp[j - 1][1] + p);
dp[j][1] = max(dp[j - 1][1], dp[max(j - 2, 0)][0] - p);
}
return dp[n][0];
}
};
#endif //CPP_0309__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_010-1/cpp_010-1/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/3/8.
//
#ifndef CPP_010_1__SOLUTION1_H_
#define CPP_010_1__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* F(N) = F(N - 1) + F(N - 2)
*
* recursion timeout (有重复计算)
* O(2^n)
*/
class Solution {
public:
long dfs(int n) {
if (n == 0 || n == 1) return n;
return dfs(n - 1) + dfs(n - 2);
}
int fib(int n) {
return dfs(n) % 1000000007;
}
};
#endif //CPP_010_1__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0621-Task Scheduler/cpp_0621/Solution2.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/12/5 12:49
*/
#ifndef CPP_0621__SOLUTION2_H
#define CPP_0621__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
*
*/
class Solution {
public:
int leastInterval(vector<char> &tasks, int n) {
vector<int> task_counts(26);
for (auto &t: tasks) {
task_counts[t - 'A']++;
}
sort(task_counts.begin(), task_counts.end(), greater<int>());
int cnt = 1, max_count = task_counts[0]; // 默认有一个,就是最大的
for (int i = 1; i < task_counts.size(); ++i) {
if (task_counts[i] == max_count) cnt++;
}
return max((int)tasks.size(), (max_count - 1) * (n + 1) + cnt);
}
};
#endif //CPP_0621__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0765-Couples Holding Hands/cpp_0765/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/14 09:42
*/
#ifndef CPP_0765__SOLUTION1_H_
#define CPP_0765__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
// dfs , 可以优化
// 理解最重要的一点 比如 [0,x,1,z], 要使0,1在一起的话,就是[0,1,x,z]或者[z,x,1,0]
// 上述的这两种情况,x,z只是相对位置发生了改变,而实际的交换次数并没有改变
class Solution {
public:
int ans = 0;
void dfs(unordered_map<int, int> &m, vector<int> &row, int i, int count) {
if (i == row.size()) {
ans = min(ans, count);
return;
}
int a = row[i], b = 0;
if (a % 2 == 0) {
b = a + 1;
} else {
b = a - 1;
}
int bIndex = m[b];
if (abs(bIndex - i) == 1) {
dfs(m, row, i + 2, count);
return;
}
swap(row[i + 1], row[bIndex]);
m[b] = i + 1;
m[row[bIndex]] = bIndex;
dfs(m, row, i, count + 1);
}
int minSwapsCouples(vector<int> &row) {
unordered_map<int, int> m;
for (int i = 0; i < row.size(); ++i) {
m[row[i]] = i;
}
ans = INT_MAX;
dfs(m, row, 0, 0);
return ans;
}
};
#endif //CPP_0765__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1807-Evaluate the Bracket Pairs of a String/cpp_1807/Solution1.h | /**
* @author ooooo
* @date 2021/4/4 08:31
*/
#ifndef CPP_1807__SOLUTION1_H_
#define CPP_1807__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:
string evaluate(string s, vector<vector<string>> &knowledge) {
unordered_map<string, string> m;
for (auto &item: knowledge) {
m[item[0]] = item[1];
}
int n = s.size();
string ans = "";
for (int i = 0; i < n; ++i) {
while (i < n && s[i] != '(') {
ans += s[i];
i++;
}
i++;
if (i >= n) break;
int l = i;
while (i < n && s[i] != ')') {
i++;
}
string word = s.substr(l, i - l);
if (m.find(word) == m.end()) {
ans += "?";
} else {
ans += m[word];
}
}
return ans;
}
};
#endif //CPP_1807__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0117-Populating-Next-Right-Pointers-in-Each-Node-II/cpp_0117/Node.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/9/28 09:19
*/
#ifndef CPP_0117__NODE_H_
#define CPP_0117__NODE_H_
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class Node {
public:
int val;
Node *left;
Node *right;
Node *next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node *_left, Node *_right, Node *_next)
: val(_val), left(_left), right(_right), next(_next) {}
};
#endif //CPP_0117__NODE_H_
|
ooooo-youwillsee/leetcode | 1562-Find Latest Group of Size M/cpp_1562/Solution2.h | /**
* @author ooooo
* @date 2020/12/12 20:27
*/
#ifndef CPP_1562__SOLUTION2_H_
#define CPP_1562__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int findLatestStep(vector<int> &arr, int m) {
int n = arr.size();
if (m == n) return n;
unordered_map<int, int> index2len, len2count;
int prev_i = -1;
for (int i = 0; i < n; ++i) {
int k = arr[i];
int left_len = index2len[k - 1], right_len = index2len[k + 1];
int new_len = left_len + right_len + 1;
index2len[k - left_len] = new_len;
index2len[k + right_len] = new_len;
len2count[new_len] += new_len;
len2count[left_len] -= left_len;
len2count[right_len] -= right_len;
// m的长度还存在,就记录下
if (len2count[m]) {
prev_i = i + 1;
}
}
return prev_i;
}
};
#endif //CPP_1562__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0145-Binary-Tree-Postorder-Traversal/cpp_0145/Solution2.h | /**
* @author ooooo
* @date 2020/9/29 09:21
*/
#ifndef CPP_0145__SOLUTION2_H_
#define CPP_0145__SOLUTION2_H_
#include "TreeNode.h"
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
if (!root) return ans;
stack<TreeNode *> s;
TreeNode *cur = root, *prev = nullptr;
while (!s.empty() || cur) {
if (cur) {
s.push(cur);
cur = cur->left;
} else {
auto x = s.top();
if (x->right && x->right != prev) {
cur = x->right;
} else {
s.pop();
prev = x;
ans.emplace_back(x->val);
}
}
}
return ans;
}
};
#endif //CPP_0145__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0322-Coin-Change/cpp_0322/Solution1.h | //
// Created by ooooo on 2020/2/26.
//
#ifndef CPP_0322__SOLUTION1_H_
#define CPP_0322__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* p = coins[j] ==> dp[i] = min(dp[i],dp[i-p] + 1)
*/
class Solution {
public:
int coinChange(vector<int> &coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; ++i) {
for (int j = 0; j < coins.size(); ++j) {
int k = i - coins[j];
if (k >= 0) {
dp[i] = min(dp[i], dp[k] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
};
#endif //CPP_0322__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_009/cpp_009/Solution1.h | <filename>lcof_009/cpp_009/Solution1.h<gh_stars>10-100
//
// Created by ooooo on 2020/3/8.
//
#ifndef CPP_009__SOLUTION1_H_
#define CPP_009__SOLUTION1_H_
#include <iostream>
#include <stack>
using namespace std;
class CQueue {
private:
stack<int> s1, s2;
public:
CQueue() {
}
void appendTail(int value) {
s1.push(value);
}
int deleteHead() {
if (s1.empty() && s2.empty()) return -1;
if (s2.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
int ret = s2.top();
s2.pop();
return ret;
}
};
#endif //CPP_009__SOLUTION1_H_
|
Subsets and Splits