repo_name
stringlengths 5
122
| path
stringlengths 3
232
| text
stringlengths 6
1.05M
|
---|---|---|
ooooo-youwillsee/leetcode | 0027-Remove-Element/cpp_0027/Solution2.h | //
// Created by ooooo on 2020/1/5.
//
#ifndef CPP_0027_SOLUTION2_H
#define CPP_0027_SOLUTION2_H
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int removeElement(vector<int> &nums, int val) {
if (nums.size() < 1) return 0;
int i = 0, j = nums.size() - 1;
while (true) {
// 先判断i和j是否越界
while (i < nums.size() && nums[i] != val) i++;
while (j >= 0 && nums[j] == val) j--;
if (i >= j) break;
swap(nums[i], nums[j]);
i++;
j--;
}
return i;
}
};
#endif //CPP_0027_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0860-Lemonade-Change/cpp_0860/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/9/30 10:57
*/
#ifndef CPP_0860__SOLUTION1_H_
#define CPP_0860__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool lemonadeChange(vector<int> &bills) {
int bill_5 = 0, bill_10 = 0;
for (auto &a :bills) {
if (a == 5) {
bill_5++;
} else if (a == 10) {
bill_10++;
bill_5--;
} else {
if (bill_10 > 0) {
bill_10--;
bill_5--;
} else {
bill_5 -= 3;
}
}
if (bill_10 < 0 || bill_5 < 0) return false;
}
return true;
}
};
#endif //CPP_0860__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_045/cpp_045/Solution1.h | //
// Created by ooooo on 2020/4/1.
//
#ifndef CPP_045__SOLUTION1_H_
#define CPP_045__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
/**
* 和 leetcode 不一样
*/
class Solution {
public:
bool static compareTo(string &s1, string &s2) {
int len1 = s1.size(), len2 = s2.size();
for (int i = 0, j = 0; i < len1 || j < len2;) {
if (i == len1) {
return s2[i] > s1[0];
}
if (j == len2) {
return s1[i] < s2[0];
}
if (s1[i] == s2[j]) {
i++;
j++;
} else return s1[i] < s2[j];
}
return true;
}
string minNumber(vector<int> &nums) {
vector<string> vec;
for (auto &num: nums) vec.push_back(to_string(num));
sort(vec.begin(), vec.end(), compareTo);
stringstream ss;
for (auto &str: vec) {
ss << str;
}
return ss.str();
}
};
#endif //CPP_045__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0116-Populating Next Right Pointers in Each Node/cpp_0116/Solution1.h | <filename>0116-Populating Next Right Pointers in Each Node/cpp_0116/Solution1.h
/**
* @author ooooo
* @date 2020/10/15 19:35
*/
#ifndef CPP_0116__SOLUTION1_H_
#define CPP_0116__SOLUTION1_H_
#include "Node.h"
class Solution {
public:
Node *getNode(Node *node) {
if (!node) return nullptr;
if (node->left) return node->left;
if (node->right) return node->right;
return getNode(node->next);
}
Node *connect(Node *root) {
if (!root) return root;
if (!root->left && !root->right) return root;
root->left->next = root->right ? root->right : getNode(root->next);
root->right->next = getNode(root->next);
connect(root->right);
connect(root->left);
return root;
}
};
#endif //CPP_0116__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_024/cpp_024/Solution1.h | //
// Created by ooooo on 2020/3/16.
//
#ifndef CPP_024__SOLUTION1_H_
#define CPP_024__SOLUTION1_H_
#include "ListNode.h"
class Solution {
public:
ListNode *reverseList(ListNode *head) {
ListNode *cur = head, *prev = nullptr;
while (cur) {
ListNode *node = cur;
cur = node->next;
node->next = prev;
prev = node;
}
return prev;
}
};
#endif //CPP_024__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0976-Largest Perimeter Triangle/cpp_0976/Solution1.h | /**
* @author ooooo
* @date 2020/11/29 09:20
*/
#ifndef CPP_0976__SOLUTION1_H_
#define CPP_0976__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int largestPerimeter(vector<int> &A) {
sort(A.begin(), A.end());
for (int i = (int) A.size() - 1; i >= 2; --i) {
if (A[i - 2] + A[i - 1] > A[i]) {
return A[i - 2] + A[i - 1] + A[i];
}
}
return 0;
}
};
#endif //CPP_0976__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0240-Search-a-2D-Matrix-II/cpp_0240/Solution1.h | //
// Created by ooooo on 2020/2/22.
//
#ifndef CPP_0240__SOLUTION1_H_
#define CPP_0240__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int>> &matrix, int target) {
if (matrix.empty()) return false;
int m = matrix.size(), n = matrix[0].size(), row = 0, col = n - 1;
while (row >= 0 && row < m && col >= 0 && col < n) {
if (matrix[row][col] == target) {
return true;
} else if (matrix[row][col] < target) {
row++;
} else {
col--;
}
}
return false;
}
};
#endif //CPP_0240__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0376-Wiggle Subsequence/cpp_0376/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/12/12 11:31
*/
#ifndef CPP_0376__SOLUTION1_H_
#define CPP_0376__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int wiggleMaxLength(vector<int> &nums) {
int n = nums.size();
if (n <= 1) return n;
vector<int> dp(n, 1);
for (int i = 1; i < n; ++i) {
dp[i] = nums[i] - nums[0] != 0 ? 2 : 1;
for (int k = 1; k < i; ++k) {
int diff = nums[i] - nums[k];
int prevDiff = nums[k] - nums[k - 1];
if ((diff < 0 && prevDiff > 0) || (diff > 0 && prevDiff < 0)) {
dp[i] = max(dp[i], dp[k] + 1);
}
}
}
return dp[n - 1];
}
};
#endif //CPP_0376__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0322-Coin-Change/cpp_0322/Solution3.h | //
// Created by ooooo on 2020/2/26.
//
#ifndef CPP_0322__SOLUTION3_H_
#define CPP_0322__SOLUTION3_H_
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
* bfs timeout
*/
class Solution {
public:
int coinChange(vector<int> &coins, int amount) {
if (amount == 0) return 0;
sort(coins.begin(), coins.end());
queue<int> q;
q.push(amount);
int level = 0;
while (!q.empty()) {
level += 1;
for (int i = 0, len = q.size(); i < len; ++i) {
auto n = q.front();
q.pop();
for (int j = 0; j < coins.size() && n >= coins[j]; ++j) {
int k = n - coins[j];
if (k == 0) return level;
else {
q.push(k);
}
}
}
}
return -1;
}
};
#endif //CPP_0322__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0416-Partition-Equal-Subset-Sum/cpp_0416/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/29.
//
#ifndef CPP_0416__SOLUTION2_H_
#define CPP_0416__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
/**
* dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
*
* 背包问题
*/
class Solution {
public:
bool canPartition(vector<int> &nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if (sum % 2 == 1) return false;
int n = nums.size(), target = sum / 2;
vector<vector<bool>> dp(n, vector<bool>(target + 1, false));
dp[0][0] = true;
if (nums[0] <= target) {
dp[0][nums[0]] = true;
}
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= target; ++j) {
dp[i][j] = dp[i - 1][j];
if (nums[i] <= j) {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i]];
}
}
if (dp[i][target]) return true;
}
return dp[n - 1][target];
}
};
#endif //CPP_0416__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_065/cpp_065/Solution1.h | <filename>lcof_065/cpp_065/Solution1.h
//
// Created by ooooo on 2020/4/23.
//
#ifndef CPP_065__SOLUTION1_H_
#define CPP_065__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
int add(int a, int b) {
int num1 = 0, num2 = 0;
do {
num1 = a ^ b; // 不进位
num2 = (unsigned int) (a & b) << 1; // 进1
a = num1;
b = num2;
} while (num2 != 0);
return num1;
}
};
#endif //CPP_065__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0888-Fair-Candy-Swap/cpp_0888/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/9/30 15:42
*/
#ifndef CPP_0888__SOLUTION1_H_
#define CPP_0888__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <numeric>
using namespace std;
class Solution {
public:
vector<int> fairCandySwap(vector<int> &A, vector<int> &B) {
int sum_a = accumulate(A.begin(), A.end(), 0), sum_b = accumulate(B.begin(), B.end(), 0);
unordered_set<int> s(B.begin(), B.end());
int diff = (sum_a + sum_b) / 2 - sum_a;
for (auto &item: A) {
if (s.find(item + diff) != s.end()) return {item, item + diff};
}
return {};
}
};
#endif //CPP_0888__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0746-Min-Cost-Climbing-Stairs/cpp_0746/Solution1.h | <reponame>ooooo-youwillsee/leetcode<filename>0746-Min-Cost-Climbing-Stairs/cpp_0746/Solution1.h
//
// Created by ooooo on 2020/2/5.
//
#ifndef CPP_0746__SOLUTION1_H_
#define CPP_0746__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dp[i] = min(dp[i-1] , dp[i-2]) + p (从第三个开始走)
*/
class Solution {
public:
int minCost(vector<int> &cost, int start, int end) {
vector<int> dp(end - start + 3);
int ans = INT_MAX;
dp[start] = cost[start];
dp[start + 1] = dp[start] + cost[start + 1];
for (int i = start + 2; i <= end; ++i) {
int p = cost[i];
dp[i] = min(dp[i - 1], dp[i - 2]) + p;
if (i >= end - 1) {
ans = min(ans, dp[i]);
}
}
return ans;
}
int minCostClimbingStairs(vector<int> &cost) {
cost.push_back(0);
int end = cost.size() - 1;
return min(minCost(cost, 0, end), minCost(cost, 1, end));
}
};
#endif //CPP_0746__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0765-Couples Holding Hands/cpp_0765/Solution2.h | <filename>0765-Couples Holding Hands/cpp_0765/Solution2.h
/**
* @author ooooo
* @date 2021/2/14 09:43
*/
#ifndef CPP_0765__SOLUTION2_H_
#define CPP_0765__SOLUTION2_H_
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
class UF {
public:
vector<int> p;
int n;
UF(int n) : n(n), p(n) {
for (int i = 0; i < n; ++i) {
p[i] = i;
}
}
int find(int i) {
if (p[i] == i) return i;
return p[i] = find(p[i]);
}
bool connect(int i, int j) {
int pi = find(i), pj = find(j);
if (pi == pj) return true;
p[pi] = pj;
n--;
return false;
}
};
int minSwapsCouples(vector<int> &row) {
int n = row.size();
UF uf(n / 2);
for (int i = 0; i < n; i += 2) {
uf.connect(row[i] / 2, row[i + 1] / 2);
}
return n / 2 - uf.n;
}
};
#endif //CPP_0765__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0200-Number-of-Islands/cpp_0200/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/19.
//
#ifndef CPP_0200__SOLUTION1_H_
#define CPP_0200__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* dfs
*/
class Solution {
public:
void dfs(int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || mark[i][j])return;
mark[i][j] = true;
if (grid[i][j] == '0') return;
dfs(i, j + 1);
dfs(i, j - 1);
dfs(i + 1, j);
dfs(i - 1, j);
}
vector<vector<char>> grid;
vector<vector<bool>> mark;
int ans = 0, m, n;
int numIslands(vector<vector<char>> &grid) {
if (grid.empty()) return 0;
this->grid = grid;
this->m = grid.size();
this->n = grid[0].size();
this->mark.assign(m, vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (!mark[i][j] && grid[i][j] == '1') {
ans++;
dfs(i, j);
}
}
}
return ans;
}
};
#endif //CPP_0200__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcci_17_12/cpp_17_12/Solution2.h | /**
* @author ooooo
* @date 2021/4/2 18:20
*/
#ifndef CPP_17_12__SOLUTION2_H_
#define CPP_17_12__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int trap(vector<int> &height) {
int n = height.size();
vector<int> left(n, 0), right(n, 0);
for (int i = 1; i < n - 1; i++) {
left[i] = max(left[i - 1], height[i - 1]);
}
for (int i = n - 2; i > 0; i--) {
right[i] = max(right[i + 1], height[i + 1]);
}
int ans = 0;
for (int i = 0; i < n; i++) {
int h = min(left[i], right[i]);
if (h > height[i]) {
ans += h - height[i];
}
}
return ans;
}
};
#endif //CPP_17_12__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1801-Number of Orders in the Backlog/cpp_1801/Solution1.h | /**
* @author ooooo
* @date 2021/3/28 12:18
*/
#ifndef CPP_1801__SOLUTION1_H_
#define CPP_1801__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:
typedef pair<int, int> pii;
int getNumberOfBacklogOrders(vector<vector<int>> &orders) {
int MOD = 1e9 + 7;
priority_queue<pii> buyMaxHeap;
priority_queue<pii, vector<pii>, greater<pii>> sellMinHeap;
for (auto &v : orders) {
if (v[2] == 0) {
while (!sellMinHeap.empty() && sellMinHeap.top().first <= v[0]) {
int p = sellMinHeap.top().first;
v[1] -= sellMinHeap.top().second;
sellMinHeap.pop();
if (v[1] < 0) {
sellMinHeap.emplace(p, -v[1]);
break;
}
}
if (v[1] > 0) {
buyMaxHeap.emplace(v[0], v[1]);
}
} else if (v[2] == 1) {
while (!buyMaxHeap.empty() && buyMaxHeap.top().first >= v[0]) {
int p = buyMaxHeap.top().first;
v[1] -= buyMaxHeap.top().second;
buyMaxHeap.pop();
if (v[1] < 0) {
buyMaxHeap.emplace(p, -v[1]);
break;
}
}
if (v[1] > 0) {
sellMinHeap.emplace(v[0], v[1]);
}
}
}
long long ans = 0;
while (!sellMinHeap.empty()) {
ans = (ans + sellMinHeap.top().second) % MOD;
sellMinHeap.pop();
}
while (!buyMaxHeap.empty()) {
ans = (ans + buyMaxHeap.top().second) % MOD;
buyMaxHeap.pop();
}
return ans;
}
};
#endif //CPP_1801__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0389-Find-the-Difference/cpp_0389/Solution1.h | //
// Created by ooooo on 2020/1/11.
//
#ifndef CPP_0389__SOLUTION1_H_
#define CPP_0389__SOLUTION1_H_
#include <iostream>
using namespace std;
/**
* sort
*/
class Solution {
public:
char findTheDifference(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for (int i = 0; i < s.size(); ++i) {
if (s[i] != t[i]) return t[i];
}
return t[t.size() - 1];
}
};
#endif //CPP_0389__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0224-Basic Calculator/cpp_0224/Solution1.h | <filename>0224-Basic Calculator/cpp_0224/Solution1.h
]/**
* @author ooooo
* @date 2021/3/10 11:52
*/
#ifndef CPP_0224__SOLUTION1_H_
#define CPP_0224__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
void calc(stack<long long> &numStack, stack<char> &opStack) {
long long x = numStack.top();
numStack.pop();
long long y = numStack.top();
numStack.pop();
char op = opStack.top();
opStack.pop();
if (op == '+') {
numStack.push(y + x);
} else if (op == '-') {
numStack.push(y - x);
}
}
// 测试用例
// "(7)-(0)+(4)"
// "-2+ 1"
int calculate(string s) {
int i = 0, n = s.size();
stack<long long> numStack;
numStack.push(0);
stack<char> opStack;
opStack.push('+');
while (i < n) {
while (i < n && s[i] == ' ') i++;
if (i >= n) break;
if (s[i] == '+' || s[i] == '-' || s[i] == '(') {
opStack.push(s[i]);
i++;
continue;
}
if (s[i] == ')') {
while (!opStack.empty() && opStack.top() != '(') {
calc(numStack, opStack);
}
opStack.pop();
if (!opStack.empty() && opStack.top() == '-') {
calc(numStack, opStack);
}
i++;
continue;
}
long long num = 0;
while (i < n && s[i] >= '0' && s[i] <= '9') {
num = num * 10 + s[i] - '0';
i++;
}
numStack.push(num);
if (!opStack.empty() && opStack.top() == '-') {
calc(numStack, opStack);
}
}
while (numStack.size() > 1) {
calc(numStack, opStack);
}
return numStack.top();
}
};
#endif //CPP_0224__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1482-Minimum Number of Days to Make m Bouquets/cpp_1482/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/5/9 17:35
*/
#ifndef CPP_1482__SOLUTION1_H_
#define CPP_1482__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool check(vector<int> &bloomDay, int m, int k, int target) {
int cnt = 0, cur_cnt = 0;
for (int i = 0; i < bloomDay.size(); i++) {
if (bloomDay[i] <= target) {
cur_cnt++;
}
if (bloomDay[i] > target) {
cnt += cur_cnt / k;
cur_cnt = 0;
}
}
cnt += cur_cnt / k;
return cnt >= m;
}
int minDays(vector<int> &bloomDay, int m, int k) {
if (bloomDay.size() < m * k) return -1;
int l = bloomDay[0], r = bloomDay[0];
for (auto b: bloomDay) {
l = min(l, b);
r = max(r, b);
}
int ans = 0;
while (l <= r) {
int mid = l + (r - l) / 2;
if (check(bloomDay, m, k, mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
};
#endif //CPP_1482__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0169-Majority-Element/cpp_0169/Solution2.h | //
// Created by ooooo on 2019/11/4.
//
#ifndef CPP_0169_SOLUTION2_H
#define CPP_0169_SOLUTION2_H
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int majorityElement(vector<int> &nums) {
sort(nums.begin(), nums.end());
int size = nums.size();
if (size == 1) {
return nums[0];
}
for (int i = 0; i < size; i++) {
if (size / 2 < size && nums[i] == nums[size / 2]) {
return nums[i];
}
}
return 0;
}
};
#endif //CPP_0169_SOLUTION2_H
|
ooooo-youwillsee/leetcode | lcof_018/cpp_018/ListNode.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/3/13.
//
#ifndef CPP_018__LISTNODE_H_
#define CPP_018__LISTNODE_H_
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
ListNode(vector<int> nums) {
ListNode *dummyHead = new ListNode(0), *cur = dummyHead;
for (auto &num : nums) {
cur->next = new ListNode(num);
cur = cur->next;
}
*this = *dummyHead->next;
dummyHead->next = nullptr;
delete dummyHead;
}
};
#endif //CPP_018__LISTNODE_H_
|
ooooo-youwillsee/leetcode | 0345-Reverse-Vowels-of-a-String/cpp_0345/Solution1.h | <filename>0345-Reverse-Vowels-of-a-String/cpp_0345/Solution1.h
//
// Created by ooooo on 2020/1/8.
//
#ifndef CPP_0345_SOLUTION1_H
#define CPP_0345_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<char> vowels = {'a', 'e', 'i', 'o', 'u'};
bool isVowel(char c) {
for (int i = 0; i < vowels.size(); ++i) {
if (vowels[i] == tolower(c)) return true;
}
return false;
}
string reverseVowels(string s) {
if (s.size() <= 1) return s;
for (int i = 0, j = s.size() - 1; i < j;) {
while (i <= s.size() - 1 && !isVowel(s[i])) i++;
while (j >= 0 && !isVowel(s[j])) j--;
if (i > j) break;
swap(s[i++], s[j--]);
}
return s;
}
};
#endif //CPP_0345_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0082-Remove Duplicates from Sorted List II/cpp_0082/Solution1.h | /**
* @author ooooo
* @date 2021/3/25 13:00
*/
#ifndef CPP_0082__SOLUTION1_H_
#define CPP_0082__SOLUTION1_H_
#include <iostream>
#include <unordered_map>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode *dummyHead = new ListNode(-1), *node = dummyHead;
ListNode *cur = head;
while (cur) {
ListNode *a = cur->next;
bool flag = false;
while (a) {
if (a->val == cur->val) {
flag = true;
a = a->next;
} else {
break;
}
}
if (flag) {
cur = a;
continue;
}
node->next = cur;
cur = cur->next;
node = node->next;
node->next = nullptr;
}
return dummyHead->next;
}
};
#endif //CPP_0082__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0015-3Sum/cpp_0015/Solution2.h | //
// Created by ooooo on 2019/10/31.
//
#ifndef CPP_0015_SOLUTION2_H
#define CPP_0015_SOLUTION2_H
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> res;
set<vector<int>> set;
for (int i = 0, len = nums.size(); i < len - 2; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int s = i + 1;
int e = len - 1;
while (s < e) {
int n = nums[s] + nums[e] + nums[i];
if (n < 0) {
s += 1;
} else if (n > 0) {
e -= 1;
} else {
vector<int> vec = {nums[i], nums[s], nums[e]};
set.insert(vec);
s += 1;
e -= 1;
}
}
}
for (auto item : set) {
res.push_back(item);
}
return res;
}
};
#endif //CPP_0015_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0718-Maximum-Length-of-Repeated-Subarray/cpp_0718/Solution1.h | /**
* @author ooooo
* @date 2020/10/5 13:13
*/
#ifndef CPP_0718__SOLUTION1_H_
#define CPP_0718__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 二维数组
*/
class Solution {
public:
int findLength(vector<int> &A, vector<int> &B) {
int m = A.size(), n = B.size();
int ans = 0;
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i < m; i++) {
dp[i][0] = B[0] == A[i] ? 1 : 0;
}
for (int j = 0; j < n; j++) {
dp[0][j] = A[0] == B[j] ? 1 : 0;
}
// cout << endl;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (A[i] == B[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = 0;
}
ans = max(ans, dp[i][j]);
// cout << " i: "<< i <<" j: " << j << " : " << dp[i][j] << "\t ";
}
//cout << endl;
}
return ans;
}
};
#endif //CPP_0718__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_058-1/cpp_058-1/Solution2.h | <filename>lcof_058-1/cpp_058-1/Solution2.h<gh_stars>10-100
//
// Created by ooooo on 2020/4/18.
//
#ifndef CPP_058_1__SOLUTION2_H_
#define CPP_058_1__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class Solution {
public:
void reverse(string &s, int i, int j) {
while (i < j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
}
void findFirstLetter(string &s, int &i) {
for (; i < s.size(); i++) {
if (s[i] != ' ') break;
}
}
string reverseWords(string s) {
int i = 0;
string ss;
while (true) {
findFirstLetter(s, i);
if (i == s.size()) break;
for (; i < s.size(); i++) {
if (s[i] != ' ') ss += s[i];
else break;
}
ss += ' ';
}
string str = ss.substr(0, ss.size() - 1);
reverse(str, 0, str.size() - 1);
int j = 0, k = str.find(" ", j);
while (k != -1) {
reverse(str, j, k - 1);
j = k + 1;
k = str.find(" ", j);
}
reverse(str, j, str.size() - 1);
return str;
}
};
#endif //CPP_058_1__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0438-Find-All-Anagrams-in-a-String/cpp_0438/Solution2.h | //
// Created by ooooo on 2020/3/1.
//
#ifndef CPP_0438__SOLUTION2_H_
#define CPP_0438__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/**
* 滑动窗口
*/
class Solution {
public:
unordered_map<char, int> window, needs;
vector<int> findAnagrams(string s, string p) {
if (p.size() > s.size()) return {};
int l = 0, r = 0, match = 0;
for (auto &c: p) ++needs[c];
vector<int> ans;
while (r < s.size()) {
char c = s[r++];
if (needs.count(c)) {
window[c]++;
if (window[c] == needs[c]) match++;
}
while (match == needs.size()) {
if (r - l == p.size()) ans.push_back(l);
c = s[l++];
if (needs.count(c)) {
window[c]--;
if (window[c] < needs[c]) match--;
}
}
}
return ans;
}
};
#endif //CPP_0438__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0125-Valid-Palindrome/cpp_0125/Solution1.h | //
// Created by ooooo on 2020/1/7.
//
#ifndef CPP_0125_SOLUTION1_H
#define CPP_0125_SOLUTION1_H
#include <iostream>
using namespace std;
class Solution {
public:
bool isPalindrome(string s) {
if (s.size() <= 1) return true;
int left = 0, right = s.size() - 1;
while (left < right) {
int a = tolower(s[left]);
int b = tolower(s[right]);
while (right > left && !isdigit(a) && !isalpha(a)) {
left++;
a = tolower(s[left]);
}
while (right > left && !isdigit(b) && !isalpha(b)) {
right--;
b = tolower(s[right]);
}
if (a == b) {
left++;
right--;
} else {
return false;
}
}
return true;
}
};
#endif //CPP_0125_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0219-Contains-Duplicate-II/cpp_0219/Solution2.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/6.
//
#ifndef CPP_0219_SOLUTION2_H
#define CPP_0219_SOLUTION2_H
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/**
* 滑动窗口 (窗口大小为k)
*/
class Solution {
public:
bool containsNearbyDuplicate(vector<int> &nums, int k) {
unordered_set<int> s;
for (int i = 0, len = nums.size(); i < len; ++i) {
if (s.count(nums[i])) return true;
s.insert(nums[i]);
if (s.size() > k) {
s.erase(nums[i - k]);
}
}
return false;
}
};
#endif //CPP_0219_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 1791-Find Center of Star Graph/cpp_1791/Solution1.h | /**
* @author ooooo
* @date 2021/3/28 12:11
*/
#ifndef CPP_1791__SOLUTION1_H_
#define CPP_1791__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 findCenter(vector<vector<int>> &edges) {
unordered_map<int, int> m;
for (auto &e: edges) {
int u = e[0], v = e[1];
m[u]++;
m[v]++;
}
for (auto &e: m) {
if (e.second == m.size() - 1) {
return e.first;
}
}
return false;
}
};
#endif //CPP_1791__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0215-Kth-Largest-Element-in-an-Array/cpp_0215/Solution1.h | <filename>0215-Kth-Largest-Element-in-an-Array/cpp_0215/Solution1.h
//
// Created by ooooo on 2020/2/20.
//
#ifndef CPP_0215__SOLUTION1_H_
#define CPP_0215__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* sort
*/
class Solution {
public:
int findKthLargest(vector<int> &nums, int k) {
sort(nums.begin(), nums.end(), greater<int>());
return nums[k - 1];
}
};
#endif //CPP_0215__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0231-Power of Two/cpp_0231/Solution1.h | //
// Created by ooooo on 2019/12/5.
//
#ifndef CPP_0231_SOLUTION1_H
#define CPP_0231_SOLUTION1_H
#include <iostream>
using namespace std;
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && (n & n - 1) == 0;
}
};
#endif //CPP_0231_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 1031-Maximum Sum of Two Non-Overlapping Subarrays/cpp_1031/Solution1.h | <filename>1031-Maximum Sum of Two Non-Overlapping Subarrays/cpp_1031/Solution1.h
/**
* @author ooooo
* @date 2021/2/15 17:39
*/
#ifndef CPP_1031__SOLUTION1_H_
#define CPP_1031__SOLUTION1_H_
#include <vector>
using namespace std;
/**
* 滑动窗口
*/
class Solution {
public:
int maxSum(vector<int> &A, int l, int r, int len) {
if (r - l + 1 < len) {
return -1;
}
int sum = 0;
int start = l, end = l;
int ans = 0;
while (end <= r) {
sum += A[end];
if (end - start + 1 < len) {
end++;
continue;
}
ans = max(ans, sum);
sum -= A[start];
start++;
end++;
}
return ans;
}
int maxSumTwoNoOverlap(vector<int> &A, int L, int M) {
int l = 0, r = 0;
int ans = 0;
int sumL = 0, sumM = 0;
while (r < A.size()) {
sumL += A[r];
if (r - l + 1 < L) {
r++;
continue;
}
sumM = max(maxSum(A, 0, l - 1, M), maxSum(A, r + 1, A.size() - 1, M));
ans = max(ans, sumL + sumM);
sumL -= A[l];
l++;
r++;
}
return ans;
}
};
#endif //CPP_1031__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_043/cpp_043/Solution1.h | <filename>lcof_043/cpp_043/Solution1.h
//
// Created by ooooo on 2020/3/28.
//
#ifndef CPP_043__SOLUTION1_H_
#define CPP_043__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* timeout
*/
class Solution {
public:
int count1(int n) {
int count = 0;
while (n) {
if (n % 10 == 1) count++;
n /= 10;
}
return count;
}
int countDigitOne(int n) {
int ans = 0;
for (int i = 1; i <= n; ++i) {
ans += count1(i);
}
return ans;
}
};
#endif //CPP_043__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0002-Add-Two-Numbers/cpp_0002/ListNode.h | /**
* @author ooooo
* @date 2020/10/4 12:42
*/
#ifndef CPP_0002__LISTNODE_H_
#define CPP_0002__LISTNODE_H_
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
#endif //CPP_0002__LISTNODE_H_
|
ooooo-youwillsee/leetcode | 0714-Best Time to Buy and Sell Stock with Transaction Fee/cpp_0714/Solution1.h | /**
* @author ooooo
* @date 2020/12/19 19:06
*/
#ifndef CPP_0714__SOLUTION1_H_
#define CPP_0714__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices, int fee) {
int n = prices.size();
vector<vector<int>> dp(n + 1, vector<int>(2));
dp[0][1] = -50000;
for (int i = 0; i < n; ++i) {
int p = prices[i];
dp[i + 1][0] = max(dp[i][0], dp[i][1] + p - fee);
dp[i + 1][1] = max(dp[i][1], dp[i][0] - p);
}
return dp[n][0];
}
};
#endif //CPP_0714__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_064/cpp_064/Solution1.h | //
// Created by ooooo on 2020/4/21.
//
#ifndef CPP_064__SOLUTION1_H_
#define CPP_064__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
int sumNums(int n) {
bool f = n > 1 && (n += sumNums(n - 1));
return n;
}
};
#endif //CPP_064__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0115-Distinct Subsequences/cpp_0115/Solution1.h | /**
* @author ooooo
* @date 2021/3/17 18:04
*/
#ifndef CPP_0115__SOLUTION1_H_
#define CPP_0115__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
using namespace std;
class Solution {
public:
// dp[i][j] 表示以s中j位置结尾的子序列个数
int numDistinct(string s, string t) {
int m = s.size(), n = t.size();
vector<vector<long long>> dp(n, vector<long long>(m, 0));
for (int i = 0; i < m; ++i) {
if (s[i] == t[0]) {
// 第一个字符结尾的
dp[0][i] = 1;
}
}
for (int i = 1; i < n; ++i) {
// 向后查找有dp[i - 1][j]的子序列个数
for (int j = 0; j < m; ++j) {
if (dp[i - 1][j] > 0) {
// 大于0,说明有
for (int k = j + 1; k < m; ++k) {
if (s[k] == t[i]) {
dp[i][k] += dp[i - 1][j];
}
}
}
}
}
return accumulate(dp[n - 1].begin(), dp[n - 1].end(), (long long) 0);
}
};
#endif //CPP_0115__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0830-Positions-of-Large-Groups/cpp_0830/Solution1.h | /**
* @author ooooo
* @date 2020/9/30 09:30
*/
#ifndef CPP_0830__SOLUTION1_H_
#define CPP_0830__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> largeGroupPositions(string s) {
vector<vector<int>> ans;
int prev = 0, cur = 0;
while (cur < s.size()) {
if (cur == s.size() - 1 || s[cur] != s[cur + 1]) {
if (cur - prev >= 2) {
ans.push_back({prev, cur});
}
prev = cur + 1;
}
cur++;
}
return ans;
}
};
#endif //CPP_0830__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1805-Number of Different Integers in a String/cpp_1805/Solution1.h | /**
* @author ooooo
* @date 2021/4/4 08:28
*/
#ifndef CPP_1805__SOLUTION1_H_
#define CPP_1805__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 numDifferentIntegers(string word) {
unordered_set<string> set;
int zero = 0;
for (int i = 0; i < word.size();) {
while (i < word.size() && isalpha(word[i])) {
i++;
}
int l = i;
while (i < word.size() && word[i] >= '0' && word[i] <= '9') {
i++;
}
if (i != l) {
int k = l;
while (k < word.size() && word[k] == '0') {
k++;
}
if (k == i) {
zero = 1;
} else {
set.insert(word.substr(k, i - k));
}
}
}
return set.size() + zero;
}
};
#endif //CPP_1805__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcci_17_13/cpp_17_13/Solution3.h | /**
* @author ooooo
* @date 2020/10/11 10:29
*/
#ifndef CPP_17_13__SOLUTION3_H_
#define CPP_17_13__SOLUTION3_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <stack>
#include <queue>
using namespace std;
class Solution {
public:
// 5535 https://leetcode-cn.com/contest/weekly-contest-210/problems/maximum-nesting-depth-of-the-parentheses/
int maxDepth(string s) {
stack<char> stack;
int ans = 0;
for (auto &c : s) {
if (c == '(') {
stack.push(c);
} else if (c == ')') {
ans = max(ans, (int) stack.size());
stack.pop();
}
}
return ans;
}
//5536 https://leetcode-cn.com/contest/weekly-contest-210/problems/maximal-network-rank/
int maximalNetworkRank(int n, vector<vector<int>> &roads) {
vector<vector<int>> g(n);
for (auto &road: roads) {
int u = road[0], v = road[1];
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j) continue;
int sum = g[i].size() + g[j].size();
for (auto &r: g[i]) {
if (r == j) {
sum--;
break;
}
}
ans = max(ans, sum);
}
}
return ans;
}
// 5537 https://leetcode-cn.com/contest/weekly-contest-210/problems/split-two-strings-to-make-palindrome/
bool help(string s) {
int l = 0, r = s.size() - 1;
while (l <= r) {
if (s[l] == s[r]) {
if (r == s.size() - 2 * l - 1) return true;
l++;
r--;
} else {
break;
}
}
return false;
}
bool checkPalindromeFormation(string a, string b) {
return a.size() <= 1 || help(a + b) || help(b + a);
}
//5538 https://leetcode-cn.com/contest/weekly-contest-210/problems/count-subtrees-with-max-distance-between-cities/
set<pair<int, int>> ss;
int bfs(int u, int n, int target) {
int count = 0;
queue<int> q;
q.push(u);
vector<int> paths(n + 1, 0);
while (!q.empty()) {
int x = q.front();
q.pop();
visited[x] = true;
for (auto &v: g[x]) {
if (visited[v]) continue;
q.push(v);
paths[v] = paths[x] + 1;
if (paths[v] == target) {
count++;
}
}
}
for (int i = 1; i < paths.size(); ++i) {
if (paths[i] == target) {
ss.insert({u, i});
ss.insert({i, u});
}
}
return count;
}
vector<vector<int>> g;
vector<bool> visited;
vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>> &edges) {
g.assign(n + 1, {});
int in_v = -1;
for (auto &e: edges) {
int u = e[0], v = e[1];
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> ans;
for (int target = 1; target < n; ++target) {
for (int u = 1; u <= n; ++u) {
visited.assign(n + 1, false);
bfs(u, n, target);
}
ans.push_back(ss.size());
ss.clear();
}
return ans;
}
};
#endif //CPP_17_13__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0034-Search-for-a-Range/cpp_0034/Solution1.h | <reponame>ooooo-youwillsee/leetcode<filename>0034-Search-for-a-Range/cpp_0034/Solution1.h<gh_stars>10-100
//
// Created by ooooo on 2020/2/9.
//
#ifndef CPP_0034__SOLUTION1_H_
#define CPP_0034__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 二分法
*/
class Solution {
public:
vector<int> searchRange(vector<int> &nums, int target) {
if (nums.empty()) return {-1, -1};
int left = 0, right = (int) nums.size() - 1, mid = 0;
while (left <= right) {
mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
break;
}
}
if (nums[mid] != target) return {-1, -1};
left = mid - 1, right = mid + 1;
while (left >= 0 && nums[left] == nums[mid]) left--;
while (right < nums.size() && nums[right] == nums[mid]) right++;
return {left + 1, right - 1};
}
};
#endif //CPP_0034__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0115-Distinct Subsequences/cpp_0115/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/3/17 19:18
*/
#ifndef CPP_0115__SOLUTION2_H_
#define CPP_0115__SOLUTION2_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
using namespace std;
class Solution {
public:
int numDistinct(string s, string t) {
int m = s.size(), n = t.size();
vector<vector<long long>> dp(m + 1, vector<long long>(n + 1, 0));
for (int i = 0; i <= m; ++i) {
dp[i][0] = 1;
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i < j) {
continue;
}
if (s[i] == t[j]) {
dp[i + 1][j + 1] = dp[i][j] + dp[i][j + 1];
} else {
dp[i + 1][j + 1] = dp[i][j + 1];
}
}
}
return dp[m][n];
}
};
#endif //CPP_0115__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0098-Validate-Binary-Search-Tree/cpp_0098/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2019/11/1.
//
#ifndef CPP_0098_SOLUTION1_H
#define CPP_0098_SOLUTION1_H
#include <iostream>
#include <vector>
#include "TreeNode.h"
using namespace std;
/**
* 对于二叉搜索树, 中序遍历是有序的
*/
class Solution {
private:
void help(TreeNode *node, vector<int> &vec) {
if (node == NULL) {
return;
}
help(node->left, vec);
vec.push_back(node->val);
help(node->right, vec);
}
public:
bool isValidBST(TreeNode *root) {
vector<int> vec;
help(root, vec);
for (int i = 0; !vec.empty() && i < vec.size() - 1; ++i) {
if (vec[i] >= vec[i + 1]) {
return false;
}
}
return true;
}
};
#endif //CPP_0098_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0078-Subsets/cpp_0078/Solution1.h | //
// Created by ooooo on 2020/2/14.
//
#ifndef CPP_0078__SOLUTION1_H_
#define CPP_0078__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 回溯
*/
class Solution {
public:
void dfs(int cur_count, int count, int index) {
if (cur_count == count) {
ans.push_back(num);
return;
}
for (int i = index; i < nums.size(); ++i) {
num.push_back(nums[i]);
dfs(cur_count + 1, count, i + 1);
num.pop_back();
}
}
vector<vector<int>> ans;
vector<int> nums, num;
vector<vector<int>> subsets(vector<int> &nums) {
this->nums = nums;
for (int i = 0; i <= nums.size(); ++i) {
num.clear();
dfs(0, i, 0);
}
return ans;
}
};
#endif //CPP_0078__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0852-Peak-Index-in-a-Mountain-Array/cpp_0852/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/1/22.
//
#ifndef CPP_0852__SOLUTION1_H_
#define CPP_0852__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int peakIndexInMountainArray(vector<int> &A) {
// left和right 就是要搜索的区间
int left = 1, right = A.size() - 2;
while (left < right) {
int mid = left + (right - left) / 2;
// 必须要缩小的区间
if (A[mid - 1] < A[mid] && A[mid] < A[mid + 1]) {
left = mid + 1;
} else if (A[mid - 1] > A[mid] && A[mid] > A[mid - 1]) {
right = mid - 1;
} else {
// 逼近。。
right = mid;
}
}
return left;
}
};
#endif //CPP_0852__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_003/cpp_003/Solution2.h | <filename>lcof_003/cpp_003/Solution2.h<gh_stars>10-100
//
// Created by ooooo on 2020/3/5.
//
#ifndef CPP_003__SOLUTION2_H_
#define CPP_003__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* sort
*/
class Solution {
public:
int findRepeatNumber(vector<int> &nums) {
sort(nums.begin(), nums.end());
for (int i = 0, len = nums.size() - 1; i < len; ++i) {
if (nums[i] == nums[i + 1]) return nums[i];
}
return -1;
}
};
#endif //CPP_003__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0033-Search-in-Rotated-Sorted-Array/cpp_0033/Solution2.h | //
// Created by ooooo on 2020/2/9.
//
#ifndef CPP_0033__SOLUTION2_H_
#define CPP_0033__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 二分法 (两次, 第一次查找分隔点,第二次查找目标值) ???
*/
class Solution {
public:
int search(vector<int> &nums, int target) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = (left + right) / 2;
if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))
left = mid + 1;
else
right = mid;
}
return left == right && nums[left] == target ? left : -1;
}
};
#endif //CPP_0033__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0685-Redundant-Connection-II/cpp_0685/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/9/17 18:47
*/
#ifndef CPP_0685__SOLUTION1_H_
#define CPP_0685__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution1 {
public:
int find(vector<int> uf, int i) {
while (uf[i] != i) {
i = uf[i];
}
return i;
}
vector<int> findRedundantDirectedConnection(vector<vector<int>> edges) {
int n = edges.size();
vector<int> uf(n + 1);
vector<int> p(n + 1);
for (int i = 1; i < n + 1; i++) {
uf[i] = p[i] = i;
}
int conflict = -1, cyclic = -1;
for (int i = 0; i < edges.size(); ++i) {
vector<int> edge = edges[i];
int u = edge[0], v = edge[1];
if (p[v] != v) {
conflict = i;
} else {
p[v] = u;
int up = find(uf, u), vp = find(uf, v);
if (vp == up) {
cyclic = i;
} else {
uf[vp] = up;
}
}
}
if (conflict == -1) {
return edges[cyclic];
} else {
auto x = edges[conflict];
return cyclic == -1 ? x : vector<int>{p[x[1]], x[1]};
}
}
};
#endif //CPP_0685__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0877-Stone Game/cpp_0877/Solution1.h | //
// Created by ooooo on 6/16/2021.
//
#ifndef CPP_0877_SOLUTION1_H
#define CPP_0877_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool stoneGame(vector<int> &piles) {
int n = piles.size();
// dp[i][j] 表示 i 到 j 之间的差值
vector<vector<int>> dp(n, vector<int>(n));
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
int a = piles[i] - (i + 1 >= n ? 0 : dp[i + 1][j]);
int b = piles[j] - (j - 1 < 0 ? 0 : dp[i][j - 1]);
dp[i][j] = max(a, b);
}
}
return dp[0][n - 1] > 0;
}
};
#endif //CPP_0877_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0139-Word-Break/cpp_0139/Solution1.h | <filename>0139-Word-Break/cpp_0139/Solution1.h
//
// Created by ooooo on 2020/2/17.
//
#ifndef CPP_0139__SOLUTION1_H_
#define CPP_0139__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
using namespace std;
/**
* 超时
*/
class Solution {
public:
unordered_set<string> word_set;
set<int> count_set;
bool help(string s) {
if (s.empty()) return true;
for (auto &count: count_set) {
string s1 = s.substr(0, count);
if (word_set.count(s1) && help(s.substr(count, s.size() - count))) return true;
}
return false;
}
bool wordBreak(string s, vector<string> &wordDict) {
for (auto &word: wordDict) {
word_set.insert(word);
count_set.insert(word.size());
}
return help(s);
}
};
#endif //CPP_0139__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1769-Minimum Number of Operations to Move All Balls to Each Box/cpp_1769/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/28 13:21
*/
#ifndef CPP_1769__SOLUTION1_H_
#define CPP_1769__SOLUTION1_H_
#include <vector>
#include <iostream>
using namespace std;
class Solution {
vector<int> minOperations(string boxes) {
int n = boxes.size();
vector<int> left(n, 0), right(n, 0);
vector<int> leftCount(n, 0), rightCount(n, 0);
leftCount[0] = boxes[0] - '0';
rightCount[n - 1] = boxes[n - 1] - '0';
for (int i = 1; i < n; ++i) {
leftCount[i] = leftCount[i - 1] + boxes[i] - '0';
left[i] = left[i - 1] + leftCount[i - 1];
}
for (int i = n - 2; i >= 0; --i) {
rightCount[i] = rightCount[i + 1] + boxes[i] - '0';
right[i] = right[i + 1] + rightCount[i + 1];
}
vector<int> ans(n, 0);
for (int i = 0; i < n; ++i) {
ans[i] = left[i] + right[i];
}
return ans;
}
};
#endif //CPP_1769__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_010-1/cpp_010-1/Solution3.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/3/8.
//
#ifndef CPP_010_1__SOLUTION3_H_
#define CPP_010_1__SOLUTION3_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* F(N) = F(N - 1) + F(N - 2)
*
* dp
*
* O(n)
*/
class Solution {
public:
/*int fib(int n) {
if (n <= 1) return n;
vector<long long> dp(n + 1);
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007;
}
return dp[n];
}*/
int fib(int n) {
if (n <= 1) return n;
int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; ++i) {
c = (a + b) % 1000000007;
a = b;
b = c;
}
return c;
}
};
#endif //CPP_010_1__SOLUTION3_H_
|
ooooo-youwillsee/leetcode | 0064-Minimum-Path-Sum/cpp_0064/Solution2.h | //
// Created by ooooo on 2020/2/13.
//
#ifndef CPP_0064__SOLUTION2_H_
#define CPP_0064__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 暴力 dfs
*/
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) {
min_sum = min(min_sum, sum);
}
dfs(i + 1, j, sum);
dfs(i, j + 1, sum);
}
int min_sum = INT_MAX, m, n;
vector<vector<int>> grid;
int minPathSum(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 min_sum;
}
};
#endif //CPP_0064__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | lcof_053-2/cpp_053-2/Solution1.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2020/4/10.
//
#ifndef CPP_053_2__SOLUTION1_H_
#define CPP_053_2__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int missingNumber(vector<int> &nums) {
vector<bool> marked(nums.size() + 1, false);
for (auto &num : nums) marked[num] = true;
for (int i = 0, len = marked.size(); i < len; ++i) {
if (!marked[i]) return i;
}
return -1;
}
};
#endif //CPP_053_2__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0148-Sort-List/cpp_0148/Solution1.h | //
// Created by ooooo on 2020/2/18.
//
#ifndef CPP_0148__SOLUTION1_H_
#define CPP_0148__SOLUTION1_H_
#include "ListNode.h"
/**
* 复杂度要求 O(nlogn) ==> 归并排序
* recursion
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (!head || !head->next) return head;
ListNode *low = head, *fast = head->next;
while (fast && fast->next) {
low = low->next;
fast = fast->next->next;
}
ListNode *mid = low->next;
low->next = nullptr;
ListNode *left = sortList(head), *right = sortList(mid);
ListNode *dummyHead = new ListNode(-1), *cur = dummyHead;
while (left && right) {
if (left->val <= right->val) {
cur->next = left;
left = left->next;
} else {
cur->next = right;
right = right->next;
}
cur = cur->next;
}
cur->next = left ? left : right;
return dummyHead->next;
}
};
#endif //CPP_0148__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1656-Design an Ordered Stream/cpp_1656/Solution1.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2021/1/24 17:58
*/
#ifndef CPP_1656__SOLUTION1_H_
#define CPP_1656__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
using namespace std;
class OrderedStream {
public:
int ptr;
vector<string> stream;
OrderedStream(int n) {
this->ptr = 1;
stream.assign(n + 1, "");
}
vector<string> insert(int id, string value) {
stream[id] = value;
if (stream[ptr] != "") {
vector<string> ans;
for (int i = ptr; i < stream.size(); ++i) {
if (stream[i] == "") {
break;
}
ans.emplace_back(stream[i]);
ptr = i + 1;
}
return ans;
}
return {};
}
};
#endif //CPP_1656__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0525-Contiguous Array/cpp_0525/Solution1.h | <filename>0525-Contiguous Array/cpp_0525/Solution1.h
/**
* @author ooooo
* @date 2021/6/3 10:41
*/
#ifndef CPP_0525__SOLUTION1_H_
#define CPP_0525__SOLUTION1_H_
#include <vector>
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
// sum[i] 表示 0 到 i 的前缀和
// sum[j] - sum[i] = (j -i) / 2
// 2 * sum[j] - j = 2 * sum[i] - i && (j - i) % 2 == 0
int findMaxLength(vector<int> &nums) {
int n = nums.size();
if (n < 2) return 0;
int ans = 0;
unordered_map<int, int> m;
// 依据 [0,1] 推导这个初始值
m[1] = -1;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += nums[i];
int a = 2 * sum - i;
if (m.find(a) != m.end() && (i - m[a]) % 2 == 0) {
ans = max(ans, i - m[a]);
} else {
// 如果不存在,就加入,如果存在,只保留最前面的
m[a] = i;
}
}
return ans;
}
};
#endif //CPP_0525__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1686-Stone Game VI/cpp_1686/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/4/11 16:46
*/
#ifndef CPP_1686__SOLUTION1_H_
#define CPP_1686__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int stoneGameVI(vector<int> &aliceValues, vector<int> &bobValues) {
int n = aliceValues.size();
vector<int> nums(n);
for (int i = 0; i < n; i++) {
nums[i] = i;
}
sort(nums.begin(), nums.end(), [&](const int x, const int y) {
return aliceValues[x] + bobValues[x] > aliceValues[y] + bobValues[y];
});
int a = 0;
for (int i = 0; i < n; i += 2) {
a += aliceValues[nums[i]];
}
int b = 0;
for (int i = 1; i < n; i += 2) {
b += bobValues[nums[i]];
}
if (a > b) return 1;
if (a == b) return 0;
return -1;
}
};
#endif //CPP_1686__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1022-Sum-of-Root-To-Leaf-Binary-Numbers/cpp_1022/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/1/5.
//
#ifndef CPP_1022_SOLUTION1_H
#define CPP_1022_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
class Solution {
public:
vector<string> nums;
void dfs(TreeNode *node, string s) {
if (!node) return;
s += to_string(node->val);
if (!node->left && !node->right) {
nums.push_back(s);
return;
}
dfs(node->left, s);
dfs(node->right, s);
}
int sumRootToLeaf(TreeNode *root) {
dfs(root, "");
int sum = 0;
for (auto num: nums) {
sum += stoi(num, 0, 2);
}
return sum;
}
};
#endif //CPP_1022_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0394-Decode-String/cpp_0394/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/27.
//
#ifndef CPP_0394__SOLUTION1_H_
#define CPP_0394__SOLUTION1_H_
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;
/**
* stack
*/
class Solution {
public:
string decodeString(string s) {
stack<char> stack;
string ans = "", tmp = "";
for (auto &c: s) {
if (c != ']') {
stack.push(c);
} else {
tmp = "";
// 获取重复的字母
while (!stack.empty() && stack.top() != '[') {
tmp.push_back(stack.top());
stack.pop();
}
stack.pop(); // 弹出'['
string num = "", str = tmp;
// 获取要重复的数字
while (!stack.empty() && isdigit(stack.top())) {
num.push_back(stack.top());
stack.pop();
}
tmp = "";
reverse(num.begin(), num.end());
for (int i = 0, size = stoi(num); i < size; ++i) {
tmp += str;
}
if (stack.empty()) {
string ss;
reverse_copy(tmp.begin(), tmp.end(), back_inserter(ss));
ans += ss;
} else {
for (int i = tmp.size() - 1; i >= 0; --i) {
stack.push(tmp[i]);
}
}
}
}
tmp = "";
while (!stack.empty()) {
tmp.push_back(stack.top());
stack.pop();
}
reverse(tmp.begin(), tmp.end());
return ans + tmp;
}
};
#endif //CPP_0394__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0121-Best-Time-to-Buy-and-Sell-Stock/cpp_0121/Solution2.h | //
// Created by ooooo on 2020/2/5.
//
#ifndef CPP_0121__SOLUTION2_H_
#define CPP_0121__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 动态规划
*
* i: 第几天, k:第几次交易
* dp[i][k][0] = max(dp[i-1][k][1] + p , dp[i-1][k][0])
* dp[i][k][1] = max(dp[i-1][k-1][0] - p , dp[i-1][k][1])
*/
class Solution {
public:
int maxProfit(vector<int> &prices) {
int ans = 0;
vector<vector<vector<int>>> dp(prices.size() + 1, vector<vector<int>>(2, vector<int>(2, 0)));
dp[0][1][1] = INT_MIN;
for (int i = 0; i < prices.size() + 1; ++i) {
dp[i][0][1] = INT_MIN;
}
for (int j = 0; j < prices.size(); ++j) {
int i = j + 1, p = prices[j];
for (int k = 1; k <= 1; ++k) {
dp[i][k][0] = max(dp[i - 1][k][1] + p, dp[i - 1][k][0]);
dp[i][k][1] = max(dp[i - 1][k - 1][0] - p, dp[i - 1][k][1]);
ans = max(dp[i][k][0], ans);
}
}
return dp[prices.size()][1][0];
}
};
#endif //CPP_0121__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1310-XOR Queries of a Subarray/cpp_1310/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/5/12 19:44
*/
#ifndef CPP_1310__SOLUTION1_H_
#define CPP_1310__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
struct SegmentTree {
vector<int> tree;
vector<int> arr;
SegmentTree(vector<int> arr) {
tree.assign(4 * arr.size(), 0);
this->arr = arr;
buildTree(0, 0, arr.size() - 1);
}
void buildTree(int root, int l, int r) {
if (l > r) return;
if (l == r) {
tree[root] = arr[l];
return;
}
int mid = l + (r - l) / 2;
buildTree(lc(root), l, mid);
buildTree(rc(root), mid + 1, r);
tree[root] = merge(tree[lc(root)], tree[rc(root)]);
}
int query(int ql, int qr) {
return query(0, 0, arr.size() - 1, ql, qr);
}
int query(int root, int l, int r, int ql, int qr) {
if (ql > qr) return 0;
if (l == ql && r == qr) {
return tree[root];
}
int mid = l + (r - l) / 2;
if (qr <= mid) {
return query(lc(root), l, mid, ql, qr);
} else if (mid + 1 <= ql) {
return query(rc(root), mid + 1, r, ql, qr);
}
int lv = query(lc(root), l, mid, ql, mid);
int rv = query(rc(root), mid + 1, r, mid + 1, qr);
return merge(lv, rv);
}
int merge(int x, int y) {
return x ^ y;
}
int lc(int x) {
return 2 * x + 1;
}
int rc(int x) {
return 2 * x + 2;
}
};
vector<int> xorQueries(vector<int> &arr, vector<vector<int>> &queries) {
vector<int> ans;
SegmentTree t(arr);
for (auto &q: queries) {
ans.push_back(t.query(q[0], q[1]));
}
return ans;
}
};
#endif //CPP_1310__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0114-Flatten-Binary-Tree-to-Linked-List/cpp_0114/Solution2.h | <gh_stars>10-100
//
// Created by ooooo on 2020/2/16.
//
#ifndef CPP_0114__SOLUTION2_H_
#define CPP_0114__SOLUTION2_H_
#include "TreeNode.h"
class Solution {
public:
TreeNode *prev = nullptr;
void flatten(TreeNode *root) {
if (!root) return;
flatten(root->right);
flatten(root->left);
root->right = prev;
root->left = nullptr;
prev = root;
}
};
#endif //CPP_0114__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0073-Set Matrix Zeroes/cpp_0073/Solution2.h | /**
* @author ooooo
* @date 2020/10/11 20:13
*/
#ifndef CPP_0073__SOLUTION2_H_
#define CPP_0073__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
// 用第一行和第一列存储
class Solution {
public:
void setZeroes(vector<vector<int>> &matrix) {
if (matrix.empty()) return;
int m = matrix.size(), n = matrix[0].size();
bool first_row_0 = false, first_col_0 = false;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 0) {
if (i == 0) {
first_row_0 = true;
}
if (j == 0) {
first_col_0 = true;
}
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
for (int i = 0; first_col_0 && i < m; ++i) {
matrix[i][0] = 0;
}
for (int i = 0; first_row_0 && i < n; ++i) {
matrix[0][i] = 0;
}
}
};
#endif //CPP_0073__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0051-N-Queens/cpp_0051/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2019/11/14.
//
#ifndef CPP_0051_SOLUTION1_H
#define CPP_0051_SOLUTION1_H
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
private:
vector<int> result;
void solve(vector<vector<string>> &res,
vector<string> &tmp, vector<bool> &cols_,
vector<bool> &diag1s_,
vector<bool> &diag2s_, int n, int row) {
if (row == n) {
res.push_back(tmp);
return;
}
for (auto col = 0; col < n; col++) {
int ll = row + col;
// 防止越界
int rr = row - col + n - 1;
if (cols_[col] && diag1s_[ll] && diag2s_[rr]) {
tmp[row][col] = 'Q';
cols_[col] = false;
diag1s_[ll] = false;
diag2s_[rr] = false;
solve(res, tmp, cols_, diag1s_, diag2s_, n, row + 1);
tmp[row][col] = '.';
cols_[col] = true;
diag1s_[ll] = true;
diag2s_[rr] = true;
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> tmp(n, string(n, '.'));
vector<bool> cols_(n, true);
vector<bool> diag1s_(2 * n - 1, true);
vector<bool> diag2s_(2 * n - 1, true);
solve(res, tmp, cols_, diag1s_, diag2s_, n, 0);
return res;
}
};
#endif //CPP_0051_SOLUTION1_H
|
ooooo-youwillsee/leetcode | lcci_17_12/cpp_17_12/Solution1.h | /**
* @author ooooo
* @date 2021/4/2 18:20
*/
#ifndef CPP_17_12__SOLUTION1_H_
#define CPP_17_12__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int trap(vector<int> &height) {
int n = height.size();
int ans = 0;
for (int i = 0; i < n; i++) {
int l = 0, r = 0;
for (int j = i - 1; j >= 0; j--) {
l = max(l, height[j]);
}
for (int j = i + 1; j < n; j++) {
r = max(r, height[j]);
}
int h = min(l, r);
if (h > height[i]) {
ans += h - height[i];
}
}
return ans;
}
};
#endif //CPP_17_12__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0107-Binary-Tree-Level-Order-Traversal-II/cpp_0107/Solution2.h | <reponame>ooooo-youwillsee/leetcode
/**
* @author ooooo
* @date 2020/9/6 01:06
*/
#ifndef CPP_0107__SOLUTION2_H_
#define CPP_0107__SOLUTION2_H_
#include <iostream>
#include <vector>
#include "TreeNode.h"
#include <queue>
using namespace std;
class Solution2 {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
if(!root) return {};
vector<vector<int>> ans;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
ans.push_back({});
for (int i = 0, sz = q.size(); i < sz; ++i) {
TreeNode *node = q.front();
q.pop();
ans[ans.size() - 1].push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
#endif //CPP_0107__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1758-Minimum Changes To Make Alternating Binary String/cpp_1758/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2021/2/21 19:05
*/
#ifndef CPP_1758__SOLUTION1_H_
#define CPP_1758__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int help(string s) {
int count = 0;
for (int i = 1; i < s.size(); ++i) {
if (s[i] == s[i - 1]) {
s[i] = s[i - 1] == '0' ? '1' : '0';
count++;
}
}
return count;
}
int minOperations(string s) {
int ans = 0;
if (s[0] == '0') {
ans = min(help(s), help('0' + s));
} else {
ans = min(help(s), help('1' + s));
}
return ans;
}
};
#endif //CPP_1758__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0628-Maximum Product of Three Numbers/cpp_0628/Solution1.h | /**
* @author ooooo
* @date 2021/1/20 12:08
*/
#ifndef CPP_0628__SOLUTION1_H_
#define CPP_0628__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maximumProduct(vector<int> &nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
return max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]);
}
};
#endif //CPP_0628__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_052/cpp_052/Solution2.h | <filename>lcof_052/cpp_052/Solution2.h
//
// Created by ooooo on 2020/4/9.
//
#ifndef CPP_052__SOLUTION2_H_
#define CPP_052__SOLUTION2_H_
#include "ListNode.h"
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *nodeA = headA, *nodeB = headB;
while (nodeA && nodeB) {
nodeA = nodeA->next;
nodeB = nodeB->next;
}
if (!nodeA) {
nodeA = headB;
}
if (!nodeB) {
nodeB = headA;
}
while (nodeA && nodeB) {
nodeA = nodeA->next;
nodeB = nodeB->next;
}
if (!nodeA) {
nodeA = headB;
}
if (!nodeB) {
nodeB = headA;
}
while (nodeA && nodeB) {
if (nodeA == nodeB) return nodeA;
nodeA = nodeA->next;
nodeB = nodeB->next;
}
return nullptr;
}
};
#endif //CPP_052__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0026-Remove-Duplicates-from-Sorted-Array/cpp_0026/Solution1.h | //
// Created by ooooo on 2020/1/5.
//
#ifndef CPP_0026_SOLUTION1_H
#define CPP_0026_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int removeDuplicates(vector<int> &nums) {
if (nums.size() <= 1) return nums.size();
int i = 0;
for (int j = 1; j < nums.size(); ++j) {
if (nums[j] == nums[i]) continue;
i++;
nums[i] = nums[j];
}
return i + 1;
}
};
#endif //CPP_0026_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0066-Plus-One/cpp_0066/Solution1.h | //
// Created by ooooo on 2020/1/6.
//
#ifndef CPP_0066_SOLUTION1_H
#define CPP_0066_SOLUTION1_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
if (digits.empty()) return {};
for (int i = digits.size() - 1; i >= 0; --i) {
digits[i] = digits[i] + 1;
if (digits[i] == 10) {
digits[i] = 0;
} else {
return digits;
}
}
//digits.insert(digits.begin(), 1);
vector<int> ans(digits.size() + 1, 0);
ans[0] = 1;
return ans;
}
};
#endif //CPP_0066_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0104-Maximum-Depth-of-Binary-Tree/cpp_0104/Solution2.h | //
// Created by ooooo on 2019/11/4.
//
#ifndef CPP_0104_SOLUTION2_H
#define CPP_0104_SOLUTION2_H
#include "TreeNode.h"
using namespace std;
class Solution {
public:
int maxDepth(TreeNode *root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
#endif //CPP_0104_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0127-Word-Ladder/cpp_0127/Solution1.h | //
// Created by ooooo on 2019/12/30.
//
#ifndef CPP_0127_SOLUTION1_H
#define CPP_0127_SOLUTION1_H
#include <iostream>
#include <string>
#include <climits>
#include <vector>
#include <unordered_map>
using namespace std;
/**
* note: 超时
*/
class Solution {
public:
int min = INT_MAX;
// 加速计算
unordered_map<string, bool> map;
bool bestMatch(string currentWord, string beginWord) {
if (this->map.find(currentWord + beginWord) != this->map.end()) {
return this->map[currentWord + beginWord];
} else if (this->map.find(beginWord + currentWord) != this->map.end()) {
return this->map[beginWord + currentWord];
}
bool flag = false;
for (int j = 0; j < currentWord.size(); ++j) {
if (currentWord[j] == beginWord[j]) continue;
if (flag) return false;
flag = true;
}
this->map[currentWord + beginWord] = this->map[beginWord + currentWord] = flag;
return flag;
}
vector<int> findBestMatch(string beginWord, string endWord, vector<string> wordList, vector<bool> &matched) {
vector<int> res;
for (int i = 0; i < wordList.size(); ++i) {
if (matched[i]) continue;
string currentWord = wordList[i];
if (bestMatch(currentWord, beginWord)) {
if (currentWord == endWord) {
res.insert(res.begin(), i);
} else {
res.push_back(i);
}
}
}
return res;
}
void dfs(string beginWord, string endWord, vector<string> wordList, vector<bool> &matched, int count) {
if (beginWord == endWord) {
min = min < count ? min : count;
return;
}
vector<int> indexList = findBestMatch(beginWord, endWord, wordList, matched);
for (int i = 0; i < indexList.size(); ++i) {
matched[indexList[i]] = true;
dfs(wordList[indexList[i]], endWord, wordList, matched, count + 1);
matched[indexList[i]] = false;
}
}
int ladderLength(string beginWord, string endWord, vector<string> &wordList) {
vector<bool> matched(wordList.size(), false);
dfs(beginWord, endWord, wordList, matched, 1);
return min == INT_MAX ? 0 : min;
}
};
#endif //CPP_0127_SOLUTION1_H
|
ooooo-youwillsee/leetcode | lcof_015/cpp_015/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2020/3/12.
//
#ifndef CPP_015__SOLUTION1_H_
#define CPP_015__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
int hammingWeight(uint32_t n) {
int ans = 0;
while (n) {
ans += 1;
n &= (n - 1);
}
return ans;
}
};
#endif //CPP_015__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1738-Find Kth Largest XOR Coordinate Value/cpp_1738/Solution1.h | /**
* @author ooooo
* @date 2021/2/28 13:14
*/
#ifndef CPP_1738__SOLUTION1_H_
#define CPP_1738__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 kthLargestValue(vector<vector<int>> &matrix, int k) {
int m = matrix.size(), n = matrix[0].size();
vector<int> ans;
vector<vector<int>> up(m + 1, vector<int>(n + 1)), dp(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0) {
up[i + 1][j + 1] = matrix[i][j];
} else {
up[i + 1][j + 1] = matrix[i][j] ^ up[i][j + 1];
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
dp[i + 1][j + 1] = dp[i + 1][j] ^ up[i][j + 1] ^ matrix[i][j];
ans.push_back(dp[i + 1][j + 1]);
}
}
sort(ans.begin(), ans.end());
return ans[ans.size() - k];
}
};
#endif //CPP_1738__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_006/cpp_006/ListNode.h | //
// Created by ooooo on 2020/3/7.
//
#ifndef CPP_006__LISTNODE_H_
#define CPP_006__LISTNODE_H_
#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
ListNode(vector<int> nums) {
if (nums.empty()) return;
this->val = nums[0];
this->next = nullptr;
ListNode *cur = this;
for (int i = 1, len = nums.size(); i < len; ++i) {
cur->next = new ListNode(nums[i]);
cur = cur->next;
}
}
};
#endif //CPP_006__LISTNODE_H_
|
ooooo-youwillsee/leetcode | 1869-Longer Contiguous Segments of Ones than Zeros/cpp_1869/Solution1.h | <filename>1869-Longer Contiguous Segments of Ones than Zeros/cpp_1869/Solution1.h
/**
* @author ooooo
* @date 2021/5/25 22:20
*/
#ifndef CPP_1869__SOLUTION1_H_
#define CPP_1869__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int help(string &s, char c) {
int cnt = 0;
int max_cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == c) {
cnt++;
} else {
max_cnt = max(max_cnt, cnt);
cnt = 0;
}
}
return max(max_cnt, cnt);
}
bool checkZeroOnes(string s) {
return help(s, '1') > help(s, '0');
}
};
#endif //CPP_1869__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp_0235/Solution1.h | //
// Created by ooooo on 2019/11/3.
//
#ifndef CPP_0235_SOLUTION1_H
#define CPP_0235_SOLUTION1_H
#include "TreeNode.h"
#include <queue>
class Solution {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
if (!root) return NULL;
if (root == p || root == q) return root;
if (p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if (p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
return root;
}
};
#endif //CPP_0235_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0048-Rotate-Image/cpp_0048/Solution1.h | //
// Created by ooooo on 2020/2/11.
//
#ifndef CPP_0048__SOLUTION1_H_
#define CPP_0048__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void rotate(vector<vector<int>> &matrix) {
int n = matrix.size();
vector<vector<int>> data(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
data[i][j] = matrix[i][j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
matrix[j][n - 1 - i] = data[i][j];
}
}
}
};
#endif //CPP_0048__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1578-Minimum-Deletion-Cost-to-Avoid-Repeating-Letters/cpp_1578/Solution1.h | /**
* @author ooooo
* @date 2020/9/7 16:00
*/
#ifndef CPP_1578__SOLUTION1_H_
#define CPP_1578__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
using namespace std;
class Solution {
public:
int minCost(string s, vector<int> &cost) {
int i = -1, ans = 0;
s += '$';
for (int j = 0; j < s.size() - 1; ++j) {
if (s[j] == s[j + 1]) {
if (i == -1) i = j;
} else {
if (i == -1) continue;
sort(cost.begin() + i, cost.begin() + j + 1);
ans += accumulate(cost.begin() + i, cost.begin() + j, 0);
i = -1;
}
}
return ans;
}
};
#endif //CPP_1578__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_044/cpp_044/Solution1.h | <gh_stars>10-100
//
// Created by ooooo on 2020/3/29.
//
#ifndef CPP_044__SOLUTION1_H_
#define CPP_044__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class Solution {
public:
long countNums(int digit) {
if (digit == 1) return 10;
return 9 * powl(10, digit - 1);
}
int findNthDigit(int n) {
int digit = 1;
while (true) {
long nums = countNums(digit);
if (n < nums * digit) return getDigit(n, digit);
n -= nums * digit;
digit++;
}
}
int getDigit(int n, int digit) {
if (digit == 1) return n;
int base = (int) pow(10, digit - 1) + n / digit;
return to_string(base)[n % digit] - '0';
}
};
#endif //CPP_044__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1733-Minimum Number of People to Teach/cpp_1733/Solution1.h | /**
* @author ooooo
* @date 2021/3/8 20:04
*/
#ifndef CPP_1733__SOLUTION1_H_
#define CPP_1733__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minimumTeachings(int n, vector<vector<int>> &languages, vector<vector<int>> &friendships) {
int pNum = languages.size();
vector<vector<bool>> aux(pNum + 1, vector<bool>(n + 1));
for (int k = 0; k < languages.size(); ++k) {
for (auto l: languages[k]) {
aux[k + 1][l] = true;
}
}
vector<bool> p(pNum + 1);
for (int j = 0; j < friendships.size(); ++j) {
int u = friendships[j][0], v = friendships[j][1];
// 判断有没有公共语言
bool common = false;
for (int i = 1; i <= n; ++i) {
if (aux[u][i] && aux[v][i]) {
common = true;
break;
}
}
if (!common) {
// 说明u和v需要被教语言
p[u] = true;
p[v] = true;
}
}
int cnt = 0;
int total = 0;
vector<int> lan(n + 1);
for (int i = 1; i < p.size(); ++i) {
if (p[i]) {
for (auto j : languages[i - 1]) {
lan[j]++;
cnt = max(cnt, lan[j]);
}
total++;
}
}
return total - cnt;
}
};
#endif //CPP_1733__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0103-Binary-Tree-Zigzag-Level-Order-Traversal/cpp_0103/Solution1.h | <reponame>ooooo-youwillsee/leetcode
//
// Created by ooooo on 2019/12/29.
//
#ifndef CPP_0103_SOLUTION1_H
#define CPP_0103_SOLUTION1_H
#include "TreeNode.h"
#include <vector>
#include <queue>
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
if (!root) return {};
queue<TreeNode *> q;
q.push(root);
vector<vector<int>> res;
bool reverse = false;
while (!q.empty()) {
vector<int> vec;
for (int i = 0, len = q.size(); i < len; ++i) {
TreeNode *node = q.front();
q.pop();
if (reverse) {
vec.insert(begin(vec), node->val);
} else {
vec.push_back(node->val);
}
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
}
reverse = !reverse;
res.push_back(vec);
}
return res;
}
};
#endif //CPP_0103_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0404-Sum-of-Left-Leaves/cpp_0404/Solution2.h | /**
* @author ooooo
* @date 2020/9/19 13:38
*/
#ifndef CPP_0404__SOLUTION2_H_
#define CPP_0404__SOLUTION2_H_
#include "TreeNode.h"
class Solution {
public:
int sumOfLeftLeaves(TreeNode *root, bool is_left) {
if (!root) return 0;
if (is_left && !root->left && !root->right) return root->val;
return sumOfLeftLeaves(root->left, true) + sumOfLeftLeaves(root->right, false);
}
int sumOfLeftLeaves(TreeNode *root) {
return sumOfLeftLeaves(root, false);
}
};
#endif //CPP_0404__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0035-Search-Insert-Position/cpp_0035/Solution2.h | <reponame>ooooo-youwillsee/leetcode<gh_stars>10-100
//
// Created by ooooo on 2020/1/6.
//
#ifndef CPP_0035_SOLUTION2_H
#define CPP_0035_SOLUTION2_H
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int searchInsert(vector<int> &nums, int target) {
if (nums.empty()) return 0;
int left = 0, right = nums.size() - 1;
int ans = -1;
while (true) {
int mid = left + (right - left + 1) / 2;
if (left > right || nums[mid] == target) {
ans = mid;
break;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
};
#endif //CPP_0035_SOLUTION2_H
|
ooooo-youwillsee/leetcode | 0917-Reverse-Only-Letters/cpp_0917/Solution2.h | //
// Created by ooooo on 2020/2/1.
//
#ifndef CPP_0917__SOLUTION2_H_
#define CPP_0917__SOLUTION2_H_
#include <iostream>
using namespace std;
/**
* 存入数组,倒序填入原字符串
*/
class Solution {
public:
string reverseOnlyLetters(string S) {
if (S.size() <= 1) return S;
int *arr = new int[S.size()], count = 0;
// 存入所有的字符
for (int i = 0; i < S.size(); ++i) {
if (isalpha(S[i])) {
arr[count++] = S[i];
}
}
// 倒序插入字符
for (int i = 0; i < S.size(); ++i) {
if (isalpha(S[i])) {
S[i] = arr[--count];
}
}
return S;
}
};
#endif //CPP_0917__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 1071-Greatest-Common-Divisor-of-Strings/cpp_1071/Solution1.h | //
// Created by ooooo on 2020/2/2.
//
#ifndef CPP_1071__SOLUTION1_H_
#define CPP_1071__SOLUTION1_H_
#include <iostream>
using namespace std;
/**
* 暴力法
*/
class Solution {
public:
// a % b
bool div(string a, string b) {
if (a.size() % b.size() != 0) return false;
for (int i = 0; i < a.size(); i += b.size()) {
if (a.substr(i, b.size()) != b) return false;
}
return true;
}
string gcdOfStrings(string str1, string str2) {
string min_str = "", max_str = "";
if (str1.size() < str2.size()) {
min_str = str1;
max_str = str2;
} else {
min_str = str2;
max_str = str1;
}
if (max_str.find(min_str) == -1) return "";
for (int i = min_str.size(); i > 0; i--) {
string target_str = min_str.substr(0, i);
if (div(min_str, target_str) && div(max_str, target_str)) return target_str;
}
return "";
}
};
#endif //CPP_1071__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1672-Richest Customer Wealth/cpp_1672/Solution1.h | <filename>1672-Richest Customer Wealth/cpp_1672/Solution1.h
/**
* @author ooooo
* @date 2020/12/11 11:16
*/
#ifndef CPP_1672__SOLUTION1_H_
#define CPP_1672__SOLUTION1_H_
#include <iostream>
#include <vector>
#include <numeric>
#include <unordered_map>
using namespace std;
class Solution {
public:
int maximumWealth(vector<vector<int>> &a) {
int ans = 0;
for (int i = 0; i < a.size(); i++) {
ans = max(ans, accumulate(a[i].begin(), a[i].end(), 0));
}
return ans;
}
};
#endif //CPP_1672__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0228-Summary Ranges/cpp_0228/Solution1.h | <filename>0228-Summary Ranges/cpp_0228/Solution1.h
/**
* @author ooooo
* @date 2021/1/10 上午9:27
*/
#ifndef CPP_0228__SOLUTION1_H_
#define CPP_0228__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> summaryRanges(vector<int> &nums) {
if (nums.empty()) return {};
int prev = 0, n = nums.size();
vector<string> ans;
for (int j = 1; j < n; ++j) {
if (nums[j] == nums[j - 1] + 1) continue;
if (prev != j - 1) {
ans.emplace_back(to_string(nums[prev]) + "->" + to_string(nums[j - 1]));
} else {
ans.emplace_back(to_string(nums[prev]));
}
prev = j;
}
if (prev != n - 1) {
ans.emplace_back(to_string(nums[prev]) + "->" + to_string(nums[n - 1]));
} else {
ans.emplace_back(to_string(nums[prev]));
}
return ans;
}
};
#endif //CPP_0228__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0315-Count of Smaller Numbers After Self/cpp_0315/Solution1.h | <gh_stars>10-100
/**
* @author ooooo
* @date 2020/11/27 11:10
*/
#ifndef CPP_0315__SOLUTION1_H_
#define CPP_0315__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> countSmaller(vector<int> &nums) {
int n = nums.size();
vector<int> ans(n), insert;
for (int i = n - 1; i >= 0; --i) {
auto it = lower_bound(insert.begin(), insert.end(), nums[i]);
ans[i] = it - insert.begin();
insert.insert(insert.begin() + ans[i], nums[i]);
}
return ans;
}
};
#endif //CPP_0315__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 1442-Count Triplets That Can Form Two Arrays of Equal XOR/cpp_1442/Solution2.h | /**
* @author ooooo
* @date 2021/5/22 11:37
*/
#ifndef CPP_1442__SOLUTION2_H_
#define CPP_1442__SOLUTION2_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int countTriplets(vector<int> &arr) {
int n = arr.size();
vector<int> pre(n + 1);
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] ^ arr[i];
}
// pre[j] ^ pre[i] = pre[k+1] ^ pre[j]
int ans = 0;
for (int i = 0; i < n; i++) {
for (int k = i + 1; k < n; k++) {
if (pre[i] == pre[k + 1]) {
ans += (k - i);
}
}
}
return ans;
}
};
#endif //CPP_1442__SOLUTION2_H_
|
ooooo-youwillsee/leetcode | 0055-Jump-Game/cpp_0055/Solution1.h | //
// Created by ooooo on 2020/2/12.
//
#ifndef CPP_0055__SOLUTION1_H_
#define CPP_0055__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
/**
* 超时
*/
class Solution {
public:
bool canJump(vector<int> &nums) {
vector<bool> dp(nums.size(), false);
dp[0] = true;
for (int i = 0; i < nums.size(); ++i) {
if (!dp[i]) return false;
for (int j = 0; j <= nums[i]; ++j) {
if (i + j >= nums.size() - 1) return true;
dp[i + j] = true;
}
}
return false;
}
};
#endif //CPP_0055__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | 0232-Implement-Queue-using-Stacks/cpp_0232/Solution1.h | //
// Created by ooooo on 2019/10/30.
//
#ifndef CPP_0232_SOLUTION1_H
#define CPP_0232_SOLUTION1_H
#include <stack>
#include <iostream>
using namespace std;
class MyQueue {
private:
stack<int> in;
stack<int> out;
int front;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
if(in.empty()) {
front = x;
}
in.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
while (!in.empty()) {
out.push(in.top());
in.pop();
}
int res = out.top();
out.pop();
if (!out.empty()) {
front = out.top();
}
while (!out.empty()) {
in.push(out.top());
out.pop();
}
return res;
}
/** Get the front element. */
int peek() {
return front;
}
/** Returns whether the queue is empty. */
bool empty() {
return in.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
#endif //CPP_0232_SOLUTION1_H
|
ooooo-youwillsee/leetcode | 0559-Maximum Depth of N-ary Tree/cpp_0559/Node.h | //
// Created by ooooo on 2019/12/29.
//
#ifndef CPP_0599_NODE_H
#define CPP_0599_NODE_H
#include <istream>
#include <vector>
using namespace std;
class Node {
public:
int val;
vector<Node *> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node *> _children) {
val = _val;
children = _children;
}
};
#endif //CPP_0599_NODE_H
|
ooooo-youwillsee/leetcode | 0706-Design-HashMap/cpp_0706/Solution1.h | //
// Created by ooooo on 2020/1/14.
//
#ifndef CPP_0706__SOLUTION1_H_
#define CPP_0706__SOLUTION1_H_
class MyHashMap {
private:
struct Node {
int key;
int value;
Node *next;
Node(int key, int value, Node *next) : key(key), value(value), next(next) {}
Node() {
this->next = nullptr;
}
};
Node *dummyHead;
int size;
public:
/** Initialize your data structure here. */
MyHashMap() {
this->dummyHead = new Node();
this->size = 0;
}
bool contains(int key) {
Node *curNode = dummyHead->next;
while (curNode) {
if (curNode->key == key) return true;
curNode = curNode->next;
}
return false;
}
Node *getNode(int key) {
Node *curNode = dummyHead->next;
while (curNode) {
if (curNode->key == key) return curNode;
curNode = curNode->next;
}
return nullptr;
}
/** value will always be non-negative. */
void put(int key, int value) {
Node *node = getNode(key);
if (node) {
node->value = value;
} else {
dummyHead->next = new Node(key, value, dummyHead->next);
size++;
}
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
Node *node = getNode(key);
if (node) {
return node->value;
} else {
return -1;
}
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
bool find = false;
Node *prev = dummyHead;
while (prev->next) {
if (prev->next->key == key) {
find = true;
break;
}
prev = prev->next;
}
if (prev && find) {
Node *delNode = prev->next;
prev->next = delNode->next;
size--;
delNode->next = nullptr;
}
}
};
#endif //CPP_0706__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_058-2/cpp_058-2/Solution1.h | <filename>lcof_058-2/cpp_058-2/Solution1.h
//
// Created by ooooo on 2020/4/19.
//
#ifndef CPP_058_2__SOLUTION1_H_
#define CPP_058_2__SOLUTION1_H_
#include <iostream>
using namespace std;
class Solution {
public:
string reverseLeftWords(string s, int n) {
return s.substr(n, s.size() - n) + s.substr(0, n);
}
};
#endif //CPP_058_2__SOLUTION1_H_
|
ooooo-youwillsee/leetcode | lcof_029/cpp_029/Solution1.h | <filename>lcof_029/cpp_029/Solution1.h
//
// Created by ooooo on 2020/3/18.
//
#ifndef CPP_029__SOLUTION1_H_
#define CPP_029__SOLUTION1_H_
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &matrix) {
if (matrix.empty()) return {};
int m = matrix.size(), n = matrix[0].size();
int i = 0, j = 0, count = 0, total = m * n;
vector<int> ans;
while (true) {
while (j < n - count) {
ans.push_back(matrix[i][j]);
j++;
}
if (ans.size() == total) break;
// 越界处理 和 重复元素处理
j--;
i++;
while (i < m - count) {
ans.push_back(matrix[i][j]);
i++;
}
if (ans.size() == total) break;
i--;
j--;
while (j >= count) {
ans.push_back(matrix[i][j]);
j--;
}
if (ans.size() == total) break;
j++;
i--;
count += 1;
while (i >= count) {
ans.push_back(matrix[i][j]);
i--;
}
if (ans.size() == total) break;
i++;
j++;
}
return ans;
}
};
#endif //CPP_029__SOLUTION1_H_
|
Subsets and Splits