diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..fe4b463 Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 02aa62a..0fcbd7f 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,7 +8,8 @@ "defines": [ "_DEBUG", "UNICODE", - "_UNICODE" + "_UNICODE", + "__SIZE_TYPE__=long long unsigned int" ], "intelliSenseMode": "gcc-x64" } diff --git a/AlgorithmCode b/AlgorithmCode new file mode 160000 index 0000000..7f0f0f4 --- /dev/null +++ b/AlgorithmCode @@ -0,0 +1 @@ +Subproject commit 7f0f0f46dbe90bbbeb1fb31635136252eae7e0dd diff --git a/IsPopOrder.cpp b/IsPopOrder.cpp new file mode 100644 index 0000000..0628c56 --- /dev/null +++ b/IsPopOrder.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +using namespace std; + +bool validateStackSequences(vector& pushed, vector& popped) { + bool res = false; + + if (!pushed.empty() && !popped.empty()) + { + int push = 0; + int pop = 0; + int lens = popped.size(); + stack tmpStack; + + while (pop < lens) + { + while (tmpStack.empty() || tmpStack.top() != popped[pop]) + { + if (push == lens) + { + break; + } + tmpStack.push(pushed[push]); + push++; + } + if (tmpStack.top() != popped[pop]) + { + break; + } + tmpStack.pop(); + pop++; + } + if (tmpStack.empty() && pop == lens) + { + res = true; + } + } + else + { + return true; + } + + return res; +} + +int main() +{ + vector pop; + vector push; + int pushed[5] = {1,2,3,4,5}; + int popped[5] = {4,5,3,2,1}; + for (int i = 0; i < 5; i++) + { + pop.push_back(popped[i]); + push.push_back(pushed[i]); + } + + bool res = validateStackSequences(push, pop); + return 0; +} \ No newline at end of file diff --git a/IsPopOrder.exe b/IsPopOrder.exe new file mode 100644 index 0000000..d790ad7 Binary files /dev/null and b/IsPopOrder.exe differ diff --git a/MedianFinder.cpp b/MedianFinder.cpp new file mode 100644 index 0000000..6305816 --- /dev/null +++ b/MedianFinder.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +priority_queue, greater> small; +priority_queue big; + +void addNum(double num) +{ + if (big.empty()) + { + big.push(num); + return; + } + + if (small.size() == big.size()) + { + if (num > big.top()) + { + small.push(num); + } + else + { + big.push(num); + } + } + else if (small.size() > big.size()) + { + if (num < small.top()) + { + big.push(num); + } + else + { + big.push(small.top()); + small.pop(); + small.push(num); + } + } + else if(big.size() > small.size()) + { + if (num > big.top()) + { + small.push(num); + } + else + { + small.push(big.top()); + big.pop(); + big.push(num); + } + } +} + + +double findMedian() { + if (small.size() == big.size()) + { + return (small.top() + big.top()) / 2.0; + } + else if(small.size() > big.size()) + { + return small.top(); + } + return big.top(); +} \ No newline at end of file diff --git a/MinStack.cpp b/MinStack.cpp new file mode 100644 index 0000000..c01cca9 --- /dev/null +++ b/MinStack.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +using namespace std; + +// stack datas; +// stack mins; + +// void push(int x) { +// datas.push(x); +// if (mins.empty() || x < mins.top()) +// { +// mins.push(x); +// } +// else +// { +// mins.push(mins.top()); +// } +// } + +// void pop() { +// if (!mins.empty() && !datas.empty()) +// { +// mins.pop(); +// datas.pop(); +// } +// } + +// int top() { +// return datas.top(); +// } + +// int min() { +// return mins.top(); +// } + +stack data; +stack mins; + +void push(int x) { + if (mins.empty() || x < mins.top()) + { + mins.push(x); + } + else + { + mins.push(mins.top()); + } + data.push(x); +} + +void pop() { + if(!data.empty() && !mins.empty()){ + data.pop(); + mins.pop(); + } + +} + +int top() { + return data.top(); +} + +int getMin() { + return mins.top(); +} \ No newline at end of file diff --git a/MyStack.cpp b/MyStack.cpp new file mode 100644 index 0000000..9f9dda8 --- /dev/null +++ b/MyStack.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +queue data; +void push(int x) { + queue tmp_queue; + tmp_queue.push(x); + while (!data.empty()) + { + tmp_queue.push(data.front()); + data.pop(); + } + + while (!tmp_queue.empty()) + { + data.push(tmp_queue.front()); + tmp_queue.pop(); + } + +} + +int pop() { + int x = data.front(); + data.pop(); + return x; +} + +int top() +{ + return data.front(); +} + +bool empty() +{ + return data.empty(); +} \ No newline at end of file diff --git a/Surrounded.cpp b/Surrounded.cpp new file mode 100644 index 0000000..0886b71 --- /dev/null +++ b/Surrounded.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +using namespace std; + +vector> map; +int n, m; + +void floodfill(vector>& gra, int i, int j) +{ + gra[i][j] = 1; + map[i][j] = '#'; + if (i + 1 < n && map[i + 1][j] == 'O' && !gra[i + 1][j]) + { + floodfill(gra, i + 1, j); + } + if (i - 1 >= 0 && map[i - 1][j] == 'O' && !gra[i - 1][j]) + { + floodfill(gra, i - 1, j); + } + if (j + 1 < m && map[i][j + 1] == 'O' && !gra[i][j + 1]) + { + floodfill(gra, i, j + 1); + } + if (j - 1 >= 0 && map[i][j - 1] == 'O' && !gra[i][j - 1]) + { + floodfill(gra, i, j - 1); + } +} + +void solve(vector>& board) { + n = board.size(); + if (!n) + { + return; + } + m = board[0].size(); + map = board; + vector> gra(n, vector(m, 0)); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (i == 0 || i == n - 1 || j == 0 || j == m - 1) + { + if (!gra[i][j] && map[i][j] == 'O') + { + floodfill(gra, i, j); + } + } + } + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (map[i][j] == 'O') + { + map[i][j] = 'X'; + } + if (map[i][j] == '#') + { + map[i][j] = 'O'; + } + } + } + board = map; +} \ No newline at end of file diff --git a/addBinary.cpp b/addBinary.cpp new file mode 100644 index 0000000..a3359ab --- /dev/null +++ b/addBinary.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string addBinary(string a, string b) { + if (a.length() > b.length()) + { + swap(a,b); + } + + vector sum(b.length() + 1, 0); + + for (int i = b.length(), j = a.length(); i > 0; i--) + { + int bl = b[i - 1] - '0'; + if (j > 0) + { + int al = a[j - 1] - '0'; + sum[i] += al + bl; + + if (sum[i] / 2 != 0) + { + sum[i - 1] += sum[i] / 2; + sum[i] %= 2; + } + j--; + } + else + { + sum[i] += bl; + sum[i - 1] += sum[i] / 2; + sum[i] %= 2; + } + } + + string res = ""; + + for (int i = 0; i < sum.size(); i++) + { + if(sum[i] == 0 && i == 0) + continue; + res += sum[i] + '0'; + } + + return res; +} + +int main() +{ + string a = "0", b = "0"; + addBinary(a, b); + return 0; +} \ No newline at end of file diff --git a/addBinary.exe b/addBinary.exe new file mode 100644 index 0000000..68c4da9 Binary files /dev/null and b/addBinary.exe differ diff --git a/algorithm b/algorithm new file mode 100644 index 0000000..8ba1589 --- /dev/null +++ b/algorithm @@ -0,0 +1,48 @@ +package Algorithm; + +public class Sortways { + + + //插入排序 + public static void insertSort(int[] nums) { + if (nums == null || nums.length < 2) { + return; + } + for(int i = 1; i < nums.length; i++) { + for(int j = i - 1; j >= 0 && nums[j + 1] < nums[j]; j--) { + swap(nums, j, j + 1); + } + } + } + + //冒泡排序 + public static void bubbleSort(int[] nums) { + if(nums == null || nums.length < 2) { + return; + } + for(int i = nums.length - 1; i > 0 ; i--) { + for(int j = i; nums[j - 1] > nums[j] && j > 0; j--) { + swap(nums,j - 1, j); + } + } + } + + + + + public static void swap(int[] nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } + + public static void main(String[] args) { + int[] nums = {5, 4, 3, 2, 1}; +// insertSort(nums); + bubbleSort(nums); + for(int i: nums) { + System.out.print(i); + } + } + +} diff --git a/binaryTree.cpp b/binaryTree.cpp index 1c0db8d..e7498c7 100644 --- a/binaryTree.cpp +++ b/binaryTree.cpp @@ -77,7 +77,7 @@ class Solution{ } tree.pop();// } - return a; + return a; } }; diff --git a/buildTree.cpp b/buildTree.cpp new file mode 100644 index 0000000..54ccc03 --- /dev/null +++ b/buildTree.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +void buildTree(TreeNode* &root, vector& nums, int lens, int i) +{ + if (i >= lens) + { + return; + } + root = new TreeNode(nums[i]); + buildTree(root->left, nums, lens, i * 2 + 1); + buildTree(root->right, nums, lens, i * 2 + 2); +} + +void inorderTravel(TreeNode* root, vector& res) +{ + if (root == NULL) + { + return; + } + + if (root->left != NULL) + { + inorderTravel(root->left, res); + } + + res.push_back(root->val); + + if (root->right != NULL) + { + inorderTravel(root->right, res); + } +} + +TreeNode* Trees(TreeNode* root, vector& nums) +{ + int lens = nums.size(); + buildTree(root, nums, lens, 0); + return root; +} + +int main() +{ + vector res; + vector nums = {1, 2, 3, 4, 5, 6, 7, 8}; + TreeNode* root; + root = Trees(root, nums); + inorderTravel(root, res); + return 0; +} + diff --git a/buildTree.exe b/buildTree.exe new file mode 100644 index 0000000..2b4d9e6 Binary files /dev/null and b/buildTree.exe differ diff --git a/calculate.cpp b/calculate.cpp new file mode 100644 index 0000000..958b121 --- /dev/null +++ b/calculate.cpp @@ -0,0 +1,232 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +stack number; +stack operation; + +void cal() +{ + if (number.size() < 2) + { + return; + } + + int b = number.top(); + number.pop(); + + int a = number.top(); + number.pop(); + + if (operation.top() == '+') + { + number.push(a + b); + } + else if (operation.top() == '-') + { + number.push(a - b); + } + operation.pop(); +} + +int calculate(string s) { + const int BEGIN = 0; + const int NUMBER = 1; + const int OPERATION = 2; + int flag = 0; + int state = BEGIN; + int num = 0; + + for (int i = 0; i < s.length(); i++) + { + if(s[i] == ' ') continue; + + switch (state) + { + case BEGIN: + if(s[i] >= '0' && s[i] <= '9') + { + state = NUMBER; + } + else + { + state = OPERATION; + } + i--; + break; + + case NUMBER: + if (s[i] >= '0' && s[i] <= '9') + { + num = num * 10 + s[i] - '0'; + } + else + { + number.push(num); + if (flag) + { + cal(); + } + num = 0; + i--; + state = OPERATION; + } + break; + + case OPERATION: + if (s[i] == '+' || s[i] == '-') + { + operation.push(s[i]); + flag = 1; + } + else if (s[i] == '(') + { + state = NUMBER; + flag = 0; + } + else if (s[i] >= '0' && s[i] <= '9') + { + state = NUMBER; + i--; + } + else if (s[i] == ')') + { + cal(); + } + break; + } + } + if (num != 0) + { + number.push(num); + cal(); + } + if (num == 0 && number.empty()) + { + return 0; + } + return number.top(); +} + +class Solution { +public: +stack number; +stack operation; + +void cal() +{ + if (number.size() < 2) + { + return; + } + + int b = number.top(); + number.pop(); + + int a = number.top(); + number.pop(); + + if (operation.top() == '+') + { + number.push(a + b); + } + else if (operation.top() == '-') + { + number.push(a - b); + } + operation.pop(); +} + +int calculate(string s) { + const int BEGIN = 0; + const int NUMBER = 1; + const int OPERATION = 2; + int flag = 0; + int state = BEGIN; + long long int num = 0; //ע˴ long long int + + for (int i = 0; i < s.length(); i++) + { + if(s[i] == ' ') continue; + + switch (state) + { + case BEGIN: + if(s[i] >= '0' && s[i] <= '9') + { + state = NUMBER; + } + else + { + state = OPERATION; + } + i--; + break; + + case NUMBER: + if (s[i] >= '0' && s[i] <= '9') + { + num = num * 10 + s[i] - '0'; + } + else + { + number.push(num); + if (flag) + { + cal(); + } + num = 0; + i--; + state = OPERATION; + } + break; + + case OPERATION: + if (s[i] == '+' || s[i] == '-') + { + operation.push(s[i]); + flag = 1; + } + else if (s[i] == '(') + { + state = NUMBER; + flag = 0; + } + else if (s[i] >= '0' && s[i] <= '9') + { + state = NUMBER; + i--; + } + else if (s[i] == ')') + { + cal(); + } + break; + } + } + if (num != 0) + { + number.push(num); + cal(); + } + if (num == 0 && number.empty()) + { + return 0; + } + cout<< number.top(); + return number.top(); +} +}; + +int main() +{ + string a = "2147483647"; + calculate(a); + return 0; +} \ No newline at end of file diff --git a/calculate.exe b/calculate.exe new file mode 100644 index 0000000..0f7736d Binary files /dev/null and b/calculate.exe differ diff --git a/calculateMinimumHP.cpp b/calculateMinimumHP.cpp new file mode 100644 index 0000000..a6b099d --- /dev/null +++ b/calculateMinimumHP.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int calculateMinimumHP(vector>& dungeon) { + int rows = dungeon.size(); + int columns = dungeon[0].size(); + + vector> dp(rows, vector(columns, 0)); + dp[rows - 1][columns - 1] = max(1, 1 - dungeon[rows - 1][columns - 1]); + + for (int i = rows - 2; i >= 0 ; i--) + { + dp[i][columns - 1] = max(1, dp[i + 1][columns - 1] - dungeon[i][columns - 1]); + } + + for (int j = columns - 2; j >= 0 ; j--) + { + dp[rows - 1][j] = max(1, dp[rows - 1][j + 1] - dungeon[rows - 1][j]); + } + + for (int i = rows - 2; i >= 0; i--) + { + for (int j = columns - 2; j >= 0; j--) + { + int mins = min(dp[i + 1][j], dp[i][j + 1]); + dp[i][j] = max(1, mins - dungeon[i][j]); + } + } + return dp[0][0]; +} \ No newline at end of file diff --git a/canCompleteCircuit.cpp b/canCompleteCircuit.cpp new file mode 100644 index 0000000..5e9a5e2 --- /dev/null +++ b/canCompleteCircuit.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int canCompleteCircuit(vector& gas, vector& cost) { + int total = 0, tank = 0, start = 0; + for (int i = 0; i < cost.size(); i++) + { + tank = gas[i] - cost[i]; + if (tank < 0 ) + { + start = i + 1; + total += tank; + tank = 0; + } + } + return total + tank > 0 ? start : -1; +} \ No newline at end of file diff --git a/canFinish.cpp b/canFinish.cpp new file mode 100644 index 0000000..02b8682 --- /dev/null +++ b/canFinish.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct node +{ + int flag; + vector neibor; + node(int x) : flag(x){}; +}; + +bool dfs(node* p, vector& visit) +{ + visit[p->flag] = 0; + + for (int i = 0; i < p->neibor.size(); i++) + { + if (visit[p->neibor[i]->flag] == -1) + { + if (dfs(p->neibor[i], visit) == 0) + { + return false; + } + } + else if(visit[p->neibor[i]->flag] == 0) + { + return false; + } + } + + visit[p->flag] = 1; + return true; +} + +bool canFinish1(int numCourses, vector>& prerequisites) { + vector visit; + vector graph; + + for (int i = 0; i < numCourses; i++) + { + visit.push_back(-1); + graph.push_back(new node(i)); + } + + for (int j = 0; j < prerequisites.size(); j++) + { + node *begin = graph[prerequisites[j][1]]; + node *end = graph[prerequisites[j][0]]; + begin->neibor.push_back(end); + } + + for (int i = 0; i < graph.size(); i++) + { + if (visit[i] == -1 && !dfs(graph[i], visit)) + { + return false; + } + } + + return true; +} + + +bool canFinish(int numCourses, vector>& prerequisites) { + vector graph; + vector dep; + + for (int i = 0; i < numCourses; i++) + { + dep.push_back(0); + graph.push_back(new node(i)); + } + + for (int i = 0; i < prerequisites.size(); i++) + { + node* begin = graph[prerequisites[i][1]]; + node* end = graph[prerequisites[i][0]]; + begin->neibor.push_back(end); + dep[prerequisites[i][0]]++; + } + + queue Q; + for (int i = 0; i < numCourses; i++) + { + if (dep[i] == 0) + { + Q.push(graph[i]); + } + } + + while (!Q.empty()) + { + node* p = Q.front(); + Q.pop(); + + for (int i = 0; i < p->neibor.size(); i++) + { + dep[p->neibor[i]->flag]--; + if (dep[p->neibor[i]->flag] == 0) + { + Q.push(p->neibor[i]); + } + } + } + + for (int i = 0; i < dep.size(); i++) + { + if (dep[i] != 0) + { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/canJump.cpp b/canJump.cpp new file mode 100644 index 0000000..315ae46 --- /dev/null +++ b/canJump.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +// bool canJump(vector& nums) { +// if(nums.empty()) return true; +// vector index; +// for (int i = 0; i < nums.size(); i++) +// { +// index.push_back(i + nums[i]); +// } +// int jump = 0; +// int maxIndex = index[0]; +// while (jump < nums.size() && jump <= maxIndex) +// { +// if (maxIndex >= nums.size()) +// { +// return true; +// } + +// maxIndex = max(maxIndex, index[jump++]); +// } +// if (jump == nums.size()) +// { +// return true; +// } +// return false; +// } + +// int jump(vector& nums) { +// if(nums.size() < 2) +// return 0; +// int cur = nums[0]; +// int pre = nums[0]; +// int jump = 1; +// for (int i = 0; i < nums.size(); i++) +// { +// if (i > cur) +// { +// jump++; +// cur = pre; +// } +// if (pre < nums[i] + 1) +// { +// pre = nums[i] + 1; +// } +// } +// return jump; +// } + +bool canJump(vector& nums) { + int maxIndex = nums[0]; + for (int i = 1; i < nums.size(); i++) + { + if (i < maxIndex) + { + maxIndex = max(maxIndex, i + nums[i]); + } + if (maxIndex > nums.size() - 2) + { + return true; + } + } + return false; +} + +int jump(vector& nums) { + int lens = nums.size(); + if (lens < 2) + { + return lens; + } + int cur = nums[0], pre = nums[0]; + int res = 0; + for (int i = 1; i < lens; i++) + { + if (i > cur) + { + cur = pre; + res++; + } + pre = max(pre, nums[i] + i); + } + return res; +} + + +int findMinArrowShots(vector>& points) +{ + if(points.empty()) return 0; + sort(points.begin(), points.end()); + int ph = points[0][0]; + int pr = points[0][1]; + int lens = points.size(); + int res = 1; + + for (int i = 0; i < lens; i++) + { + if (points[i][0] <= pr) + { + ph = points[i][0]; + pr = min(pr, points[i][1]); + } + else + { + res++; + ph = points[i][0]; + pr = points[i][1]; + } + } + return res; +} \ No newline at end of file diff --git a/climbStairs.cpp b/climbStairs.cpp new file mode 100644 index 0000000..b2e0c79 --- /dev/null +++ b/climbStairs.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int climbStairs1(int n) { + if(!n) return 0; + int tmp, cur = 1, pre = 1; + for (int i = 0; i < n; i++) + { + tmp = cur; + cur += pre; + pre = tmp; + } + return cur; +} + +int climbStairs2(int n) { + if (n == 1 && n == 2) + { + return n; + } + int sum = climbStairs2(n - 1) + climbStairs2(n - 2); + return sum; +} + +int climbStairs(int n) { + vector nums(n + 3); + nums[0] = 1, nums[1] = 1, nums[2]; + for (int i = 0; i < n; i++) + { + nums[i] = nums[i - 2] + nums[i - 1]; + } + return nums[n - 1]; +} \ No newline at end of file diff --git a/code/LCOF.cpp b/code/LCOF.cpp new file mode 100644 index 0000000..40fd191 --- /dev/null +++ b/code/LCOF.cpp @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int findRepeatNumber(vector& nums) +{ + int lens = nums.size(); + for(int i = 0; i < lens; i++){ + while (nums[i] != i){ + if (nums[i] == nums[nums[i]]) + { + return nums[i]; + } + swap(nums[i], nums[nums[i]]); + } + } + return 0; +} + + +bool containsDuplicate_one(vector& nums) +{ + unordered_set dup(nums.begin(), nums.end()); + if (dup.size() != nums.size()){ + return true; + } + return false; +} + +bool containsDuplicate(vector& nums) +{ + int lens = nums.size(); + unordered_map dup; + if (lens < 2){ + return false; + } + + for(int i = 0; i < lens; i++){ + dup[nums[i]]++; + if(dup[nums[i]] > 1){ + return true; + } + } + return false; +} + +bool Find(int* matrix, int rows, int columns, int numbers) +{ + bool found = false; + if (matrix != nullptr && rows > 0 && columns > 0) + { + int row = 0; + int column = columns - 1; + while (row < rows && columns>= 0) + { + if (matrix[rows * columns + columns] == numbers){ + found = true; + break; + } + else if(matrix[row * columns + column] > numbers){ + column--; + } + else{ + row++; + } + } + } + return found; +} + +// searchMatrix +bool findNumberIn2DArray(vector>& matrix, int target) +{ + if (matrix.empty()) + { + return false; + } + + int rows = matrix.size(); + int columns = matrix[0].size(); + int row = 0; + int column = columns - 1; + if(rows > 0 || columns > 0){ + while (row < rows && column > -1){ + if (matrix[row][column] == target) + { + return true; + } + else if (matrix[row][column] > target) + { + column--; + } + else + { + row++; + } + } + } + return false; +} + +// void replaceBlank(char string[], int length) +// { +// if (string == nullptr || length <= 0) +// { +// return; +// } +// int rawLength = 0; +// int numBlank = 0; +// int i = 0; +// while (string[i] != '\0') +// { +// rawLength++; +// if(string[i] == ' '){ +// numBlank++; +// } +// i++; +// } + +// int length = rawLength + numBlank * 2; +// int p1 = rawLength; +// int p2 = length; +// while (p1 > -1 && p2 > p1) +// { +// if(string[p1] = ' '){ +// string[p2--] = '0'; +// string[p2--] = '2'; +// string[p2--] = '%'; +// } +// else +// { +// string[p2--] = string[p1]; +// } +// p1--; +// } + + + +// } + +string replaceSpace(string s) +{ + int rawLength = s.length(); + int numBlank = 0; + if (rawLength == 0) + { + return s; + } + + for(int i = 0; i < rawLength; i++){ + if (s[i] == ' '){ + numBlank++; + } + } + + int p2 = rawLength + 2 * numBlank; + int p1 = rawLength; + char string[p2]; + + while (p1 > -1 && p2 > p1) + { + if(s[p1] == ' '){ + string[p2--] = '0'; + string[p2--] = '2'; + string[p2--] = '%'; + } + else + { + string[p2--] = s[p1]; + } + p1--; + } + printf("%s",string); + return s; +} + +int main() +{ + // vector> nums = {{-5}}; + // findNumberIn2DArray(nums,-5); + replaceSpace("We are happy."); + return 0; +} diff --git a/code/LCOF.exe b/code/LCOF.exe new file mode 100644 index 0000000..7c7c5be Binary files /dev/null and b/code/LCOF.exe differ diff --git a/code/binaryTree.cpp b/code/binaryTree.cpp new file mode 100644 index 0000000..1c0db8d --- /dev/null +++ b/code/binaryTree.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x): val(x),left(NULL),right(NULL){} +}; + +// unordered_map maps; + +// TreeNode* buildTree2(int preL, int inL, int preR) +// { + +// } + +// TreeNode* buildTree(vector& preorder, vector& inorder) +// { +// for (int i = 0; i < inorder.size(); i++) +// { +// maps[inorder[i]] = i; +// } + +// return buildTree2(0, 0, inorder.size() - 1, preorder); +// } +class Solution{ + public: + unordered_mapmaps; + vector preorder; + + TreeNode* buildTree(vector& preorder, vector& inorder) + { + for (int i = 0; i < inorder.size(); i++) + { + maps[inorder[i]] = i; + } + TreeNode* root = buildTree2(0,inorder.size() - 1,0, preorder.size() - 1,preorder); + return root; + } + + TreeNode* buildTree2(int preL, int preR, int inL, int inR, vector preorder) + { + if (preL > preR || inL > inR){ + return NULL; + } + + int middle = preorder[preL]; + TreeNode* root = new TreeNode(middle); + int index = maps[middle]; + root->left = buildTree2(preL + 1, preL + (index - inL), inL, index - 1); + root->right = buildTree2(preL + (index - inL) + 1, preR, index + 1, inR); + return root; + } + + vector PrintFromTopToBottom(TreeNode* root) { + vector a; + queue tree; + if(root==NULL)return a; + TreeNode* r; + tree.push(root); + while(tree.size()){ + r=tree.front(); + a.push_back(r->val); + if(r->left!=NULL){ + printf("%d",r->left); + tree.push(r->left); + } + if(r->right!=NULL){ + printf("%d",r->right); + tree.push(r->right); + } + tree.pop();// + } + return a; + } +}; + +int main() +{ + Solution* a = new Solution(); + vector b = {3,9,20,15,7}; + vector c = {9,3,15,20,7}; + a->preorder = b; + TreeNode* root = a->buildTree(b,c); + a->PrintFromTopToBottom(root); + return 0; +} + +// unordered_map map; + +// TreeNode* buildTree2(int preL, int preR, int inL,int inR, vector& preorder, vector& inorder) +// { +// if(preL > preR || inL > inR){ +// return NULL; +// } + +// int middle = preorder[preL]; +// TreeNode* root = new TreeNode(middle); +// auto it = map.find(middle); +// int index = it->first; + +// root->left = buildTree2(preL + 1, preL + (index - inL), inL, index - 1, preorder, inorder); +// root->right = buildTree2(preL + (index - inL) + 1, preR, index + 1, inR, preorder, inorder); +// return root; +// } + +// TreeNode* buildTree(vector& preorder, vector& inorder) +// { +// int preLen = preorder.size(); +// int inLen = inorder.size(); + +// for (int i = 0; i < inLen; i++) +// { +// map[i] = inorder[i]; +// } + +// return buildTree2(0, preLen - 1, 0, inLen - 1, preorder, inorder); + +// } diff --git a/code/binaryTree.exe b/code/binaryTree.exe new file mode 100644 index 0000000..1e64b71 Binary files /dev/null and b/code/binaryTree.exe differ diff --git a/code/constructTree.cpp b/code/constructTree.cpp new file mode 100644 index 0000000..864a11f --- /dev/null +++ b/code/constructTree.cpp @@ -0,0 +1,10 @@ +#include +#include + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x): val(x),left(NULL),right(NULL){} +}; + diff --git a/code/cuttingRope.cpp b/code/cuttingRope.cpp new file mode 100644 index 0000000..be307cb --- /dev/null +++ b/code/cuttingRope.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +using namespace std; + +// int cuttingRope(int n) +// { + +// if (n < 2) +// { +// return 0; +// } +// if (n == 2) +// { +// return 1; +// } +// if (n == 3) +// { +// return 2; +// } + +// int *res = new int[n + 1]; +// int max = 0; +// res[1] = 1; +// res[2] = 2; +// res[3] = 3; + +// for (int i = 4; i <= n; i++) +// { max = 0; +// for (int j = 1; j <= i/2; j++) +// { +// int tmp = res[j] * res[i - j]; +// if (max < tmp){ +// max = tmp; +// } +// } +// res[i] = max; +// } +// max = res[n]; +// return max; +// } + + +// int pows(int n, int m) +// { +// int res = 1; +// if (m == 3) +// { +// for (int i = 0; i < n; i++) +// { +// res = (res * 3) % 1000000007; +// } +// return res; +// } +// else +// { +// for (int i = 0; i < n; i++) +// { +// res = (res * 2) % 1000000007; +// } +// return res; +// } +// } + + +// int cuttingRope(int n) +// { + // if (n < 2) + // { + // return 1; + // } + // if (n == 2) + // { + // return 1; + // } + // if (n == 3) + // { + // return 2; + // } + +// int timeThree = n / 3; +// if (n - timeThree * 3 == 1) +// { +// timeThree--; +// } +// int timeTwo = (n - timeThree * 3) / 2; +// int max = pows(timeThree, 3) * pows(timeTwo, 2); +// return max % 1000000007; +// } + +int cuttingRope(int n) +{ + if (n < 2) + { + return 1; + } + if (n == 2) + { + return 1; + } + if (n == 3) + { + return 2; + } + + long long res = 1; + while (n > 4) + { + res *= 3; + res %= 1000000007; + n -= 3; + } + return res * n % 1000000007; + +} + +int main() +{ + int n = cuttingRope(127); + printf("%d",n); + return 0; +} \ No newline at end of file diff --git a/code/cuttingRope.exe b/code/cuttingRope.exe new file mode 100644 index 0000000..bfa1d81 Binary files /dev/null and b/code/cuttingRope.exe differ diff --git a/code/fib.cpp b/code/fib.cpp new file mode 100644 index 0000000..088dd9c --- /dev/null +++ b/code/fib.cpp @@ -0,0 +1,68 @@ +#include + +// int fib2(int n){ +// if(n == 0){ +// return 0; +// } +// else if(n == 1) +// { +// return 1; +// } +// else +// { +// return fib(n - 1) + fib(n - 2); +// } + +// } + +int fib(int n) +{ + if (n == 0) + { + return 0; + } + else if(n == 1){ + return 1; + } + else + { + long long fib_one = 1; + long long fib_two = 0; + long long fibn = 0; + for (int i = 2; i <= n; i++) + { + fibn = (fib_one + fib_two)%1000000007; + fib_two = fib_one; + fib_one = fibn; + } + return fibn; + } +} + +int numWays(int n) +{ + if(n == 0){ + return 1; + } + if (n == 1){ + return 1; + } + int one = 1; + int two = 1; + int way = 0; + for (int i = 2; i <= n; i++) + { + way = (one + two) % 1000000007; + one = two; + two = way; + } + return way; +} + + +int main() +{ + int c = numWays(2); + printf("%d",c); + return 0; +} \ No newline at end of file diff --git a/code/fib.exe b/code/fib.exe new file mode 100644 index 0000000..b0f29e5 Binary files /dev/null and b/code/fib.exe differ diff --git a/code/hammingWeight.cpp b/code/hammingWeight.cpp new file mode 100644 index 0000000..8703403 --- /dev/null +++ b/code/hammingWeight.cpp @@ -0,0 +1,33 @@ +#include +#include + +// int hammingWeight(uint32_t n) { +// int num = 0; +// while (n) +// { +// if (n & 1) +// { +// num++; +// } +// n = n >> 1; +// } +// return num; +// } + +int hammingWeight(uint32_t n) +{ + int num = 0; + while (n) + { + n = (n - 1) & n; + num++; + } + return num; +} + +int main() +{ + int num = hammingWeight(00000000000000000000000000001011); + printf("%d",num); + return 0; +} \ No newline at end of file diff --git a/code/hammingWeight.exe b/code/hammingWeight.exe new file mode 100644 index 0000000..e48072d Binary files /dev/null and b/code/hammingWeight.exe differ diff --git a/code/hashpath.cpp b/code/hashpath.cpp new file mode 100644 index 0000000..a34f443 --- /dev/null +++ b/code/hashpath.cpp @@ -0,0 +1,52 @@ +#include +// #include +#include +#include +#include +#include +#include +using namespace std; + +bool hasPath(char* matrix, int rows, int cols, char* str) +{ + bool *visited = new bool[rows * cols]; + memset(visited, 0, rows * cols); + + int lens = 0; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (hasPath(matrix, rows, cols, i, j, str, lens, visited)) + { + return true; + } + + } + } + delete[] visited; + return false; +} + +bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, + const char* str, int& lens, bool* visited) +{ + if(str[lens] == '\0') return true; + bool haspath = false; + if (row >= 0 && row < rows && col >= 0 && col < cols + && matrix[row * cols + col] == str[lens] + && !visited[row * cols + col]) + { + lens++; + visited[row * cols + col] = true; + haspath = hasPathCore(matrix, rows, cols, row, col - 1, str, lens, visited) + ||hasPathCore(matrix, rows, cols, row - 1, col, str, lens, visited) + ||hasPathCore(matrix, rows, cols, row, col + 1, str, lens, visited) + ||hasPathCore(matrix, rows, cols, row + 1, col, str, lens, visited); + if(!haspath){ + lens--; + visited[row * cols + col] = false; + } + } + return haspath; +} \ No newline at end of file diff --git a/code/list.cpp b/code/list.cpp new file mode 100644 index 0000000..d03b986 --- /dev/null +++ b/code/list.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +using namespace std; + +struct Listnode{ + int val; + Listnode* next; +}; + +void add_tail(Listnode* &head, int value) +{ + Listnode* newNode = new Listnode(); + newNode->val = value; + newNode->next = NULL; + + if(head == NULL){ + head = newNode; + } + else{ + Listnode* p = head; + while (p->next != NULL) + { + p = p->next; + } + p->next = newNode; + } +} + +vector reversePrint(Listnode* head) +{ + stack nodes; + Listnode* p = head; + while (p != nullptr) + { + nodes.push(p); + p = p->next; + } + while (!nodes.empty()) + { + p = nodes.top(); + printf("%d\t",p->val); + nodes.pop(); + } + +} + + +int main() +{ + Listnode* head =NULL; + add_tail(head, 10); + if (head != NULL) + { + cout << head->val << endl; + } + else + { + cout << "head is null"< +#include +using namespace std; + + struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} + }; + +int min_dif = 100; +int temp1 = -100; +int temp2 = -100; + +int minDiffInBST(TreeNode* root) { + return inorder(root); +} + +int inorder(TreeNode* r) +{ + if(!r) + return min_dif; + if(r -> left) + inorder(r -> left); + temp1 = temp2; + temp2 = r -> val; + min_dif = min(min_dif, temp2 - temp1); + if(r -> right) + inorder(r -> right); + return min_dif; +} + +int main() +{ + int nums[] = {4,2,6,1,3,NULL,NULL}; + TreeNode* nodes = nums; + int res = minDiffInBST(nodes); +} \ No newline at end of file diff --git a/code/minnumber.cpp b/code/minnumber.cpp new file mode 100644 index 0000000..618ad03 --- /dev/null +++ b/code/minnumber.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +int minArray(vector& nums) +{ + int length = nums.size(); + if (length < 0){ + return 0; + } + int p1 = 0; + int p2 = length - 1; + int pm = p1; + + while (nums[p1] >= nums[p2]) + { + if (p2 - p1 == 1) + { + pm = p2; + break; + } + pm = (p1 + p2) / 2; + + if (nums[p1] == nums[p2] && nums[p1] == nums[pm]){ + int res = nums[p1]; + for (int i = 0; i <= p2; i++){ + if (res > nums[i]){ + res = nums[i]; + } + } + return res; + } + + if (nums[pm] >= nums[p1]) + { + p1 = pm; + } + else if(nums[pm] <= nums[p2]) + { + p2 = pm; + } + } + return nums[pm]; +} \ No newline at end of file diff --git a/code/movingCount.cpp b/code/movingCount.cpp new file mode 100644 index 0000000..70d312b --- /dev/null +++ b/code/movingCount.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +using namespace std; + +int movingCount(int m, int n, int k) +{ + int movingCountCore(int threshold, int rows, int cols, int i, int j, bool *visited); + int rows = m, cols = n; + int threshold = k; + + if (threshold < 0|| rows < 0 || cols < 0) + { + return 0; + } + + bool *visited = new bool[rows * cols]; + memset(visited, 0, rows * cols); + // for (int i = 0; i < rows * cols; i++) + // { + // visited[i] = false; + // } + + + int count = movingCountCore(threshold, rows, cols, 0, 0, visited); + return count; +} + +int movingCountCore(int threshold, int rows, int cols, int i, int j, bool *visited) +{ + bool check(int threshold, int rows, int cols, int i , int j, bool *visited); + int count = 0; + if (check(threshold, rows, cols, i, j, visited)) + { + visited[i * cols + j] = true; + int one = movingCountCore(threshold,rows, cols, i - 1, j, visited); + int two = movingCountCore(threshold,rows, cols, i, j - 1, visited); + int three = movingCountCore(threshold, rows, cols, i + 1, j, visited); + int four = movingCountCore(threshold, rows, cols, i, j + 1,visited); + count = 1 + one + two + three + four; + } + return count; +} + +bool check(int threshold, int rows, int cols, int i , int j, bool *visited) +{ + int getSum(int row, int col); + if (i >= 0 && i < rows && j >= 0 && j < cols && getSum(i ,j) <= threshold && !visited[i * cols + j]) + { + return true; + } + return false; +} + +int getSum(int row, int col) +{ + int sum = 0; + while (row > 0) + { + sum += row % 10; + row /= 10; + } + while (col > 0) + { + sum += col % 10; + col /= 10; + } + return sum; +} + +int main() +{ + int row = 2, col = 3, threshold = 1; + printf("%d",movingCount(row,col,threshold)); + return 0; +} \ No newline at end of file diff --git a/code/movingCount.exe b/code/movingCount.exe new file mode 100644 index 0000000..81cac24 Binary files /dev/null and b/code/movingCount.exe differ diff --git a/code/myPow.cpp b/code/myPow.cpp new file mode 100644 index 0000000..73ed4e8 --- /dev/null +++ b/code/myPow.cpp @@ -0,0 +1,47 @@ +#include +#include +using namespace std; + +double myPow(double x, int n) +{ + if (n < 0) + { + int res = myPowCore(x, n); + return 1/res; + } + else + { + int res = myPowCore(x, n); + return res; + } +} + +double myPowCore(double x, int n) { + if (n == 0) + { + return 1; + } + else if (n == 1) + { + return x; + } + else + { + double res = myPow(x, n >> 1); + res *= res; + if (n & 1 == 1) + { + res *= x; + } + return res; + } +} + + + +int main() +{ + int res = myPow(2,12); + printf("%d",res); + return 0; +} \ No newline at end of file diff --git a/code/myPow.exe b/code/myPow.exe new file mode 100644 index 0000000..887bd42 Binary files /dev/null and b/code/myPow.exe differ diff --git a/code/sort_again.cpp b/code/sort_again.cpp new file mode 100644 index 0000000..39f0be6 --- /dev/null +++ b/code/sort_again.cpp @@ -0,0 +1,96 @@ +#include +#include +using namespace std; + +// void quickSort(int* nums, int left, int right) +// { +// if (left < right) +// { +// int i = left, j = right; +// int x = nums[left]; + +// while (i < j) +// { +// while (i < j && nums[j] >= x){ +// j--; +// } +// if (i < j){ +// nums[i++] = nums[j]; +// } +// while (i < j && nums[i] <= x){ +// i++; +// } +// if (i < j){ +// nums[j--] = nums[i]; +// } +// } +// nums[i] = x; +// quickSort(nums, left, i - 1); +// quickSort(nums, i + 1, right); +// } +// } + +int partition(vector& nums, int left, int right) +{ + int i = left, j = right; + int x = nums[left]; + + while (i < j) + { + while (i < j && nums[j] >= x){ + j--; + } + if (i < j){ + nums[i++] = nums[j]; + } + while (i < j && nums[i] <= x){ + i++; + } + if (i < j){ + nums[j--] = nums[i]; + } + } + nums[i] = x; + return i; +} + +int partition_two(vector& nums, int left, int right) +{ + int pivot = nums[left]; + int i = left + 1, j = right; + while (i < j) + { + while(i < j && nums[i] <= pivot) i++; + while(i < j && nums[j] >= pivot) j--; + if (i < j){ + swap(nums[i], nums[j]); + } + } + swap(nums[i], nums[left]); + return i; +} + +vector quickSort(vector& nums, int left, int right) +{ + if (left < right) + { + int i = partition_two(nums, left, right); + quickSort(nums, left, i - 1); + quickSort(nums, i + 1, right); + } +} + + vector sortArray(vector& nums) { + int i = 0, j = nums.size() - 1; + quickSort(nums, i, j); + return nums; +} + +int main() +{ + vector num = {72,6,57,88,60,42,83,73,48,85}; + int nums[10] = {72,6,57,88,60,42,83,73,48,85}; + quickSort(num, 0, 9); + return 0; + +} \ No newline at end of file diff --git a/code/sort_again.exe b/code/sort_again.exe new file mode 100644 index 0000000..4d19aef Binary files /dev/null and b/code/sort_again.exe differ diff --git a/code/stack_to_queue.cpp b/code/stack_to_queue.cpp new file mode 100644 index 0000000..9fd0af5 --- /dev/null +++ b/code/stack_to_queue.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +class CQueue { + stack s1; + stack s2; +public: + CQueue() { + + } + + void appendTail(int value) { + s1.push(value); + } + + int deleteHead() { + if (s2.empty()) + { + if(s1.empty()){ + return -1; + } + else{ + while (!s1.empty()) + { + s2.push(s1.top()); + s1.pop(); + } + } + } + int data = s2.top(); + s2.pop(); + return data; + } +}; + + +class CQueue { + stack stack_one; + stack stack_two; +public: + CQueue() { + + } + + void appendTail(int value) { + stack_one.push(value); + } + + int deleteHead() { + if (stack_two.empty()) + { + while (!stack_one.empty()) + { + int data = stack_one.top(); + stack_two.push(data); + stack_one.pop(); + } + } + else + { + int data = stack_two.top(); + stack_two.pop(); + } + return data; + } +}; \ No newline at end of file diff --git a/code/test.cpp b/code/test.cpp new file mode 100644 index 0000000..8d307bb --- /dev/null +++ b/code/test.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +bool exist(vector>& board, string word) { + int rows = board.size(); + int cols = board[0].size(); + int lens = 0; + + bool *visited = new bool[rows * cols]; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (hasPathCore(board, rows, cols, i, j, word, lens, visited)) + { + return true; + } + } + } + delete[] visited; + return false; +} + +bool hasPathCore(const char* matrix, int rows, int cols, int i, int j, + const char* str, int& lens, bool* visited) +{ + if(str[lens] == '\0') return true; + bool haspath = false; + if (i >= 0 && i < rows && j >=0 && j < cols + && matrix[i * cols + j] == str[lens] && !visited[i * cols + j]) + { + lens++; + visited[i * cols + j] = true; + haspath = hasPathCore(matrix, rows, cols, i, j - 1, str, lens, visited) + ||hasPathCore(matrix, rows, cols, i - 1, j, str, lens, visited) + ||hasPathCore(matrix, rows, cols, i + 1, j, str, lens , visited) + ||hasPathCore(matrix, rows, cols, i + 1, j, str, lens, visited); + + if (!haspath) + { + lens--; + visited[i * cols +j] = false; + } + } + return haspath; +} \ No newline at end of file diff --git a/coinChange.cpp b/coinChange.cpp new file mode 100644 index 0000000..6214cbc --- /dev/null +++ b/coinChange.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int coinChange(vector& coins, int amount) { + if (coins.empty()) + { + return 0; + } + vector dp(amount+1, -1); //dp1ʼamount + 1 + dp[0] = 0; + for (int i = 0; i < coins.size(); i++) + { + if(coins[i] <= amount) // coinsԽ + dp[coins[i]] = 1; + } + // cout<= 0 && dp[i - coins[j]] != -1) + { + // cout< dp[i - coins[j]] + 1) + // dp[i] = dp[i - coins[j]] + 1; + } + } + } + return dp[amount]; +} \ No newline at end of file diff --git a/combinationSum2.cpp b/combinationSum2.cpp new file mode 100644 index 0000000..b59c2a0 --- /dev/null +++ b/combinationSum2.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector> res; + +void combinationSum(vector& nums, vector& item, int i, int sum, int target) +{ + if (i >= nums.size() || sum > target) + { + return; + } + sum += nums[i]; + item.push_back(nums[i]); + if (sum == target) + { + res.push_back(item); + } + combinationSum(nums, item, i + 1, sum, target); + item.pop_back(); + sum -= nums[i]; + combinationSum(nums, item, i + 1, sum, target); +} + +vector> combinationSum2(vector& candidates, int target) { + if (candidates.empty()) + { + return res; + } + vector item; + sort(candidates.begin(), candidates.end()); + combinationSum(candidates, item, 0, 0, target); + set> ans(res.begin(), res.end()); + vector> res(ans.begin(), ans.end()); + return res; +} \ No newline at end of file diff --git a/constructTrees.cpp b/constructTrees.cpp new file mode 100644 index 0000000..92e70d7 --- /dev/null +++ b/constructTrees.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +vector res; + +TreeNode* constructTrees(TreeNode*& root, vector& nums,int lens, int i) +{ + if (i > lens){ + return root; + } + + root = new TreeNode(nums[i]); + constructTrees(root->left, nums, lens, 2 * i + 1); + constructTrees(root->right, nums, lens, 2 * i + 2); + i--; +} + +void inorderTravel(TreeNode* root) +{ + if (root == NULL) + { + return; + } + + if (root->left != NULL) + { + inorderTravel(root->left); + } + + res.push_back(root->val); + + if (root->right != NULL) + { + inorderTravel(root->right); + } +} + +TreeNode* Trees(TreeNode*& root, vector& nums) +{ + int lens = nums.size() - 1; + TreeNode* roots = constructTrees(root, nums, lens, 0); + return roots; +} + +int main() +{ + vector nums = {1, 2, 3, 4, 5, 6, 7, 8}; + // int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; + // vector nums(a,a+7); + TreeNode* root; + Trees(root, nums); + return 0; +} \ No newline at end of file diff --git a/copyRandomList.cpp b/copyRandomList.cpp new file mode 100644 index 0000000..37bfd3e --- /dev/null +++ b/copyRandomList.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct Node { + int val; + Node* next; + Node* random; + + Node(int _val) { + val = _val; + next = NULL; + random = NULL; + } +}; + +Node* copyRandomList(Node* head) { + map nodeMap; + vector nodes; + Node* p = head; + + int i = 0; + while (p) + { + nodes.push_back(new Node(p->val)); + nodeMap[p] = i; + p = p->next; + i++; + } + + p = head; + i = 0; + nodes.push_back(0); + + while (p) + { + nodes[i]->next = nodes[i + 1]; + if (p->random) + { + int index = nodeMap[p->random]; + nodes[i]->random = nodes[index]; + } + i++; + p = p->next; + } + return nodes[0]; +} + + +// void cloneNodes(Node* head) +// { +// Node* point = head; +// while (point != nullptr) +// { +// Node* clonePoint = new Node(point->val); +// clonePoint->next = point->next; +// clonePoint->random = nullptr; +// point->next = clonePoint; +// point = clonePoint->next; +// } +// } + +// void connection(Node* head) +// { +// Node* point = head; +// while (point != nullptr) +// { +// Node* clonePoint = point->next; +// if (point->random != nullptr) +// { +// clonePoint->random = point->random->next; +// } +// point = clonePoint->next; +// } +// } + +// Node* reconnection(Node* head) { +// Node* point = head; +// Node* cloneHead = nullptr; +// Node* cloneNode = nullptr; + +// if (point != nullptr) +// { +// cloneHead = cloneNode = point->next; +// point->next = cloneNode->next; +// point = point->next; +// } +// while (point != nullptr) +// { +// cloneNode->next = point->next; +// cloneNode = cloneNode->next; +// point->next = cloneNode->next; +// point = point->next; +// } +// return cloneHead; +// } + +// Node* copyRandomList(Node* head) { +// cloneNodes(head); +// connection(head); +// return reconnection(head); +// } \ No newline at end of file diff --git a/countSmaller.cpp b/countSmaller.cpp new file mode 100644 index 0000000..ae3a023 --- /dev/null +++ b/countSmaller.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector res; + +void merge(vector>& arr, int left, int mid, int right) +{ + vector> tmp; + int i = left; + int j = mid + 1; + + while (i <= mid && j <= right) + { + if (arr[i].first <= arr[j].first) + { + res[arr[i].second] += j - mid - 1; + tmp.push_back(arr[i++]); + } + else if(arr[i].first > arr[j].first) + { + tmp.push_back(arr[j++]); + } + } + + while( i <= mid) + { + res[arr[i].second] += j -mid - 1; + tmp.push_back(arr[i++]); + } + + while (j <= right) + { + tmp.push_back(arr[j++]); + } + + for (int i = 0; i < tmp.size(); i++) + { + arr[i + left] = tmp[i]; + } +} + +void mergeSort(vector>& arr, int left, int right) +{ + if (left == right){ + return; + } + int mid = left + (right - left)/2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr,left, mid, right); +} + +vector countSmaller(vector& nums) { + int n = nums.size(); + if(!n) return res; + vector> map; + vector a(n, 0); + res = a; + for (int i = 0; i < n; i++) + { + map.push_back({nums[i], i}); + } + + mergeSort(map, 0, n - 1); + // for(int i = 0; i < map.size(); i++) + // cout<< map[i].first; + for (int i = 0; i < map.size(); i++){ + cout< nums = {5,2,6,1}; + countSmaller(nums); + return 0; +} \ No newline at end of file diff --git a/countSmaller.exe b/countSmaller.exe new file mode 100644 index 0000000..081eeea Binary files /dev/null and b/countSmaller.exe differ diff --git a/deleteNode.cpp b/deleteNode.cpp new file mode 100644 index 0000000..1fc30a2 --- /dev/null +++ b/deleteNode.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* deleteNode(ListNode* head, int val) { + ListNode* p = new ListNode(-1); + p = head; + + if (p == nullptr) + { + return head; + } + if (p->val == val) + { + return nullptr; + } + + while (p->next != nullptr && p->next->val != val) + { + p = p->next; + } + if (p->next->next != nullptr) + { + p->next = p->next->next; + } + else + { + p->next = nullptr; + } + + return head; +} + +ListNode* createNode(ListNode* head, vector& nums) +{ + head = new ListNode(0); + ListNode* p = head; + for (int i = 0; i < nums.size(); i++) + { + ListNode* p2 = new ListNode(nums[i]); + p->next = p2; + p = p2; + } + return head; +} + +int main() +{ + vector nums = {4,5,1,9}; + ListNode* p; + p = createNode(p, nums); + deleteNode(p, 1); + return 0; +} diff --git a/deleteNode.exe b/deleteNode.exe new file mode 100644 index 0000000..97e77f5 Binary files /dev/null and b/deleteNode.exe differ diff --git a/deserialize.cpp b/deserialize.cpp new file mode 100644 index 0000000..e8137d8 --- /dev/null +++ b/deserialize.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + + +void solve(ostringstream& out, TreeNode* root) +{ + if (!root) + { + out<<"null "; + return; + } + out<< root->val << " "; + solve(out, root->left); + solve(out, root->right); +} +// Encodes a tree to a single string. +string serialize(TreeNode* root) { + ostringstream out; + solve(out, root); + return out.str(); +} + +// Decodes your encoded data to tree. + +TreeNode* solve2(istringstream& is) +{ + string str; + is>>str; + if (str == "null") + { + return nullptr; + } + + TreeNode* root = new TreeNode(stoi(str)); + root->left = solve2(is); + root->right = solve2(is); + return root; +} + +TreeNode* deserialize(string data) { + if (!data.length()) + { + return nullptr; + } + istringstream is(data); + TreeNode* root = solve2(is); + return root; +} + +TreeNode* solve2(string data, int index) +{ + TreeNode* root; + if (index >= data.length()) + { + return root; + } + root = new TreeNode(data[index] - '0');//ûа취null漰charstring֮ת + root->left = solve2(data, index * 2 + 1); + root->right = solve2(data, index * 2 + 2); + return root; +} + +TreeNode* deserialize(string data) { + if (!data.length()) + { + return nullptr; + } + TreeNode* root = solve2(data, 0); + return root; +} \ No newline at end of file diff --git a/detectCycle.cpp b/detectCycle.cpp new file mode 100644 index 0000000..6b70607 --- /dev/null +++ b/detectCycle.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode *detectCycle(ListNode *head) { + ListNode *fast = head, *slow = head, *meet; + while (fast) + { + fast = fast->next; + slow = slow->next; + + if(!fast) + return nullptr; + + fast = fast->next; + + if (fast == slow) + { + meet = fast; + break; + } + } + + while (meet != head) + { + if (meet == head) + { + return head; + } + + meet = meet->next; + head = head->next; + } + return nullptr; +} \ No newline at end of file diff --git a/findBestValue.cpp b/findBestValue.cpp new file mode 100644 index 0000000..5f208f7 --- /dev/null +++ b/findBestValue.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int findBestValue(vector& arr, int target) { + int lens = arr.size(); + if (!lens) + { + return 0; + } + sort(arr.begin(), arr.end()); + vector nums(lens + 1); + int diff = 999999999, res = 0; + for (int i = 1; i <= lens; i++) + { + nums[i] = nums[i - 1] + arr[i - 1]; + } + + int r = *max_element(arr.begin(), arr.end()); + int left = 0; + while (left <= r) + { + int mid = (left + r) / 2; + auto p = lower_bound(arr.begin(), arr.end(), mid); + int sum = nums[p - arr.begin()] + (arr.end() - p) * left; + + if (sum < target) + { + res = mid; + mid += 1; + } + else + { + mid -= 1; + } + } + return res; +} \ No newline at end of file diff --git a/findContentChildren.cpp b/findContentChildren.cpp new file mode 100644 index 0000000..92d98c6 --- /dev/null +++ b/findContentChildren.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +// int findContentChildren(vector& g, vector& s) { +// sort(g.begin(), g.end()); +// sort(s.begin(), s.end()); +// int child = 0, cookie = 0, num = 0; +// while (child < g.size() && cookie < s.size()) +// { +// if (g[child] <= s[cookie]) +// { +// num++; +// child++; +// cookie++; +// } +// else +// { +// cookie++; +// } +// } +// return num; +// } + + +// int findContentChildren(vector& g, vector& s) { +// sort(g.begin(), g.end()); +// sort(s.begin(), s.end()); +// int res = 0; + +// for (int i = 0; i < s.size(); i++) +// { +// if (g[res] < s[i]) +// { +// res++; +// if (res == g.size()) +// { +// return g.size(); +// } + +// } +// } +// return res; +// } + +int findContentChildren(vector& g, vector& s) { + int j = 0; + int lens = g.size() < s.size() ? g.size() : s.size(); + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + for (int i = 0; i < lens; i++) + { + if (g[j] <= s[i]) + { + j++; + } + } + return j; +} + + + + + + + + + + + + + + + + diff --git a/findKthLargest.cpp b/findKthLargest.cpp new file mode 100644 index 0000000..e68077a --- /dev/null +++ b/findKthLargest.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +//ֱʹstlеpriority_queueȶ + //󶥶 + // priority_queue, less> q; + + // //С + // priority_queue, greater> p; + +int findKthLargest(vector& nums, int k) { + priority_queue, greater> a; + for (int i = 0; i < nums.size(); i++) + { + if (a.size() < k) + { + a.push(nums[i]); + } + else if (a.top() < nums[i]) + { + a.pop(); + a.push(nums[i]); + } + } + + return a.top(); +} \ No newline at end of file diff --git a/findLadders.cpp b/findLadders.cpp new file mode 100644 index 0000000..1378097 --- /dev/null +++ b/findLadders.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +vector> findLadders(string beginWord, string endWord, vector& wordList) { + + + } \ No newline at end of file diff --git a/findLength.cpp b/findLength.cpp new file mode 100644 index 0000000..9666430 --- /dev/null +++ b/findLength.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +//ڵķʽ +int find(vector& a, vector& b, int pa, int pb, int lens) +{ + int num = 0, res = 0; + for (int i = 0; i < lens; i++) + { + if (a[i + pa] == b[i + pb]) + { + num++; + } + else + { + num = 0; + } + res = max(res, num); + } + return res; +} + + // int find(vector& A, vector& B, int A_initial_position, int B_initial_position, int both_length) { + // int k = 0; + // int res = 0; + // for (int i = 0; i < both_length; i++) { + // if (A[A_initial_position + i] == B[B_initial_position + i]) + // k++; + // else k = 0; + // res = max(res, k); + // } + // return res; + // } + + +int findLength(vector& A, vector& B) { + int lens1 = A.size(), lens2 = B.size(); + int res = 0; + + for (int i = 0; i < lens1; i++) + { + int length = min(lens1 - i, lens2); + res = max(res, find(A, B, i, 0, length)); + } + + for (int j = 0; j < lens2; j++) + { + int length = min(lens2 - j, lens2); + res = max( res, find(A, B, 0, j, length)); + } + + return res; +} + + +int findLength2(vector& A, vector& B) { + int lens1 = A.size(), lens2 = B.size(); + int res = 0; + vector> dp(lens1 + 1, vector(lens2 + 1, 0)); + + for (int i = 1; i <= lens1; i++) + { + for (int j = 1; j <= lens2; j++) + { + if (A[i - 1] == B[j - 1]) + { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j] = 0; + } + res = max(res, dp[i][j]); + } + } + return res; +} \ No newline at end of file diff --git a/findMinArrowShots.cpp b/findMinArrowShots.cpp new file mode 100644 index 0000000..0eb538b --- /dev/null +++ b/findMinArrowShots.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +bool cmp(vector& a, vector& b) +{ + return a[0] < b[0]; +} + +int findMinArrowShots(vector>& points) { + if(points.empty()) return 0; + sort(points.begin(), points.end(), cmp); + int num = 1; + int ph = points[0][0], pr = points[0][0]; + + for (int i = 1; i < points.size(); i++) + { + if (points[i][0] < pr) + { + ph = points[i][0]; + if (points[i][1] < pr) + { + pr = points[i][1]; + } + } + else + { + num++; + ph = points[i][0]; + pr = points[i][1]; + } + } + return num; +} \ No newline at end of file diff --git a/findPath.cpp b/findPath.cpp new file mode 100644 index 0000000..8e691a3 --- /dev/null +++ b/findPath.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x): val(x),left(NULL),right(NULL){} +}; + +void findPath(TreeNode* root, int sum, vector>& path, int cursum) +{ + vector curpath; + cursum += root->val; + curpath.push_back(root->val); + bool leaf = (root->left == nullptr) && (root->right == nullptr); + if (cursum == sum && leaf) + { + path.push_back(curpath); + } + if (root->left != nullptr) + { + findPath(root->left, sum, path, cursum); + } + if (root->right != nullptr) + { + findPath(root->right, sum, path, cursum); + } + curpath.pop_back(); +} + +vector> pathSum(TreeNode* root, int sum) { + vector> path; + if (root == nullptr) + { + return path; + } + int curSum = 0; + findPath(root, sum, path, curSum); + return path; +} + +TreeNode* buildTree(TreeNode* &root, vector& nums, int lens, int i) +{ + if (i >= lens) + { + return root; + } + root = new TreeNode(nums[i]); + buildTree(root->left, nums, lens, i * 2 + 1); + buildTree(root->right, nums, lens, i * 2 + 2); + return root; +} + +TreeNode* Trees(TreeNode* root, vector& nums) +{ + int lens = nums.size(); + buildTree(root, nums, lens, 0); + return root; +} + +int main() +{ + // vector res; + vector nums = {5,4,8,11,NULL,13,4,7,2,NULL,NULL,5,1}; + TreeNode* root; + root = Trees(root, nums); + vector> res = pathSum(root, 22); + return 0; +} diff --git a/findPath.exe b/findPath.exe new file mode 100644 index 0000000..31a78b7 Binary files /dev/null and b/findPath.exe differ diff --git a/findRepeatedDnaSequences.cpp b/findRepeatedDnaSequences.cpp new file mode 100644 index 0000000..35e44c4 --- /dev/null +++ b/findRepeatedDnaSequences.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector findRepeatedDnaSequences(string s) { + map word_map; + vector res; + + for (int i = 0; i < s.length(); i++) + { + string tmp_str = s.substr(i, 10); + word_map[tmp_str]++; + } + + map::iterator it; + for(it = word_map.begin(); it != word_map.end(); it++) + { + if (it->second > 1) + { + res.push_back(it->first); + } + } + return res; +} \ No newline at end of file diff --git a/firstMissingPositive.cpp b/firstMissingPositive.cpp new file mode 100644 index 0000000..b867183 --- /dev/null +++ b/firstMissingPositive.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int firstMissingPositive(vector& nums) { + int n = nums.size(); + + for(auto& x : nums) + { + if (x <= 0) + { + x = n + 1; + } + } + + for (int i = 0; i < n; i++) + { + int tmp = abs(nums[i]); + if (tmp <= n) + { + nums[tmp - 1] = -abs(nums[tmp - 1]); + } + } + + for (int i = 0; i < n; i++) + { + if (nums[i] > 0) + { + return i + 1; + } + } + + return n + 1; +} \ No newline at end of file diff --git a/flatten.cpp b/flatten.cpp new file mode 100644 index 0000000..564ccce --- /dev/null +++ b/flatten.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +vector res; + +void preorder(TreeNode* root) +{ + if (!root) + { + return; + } + res.push_back(root); + preorder(root->left); + preorder(root->right); +} + +void flatten(TreeNode* root) { + preorder(root); + for (int i = 1; i < res.size(); i++) + { + res[i - 1]->left = nullptr; + res[i - 1]->right = res[i]; + } + return; +} + diff --git a/gasStation.cpp b/gasStation.cpp new file mode 100644 index 0000000..73277c0 --- /dev/null +++ b/gasStation.cpp @@ -0,0 +1,8 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + diff --git a/generateParenthesis.cpp b/generateParenthesis.cpp new file mode 100644 index 0000000..4df2ba6 --- /dev/null +++ b/generateParenthesis.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector res; + +void generate(int left, int right, string str) +{ + if (left == 0 && right == 0) + { + res.push_back(str); + return; + } + if (left > 0) + { + str += "("; + generate(left - 1, right, str); + } + if (left < right) + { + str += ")"; + generate(left, right - 1, str); + } + +} + +vector generateParenthesis(int n) { + if(!n) return res; + string str = ""; + generate(n, n, str); + return res; +} \ No newline at end of file diff --git a/getIntersectionNode.cpp b/getIntersectionNode.cpp new file mode 100644 index 0000000..42d79c4 --- /dev/null +++ b/getIntersectionNode.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +int getLens(ListNode* head) +{ + int lens = 0; + while (head) + { + lens++; + head = head->next; + } + return lens; +} + +ListNode* forward(ListNode* head, int lens) +{ + while (head && lens) + { + head = head->next; + lens--; + } + return head; +} + + +ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + int lensA = getLens(headA); + int lensB = getLens(headB); + + if (lensA < lensB) + { + headB = forward(headB, lensB - lensA); + } + else + { + headA = forward(headA, lensA - lensB); + } + + while (headA && headB) + { + if(headA == headB){ + return headA; + } + else + { + headA = headA->next; + headB = headB->next; + } + } + return nullptr; +} \ No newline at end of file diff --git a/getLeastNumbers.cpp b/getLeastNumbers.cpp new file mode 100644 index 0000000..b9430a3 --- /dev/null +++ b/getLeastNumbers.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; + +vector ress; +typedef multiset::iterator setIter; + +vector getLeastNumbers(vector& arr, int k) { + multiset res; + if (k < 1 || arr.size() < k) + { + ress.assign(res.begin(), res.end()); + return ress; + } + + for (vector::iterator iter = arr.begin(); iter != arr.end(); ++iter) + { + if (res.size() < k) + { + res.insert(*iter); + } + else + { + setIter iters = res.end(); + int c= *iters; + if (*iter < *iters) + { + res.erase(iters); + res.insert(*iter); + } + } + } + + ress.assign(res.begin(), res.end()); + return ress; +} + +int main() +{ + vector arr = {6,5,4}; + vector aa=getLeastNumbers(arr,2); + return 0; +} \ No newline at end of file diff --git a/getLeastNumbers.exe b/getLeastNumbers.exe new file mode 100644 index 0000000..fb918fd Binary files /dev/null and b/getLeastNumbers.exe differ diff --git a/getMin.cpp b/getMin.cpp new file mode 100644 index 0000000..ca3e482 --- /dev/null +++ b/getMin.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +bool cmp(pair& a, pair& b) +{ + return a.first > b.first; +} + +int getMin(int dis, int quant, vector>& stop) +{ + priority_queue q; + int res = 0; + stop.push_back({0, 0}); + sort(stop.begin(), stop.end(), cmp); + + for (int i = 0; i < stop.size(); i++) + { + int distance = dis - stop[i].first; + + while (!q.empty() && quant < distance) + { + quant += q.top(); + q.pop(); + res++; + } + + if (q.empty() && quant < distance) + { + return -1; + } + + quant = quant - distance; + dis = stop[i].first; + q.push(stop[i].second); + } + return res; +} + +int main(){ + std::vector > stop; + int N; + int L; + int P; + int distance; + int fuel; + scanf("%d", &N); + for (int i = 0; i < N; i++){ + scanf("%d %d", &distance, &fuel); + stop.push_back(std::make_pair(distance, fuel)); + } + scanf("%d %d", &L, &P); + printf("%d\n", getMin(L, P, stop)); + return 0; +} \ No newline at end of file diff --git a/getMin.exe b/getMin.exe new file mode 100644 index 0000000..fc80da9 Binary files /dev/null and b/getMin.exe differ diff --git a/groupAnagrams.cpp b/groupAnagrams.cpp new file mode 100644 index 0000000..8b44fca --- /dev/null +++ b/groupAnagrams.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector> groupAnagrams(vector& strs) { + map> maps; + vector> res; + + for(auto x : strs) + { + string tmp_str = x; + sort(tmp_str.begin(), tmp_str.end()); + + if (maps.find(tmp_str) == maps.end()) + { + vector tmp_num; + maps[tmp_str] = tmp_num; + } + maps[tmp_str].push_back(x); + } + + map>::iterator it; + for (it = maps.begin(); it != maps.end(); it++) + { + res.push_back((*it).second); + } + return res; +} \ No newline at end of file diff --git a/hasSubtree.cpp b/hasSubtree.cpp new file mode 100644 index 0000000..a6ab8d4 --- /dev/null +++ b/hasSubtree.cpp @@ -0,0 +1,49 @@ +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} + }; + +bool hasTree(TreeNode* a, TreeNode* b) +{ + if (b == nullptr) + { + return true; + } + if (a == nullptr) + { + return false; + } + if (!(a->val == b->val)) + { + return false; + } + return hasTree(a->left, b->left) && hasTree(a->right, b->right); +} + + +bool isSubStructure(TreeNode* A, TreeNode* B) { + bool res = false; + if (A != nullptr && B != nullptr) + { + if (A->val == B->val) + { + res = hasTree(A, B); + } + if (!res) + { + res = isSubStructure(A->left, B); + } + if (!res) + { + res = isSubStructure(A->right, B); + } + } + return res; +} + diff --git a/heapSort.cpp b/heapSort.cpp new file mode 100644 index 0000000..07e99f0 --- /dev/null +++ b/heapSort.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +using namespace std; + +// ǵݹ汾Ķ + +void heapify(vector& nums, int lens, int index) +{ + int left = 2 * index + 1; + while (left < lens) + { + int maxIndex = left + 1 < lens && nums[left + 1] < nums[left] ? left : left + 1; + maxIndex = nums[maxIndex] < nums[index] ? index : maxIndex; + if (maxIndex == index) + { + return; + } + swap(nums[maxIndex], nums[index]); + index = maxIndex; + left = 2 * index + 1; + } +} \ No newline at end of file diff --git a/invertTree.cpp b/invertTree.cpp new file mode 100644 index 0000000..9c65504 --- /dev/null +++ b/invertTree.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +TreeNode* invertTree(TreeNode* root) { + if (!root) + { + return root; + } + TreeNode* p = root; + TreeNode* tmp = p->left; + p->left = p->right; + p->right = tmp; + // if (p->left && p->right) + // { + // swap(p->left,p->right); + // } + // else if (p->left) + // { + // p->right = p->left; + // } + // else + // { + // p->left = p->right; + // } + if (p->left) + { + invertTree(p->left); + } + + if (p->right) + { + invertTree(p->right); + } + return root; +} + +TreeNode* invertTree(TreeNode* root) { + stack stack; + TreeNode* p = root; + if (root) + { + return root; + } + stack.push(root); + while (!stack.empty()) + { + p = stack.top(); + stack.pop(); + TreeNode* tmp = p->left; + p->left = p->right; + p->right = tmp; + if (p->left) + { + stack.push(p->left); + } + if(p->right) + { + stack.push(p->right); + } + } + return root; +} \ No newline at end of file diff --git a/isMatch.cpp b/isMatch.cpp new file mode 100644 index 0000000..b060ecf --- /dev/null +++ b/isMatch.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string str, pattern; +bool match(int i, int j){ + if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.') + { + return true; + } + + return false; +} + +bool isMatch(string s, string p) { + str = " " +s, pattern = " " + p; + int n = s.length(), m = p.length(); + vector> dp(n + 1, vector(m + 1, 0)); + dp[0][0] = true; + + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (match(i, j)) + { + dp[i][j] = dp[i - 1][j - 1]; + } + else if (pattern[j - 1] == '*') + { + if (match(i, j - 1)) + { + dp[i][j] = dp[i][j - 2] || dp[i - 1][j] || dp[i][j - 1]; + } + else + { + dp[i][j] = dp[i][j - 2]; + } + } + } + } + return dp[n][m]; +} + +bool isMatch(string s, string p) { + s=" "+s;//ֹð""\n"c*" + p=" "+p; + int m=s.size(),n=p.size(); + bool dp[m+1][n+1]; + vector> dp(n + 1, vector(m + 1, 0)); + dp[0][0]=true; + for(int i=1;i<=m;i++){ + for(int j=1;j<=n;j++){ + if(match(i, j)){ + dp[i][j]=dp[i-1][j-1]; + } + else if(p[j-1]=='*'){ + if(s[i-1]!=p[j-2] && p[j-2]!='.') + dp[i][j]=dp[i][j-2]; + else{ + dp[i][j]=dp[i][j-1] || dp[i][j-2] || dp[i-1][j]; + + } + } + } + } + return dp[m][n]; + } + + +bool isMatch(string s, string p) { + int m = s.size(); + int n = p.size(); + + auto matches = [&](int i, int j) { + if (i == 0) { + return false; + } + if (p[j - 1] == '.') { + return true; + } + return s[i - 1] == p[j - 1]; + }; + + vector> f(m + 1, vector(n + 1)); + f[0][0] = true; + for (int i = 0; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (p[j - 1] == '*') { + f[i][j] |= f[i][j - 2]; + if (matches(i, j - 1)) { + f[i][j] |= f[i - 1][j]; + } + } + else { + if (matches(i, j)) { + f[i][j] = f[i - 1][j - 1]; + } + } + } + } + return f[m][n]; + } + +int main() +{ + string a = "aab", b = "accca*b"; + isMatch(a, b); + return 0; +} \ No newline at end of file diff --git a/isMatch.exe b/isMatch.exe new file mode 100644 index 0000000..024246d Binary files /dev/null and b/isMatch.exe differ diff --git a/isNumber.cpp b/isNumber.cpp new file mode 100644 index 0000000..a443ff7 --- /dev/null +++ b/isNumber.cpp @@ -0,0 +1,144 @@ +#include +#include +using namespace std; + + +//㷨ʵʧˣʹָķʽʵboolֵָƶм +// +// bool flag = false; + +// string Unint(string s) +// { +// while (s[0] >= '0' && s[0] <= '9'){ +// s = s.substr(1); +// } +// return s; +// } + +// string ints(string &s) +// { +// if (s[0] == '+' || s[0] == '-') +// { +// s.substr(1); +// } +// while (s[0] >= '0' && s[0] <= '9'){ +// s.substr(1); +// } +// return s; +// } + +// bool compare_one(string one, string two) +// { +// return one.size() < two.size() ? true : false; +// } + +// bool compare_two(string one, string two) +// { +// return one.size() < two.size() - 1? true : false; +// } + +// string trim(string s) +// { +// if (s.empty()) +// { +// return s; +// } +// s.erase(0,s.find_first_not_of(" ")); +// s.erase(s.find_last_not_of(" ") + 1); +// return s; +// } + +// bool isNumber(string s) +// { +// s = trim(s); +// if (s.empty() || s.find(" ") != s.npos) +// { +// return false; +// } +// string tmp = ints(s); +// string tmp_two; +// bool numeric = compare_two(tmp, s); +// if (tmp[0] == '.') +// { +// tmp = tmp.substr(1); +// tmp_two = Unint(tmp); +// numeric = numeric || compare_one(tmp_two, tmp); +// tmp =tmp_two; +// } +// if (tmp[0] == 'e'|| tmp[0] == 'E') +// { +// tmp_two = tmp.substr(1); +// tmp = ints(tmp_two); +// numeric = numeric && compare_two(tmp, tmp_two); +// } +// return numeric && tmp.empty(); +// } + +int point = 0; + +bool Unint(string s) +{ + int p = point; + while (s[point] >= '0' && s[point] <= '9') + { + point++; + } + return point > p; +} + +bool ints(string s) +{ + int p = point; + if (s[point] == '+' || s[point] == '-') + { + point++; + } + return Unint(s); +} + +string trim(string s) +{ + if (s.empty()) + { + return s; + } + s.erase(0,s.find_first_not_of(" ")); + s.erase(s.find_last_not_of(" ") + 1); + if (s.find(" ") != s.npos) + { + s = ""; + } + return s; +} + + +bool isNumber(string s) +{ + s = trim(s); + if (s.empty()) + { + return false; + } + + bool numeric = ints(s); + + if (s[point] == '.') + { + point++; + numeric = Unint(s) || numeric; + } + if (s[point] == 'e'|| s[point] == 'E') + { + point++; + numeric = numeric && ints(s); + } + return numeric && (point == s.size()); +} + +int main() +{ + string s = "0.8"; + bool one = isNumber(s); + // cout<< s.substr(1); + return 0; +} diff --git a/isNumber.exe b/isNumber.exe new file mode 100644 index 0000000..f771096 Binary files /dev/null and b/isNumber.exe differ diff --git a/isPalindrome.cpp b/isPalindrome.cpp new file mode 100644 index 0000000..debba6b --- /dev/null +++ b/isPalindrome.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +bool isPalindrome(string s) { + string str = ""; + for (int i = 0; i < s.size(); i++) + { + if (isalnum(s[i])) + { + str += tolower(s[i]); + } + } + int left = 0, right = str.length() - 1; + while (left < right) + { + if(s[left] != s[right]) + { + return false; + } + left++; + right--; + } + return true; +} \ No newline at end of file diff --git a/isSymmetric.cpp b/isSymmetric.cpp new file mode 100644 index 0000000..6408f98 --- /dev/null +++ b/isSymmetric.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +bool isSymmetric(TreeNode* a, TreeNode* b) +{ + stack s1; + stack s2; + s1.push(a); + s2.push(b); + + bool res = false; + + while (!s1.empty() && !s2.empty()) + { + TreeNode* one = s1.top(); + s1.pop(); + TreeNode* two = s2.top(); + s2.pop(); + + if (one->val != two->val) + { + res = false; + return res; + } + + s1.push(a->left); + s1.push(a->right); + s2.push(b->right); + s2.push(b->left); + } + + res = s1.empty() && s2.empty(); + return res; +} \ No newline at end of file diff --git a/kthSmallest.cpp b/kthSmallest.cpp new file mode 100644 index 0000000..e68b567 --- /dev/null +++ b/kthSmallest.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct item +{ + int val; + int x; + int y; + item(int val, int x, int y) : val(val), x(x), y(y){}; + bool operator>(const item& a) const {return this->val > a.val;} + +}; + + +int kthSmallest1(vector>& matrix, int k) { + if (matrix.empty()) + { + return 0; + } + priority_queue, greater> queues; + + // matrixеһзȶУʵԶ + for (int i = 0; i < matrix.size(); i++) + { + queues.emplace(matrix[i][0], i, 0); + } + + //СӣСӡСǡСҲһ j + 1 + for (int j = 0; j < k - 1; j++) + { + item a = queues.top(); + queues.pop(); + if (a.y != matrix.size() - 1) //ֹԽ + { + queues.emplace(matrix[a.x][a.y + 1], a.x, a.y + 1); + } + } + //popk - 1һǶеtop + return queues.top().val; +} + + +bool check(vector>& nums, int mid, int n, int k) +{ + int i = n - 1; + int j = 0; + int num = 0; + while (i >= 0 && j < n) + { + if (nums[i][j] > mid) + { + i--; + } + else + { + j++; + num += i + 1; + } + } + return num >= k; +} + +int kthSmallest(vector>& matrix, int k) { + int rows = matrix.size(); + int cols = matrix[0].size(); + + int left = matrix[rows - 1][0]; + int right = matrix[0][cols - 1]; + int mid = left + ((right - left) >> 1); + + while (left <= right) + { + if (check(matrix, mid, cols, k)) + { + mid = right; + } + else + { + left = mid + 1; + } + } + return left; +} + + + + + +int main() +{ + vector> a = {{1,5,9},{10,11,13},{12,13,15}}; + kthSmallest1(a, 8); + return 0; +} \ No newline at end of file diff --git a/kthSmallest.exe b/kthSmallest.exe new file mode 100644 index 0000000..a38bda0 Binary files /dev/null and b/kthSmallest.exe differ diff --git a/ladderLength.cpp b/ladderLength.cpp new file mode 100644 index 0000000..564a306 --- /dev/null +++ b/ladderLength.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +map> graph; + +bool connect(string &a, string &b) +{ + int cnt = 0; + for (int i = 0; i < a.size(); i++) + { + if (a[i] != b[i]) + { + cnt++; + } + } + return cnt == 1; +} + +void construct(string beginWord, vector wordList) +{ + wordList.push_back(beginWord); + + for(auto x : wordList){ + graph[x] = vector(); + } + + for (int i = 0; i < wordList.size(); i++) + { + for (int j = i + 1; j < wordList.size(); j++) + { + if (connect(wordList[i], wordList[j])) + { + graph[wordList[i]].push_back(wordList[j]); + graph[wordList[j]].push_back(wordList[i]); + } + + } + } +} + +int bfs(string beginWord, string endWord) +{ + queue> Q; + set visit; + Q.push({beginWord, 1}); + visit.insert(beginWord); + + while (!Q.empty()) + { + string word = Q.front().first; + int step = Q.front().second; + Q.pop(); + if (word == endWord) + { + return step; + } + vector neibor = graph[word]; + + for (auto x : neibor) + { + if (visit.find(x) == visit.end()) + { + Q.push({x, step + 1}); + visit.insert(x); + } + } + } + return 0; +} + +int ladderLength(string beginWord, string endWord, vector& wordList) +{ + construct(beginWord, wordList); + return bfs(beginWord, endWord); +} \ No newline at end of file diff --git a/lengthOfLIS.cpp b/lengthOfLIS.cpp new file mode 100644 index 0000000..b23a3f6 --- /dev/null +++ b/lengthOfLIS.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +int lengthOfLIS1(vector& nums) { + if (nums.empty()) + { + return 0; + } + int res = 1; + vector dp(nums.size(), 0); + dp[0] = 1; + for (int i = 1; i < nums.size(); i++) + { + dp[i] = 1; + for (int j = 0; j < i; j++) + { + if (nums[j] < nums[i]) + { + dp[i] = max(dp[j] + 1, dp[i]); + } + } + cout<& nums) { + if (nums.empty()) + { + return 0; + } + vector tmp; + int res = 1; + tmp.push_back(nums[0]); + for (int i = 1; i < nums.size(); i++) + { + if (nums[i] > tmp.back()) + { + tmp.push_back(nums[i]); + } + else + { + for (int j = 0; j < tmp.size(); j++) + { + if (tmp[j] >= nums[i]) + { + tmp[j] = nums[i]; + break; + } + } + } + } + return tmp.size(); +} + + +int lengthOfLIS3(vector& nums) { + if (nums.empty()) + { + return 0; + } + int res = 0; + vector dp(nums.size()); + dp[0] = nums[0]; + for (int i = 1; i < nums.size(); i++) + { + dp[i] = 1; + for (int j = 0; j < i; j++) + { + if (nums[j] < nums[i]) + { + dp[i] = max(dp[i], dp[i - 1] + 1); + } + } + res = max(res, dp[i]); + } + return res; +} + +int binary_search(vector& res, int target) +{ + int begin = 0, end = res.size() - 1, index = -1; + + while (index == -1 && begin <= end) + { + int mid = (begin + end) / 2; + if (res[mid] >= target) + { + if (mid == 0 || target > res[mid - 1]) + { + index = mid - 1; + break; + } + else + { + end = mid - 1; + } + + } + else + { + if (mid == res.size() - 1 || target < res[mid + 1]) + { + index = mid + 1; + break; + } + else + begin = mid + 1; + } + } + return index; +} + +int lengthOfLIS(vector& nums) { + int lens = nums.size(); + if (lens < 2) + { + return lens; + } + vector res(lens, 0); + res.push_back(nums[0]); + for (int i = 0; i < lens; i++) + { + if (nums[i] > res.back()) + { + res.push_back(nums[i]); + } + else + { + int index = binary_search(res, nums[i]); + res[index] = nums[i]; + } + } + return res.size(); +} + + + + + + diff --git a/lengthOfLongestSubstring.cpp b/lengthOfLongestSubstring.cpp new file mode 100644 index 0000000..70edfbd --- /dev/null +++ b/lengthOfLongestSubstring.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int lengthOfLongestSubstring(string s) { + int begin = 0; + int res = 0; + string word = ""; + vector map(128, 0); + + for (int i = 0; i < s.length(); i++) + { + map[s[i]]++; + if (map[s[i]] == 1) + { + word += s[i]; + res = res > word.length() ? res : word.length(); + } + else + { + while (begin < i && map[s[i]] > 1) + { + map[s[begin]]--; + begin++; + } + + word = ""; + for (int j = begin; j <= i; j++) + { + word += s[j]; + } + } + } + return res; +} \ No newline at end of file diff --git a/levelOrder.cpp b/levelOrder.cpp new file mode 100644 index 0000000..568b3c9 --- /dev/null +++ b/levelOrder.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +void buildTree(TreeNode* &root, int* nums, int lens, int i) +{ + if (i >= lens) + { + return; + } + root = new TreeNode(nums[i]); + buildTree(root->left, nums, lens, 2 * i + 1); + buildTree(root->right, nums, lens, 2 * i + 2); +} + +vector levelOrder(TreeNode* root) { + vector res; + if (!root) + { + return res; + } + deque dequeNode; + dequeNode.push_back(root); + while (dequeNode.size()) + { + TreeNode * pNode = dequeNode.front(); + dequeNode.pop_front(); + res.push_back(pNode->val); + if (pNode->left) + { + dequeNode.push_back(pNode->left); + } + if (pNode->right) + { + dequeNode.push_back(pNode->right); + } + } + return res; +} + +vector> levelOrder2(TreeNode* root) { + vector> res; + if (root == nullptr) + { + return res; + } + int cur = 0; + int next = 0; + stack nodes[2]; + nodes[cur].push(root); + vector node; + while (!nodes[0].empty() || !nodes[1].empty()) + { + TreeNode* pNode = nodes[cur].top(); + nodes[cur].pop(); + node.push_back(pNode->val); + + if (cur == 0) + { + if (pNode->left != nullptr) + { + nodes[next].push(pNode->left); + } + if (pNode->right != nullptr) + { + nodes[next].push(pNode->right); + } + } + else{ + if (pNode->right != nullptr) + { + nodes[next].push(pNode->right); + } + if (pNode->left != nullptr) + { + nodes[next].push(pNode->left); + } + } + if (nodes[cur].empty()) + { + cur = 1 - cur; + next = 1 - next; + res.push_back(node); + node.clear(); + } + } + return res; +} + +int main() +{ + int nums[7] = {3,9,20,NULL,NULL,15,7}; + TreeNode* root; + buildTree(root, nums, 7, 0); + vector> res = levelOrder2(root); + return 0; +} \ No newline at end of file diff --git a/levelOrder.exe b/levelOrder.exe new file mode 100644 index 0000000..0587ff6 Binary files /dev/null and b/levelOrder.exe differ diff --git a/longestCommonPrefix.cpp b/longestCommonPrefix.cpp new file mode 100644 index 0000000..74f915c --- /dev/null +++ b/longestCommonPrefix.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +string findLongest(string& a, string& b) +{ + int i = 0 , j = 0; + int lens = min(a.size(), b.size()); + for (; i < lens && a[i] == b[j]; i++ , j++); + string res = a.substr(0, i); + return res; +} + +string longestCommonPrefix1(vector& strs) { + if(strs.empty()) return "";//˴return nullptrԣ + string str = strs[0]; + for (int i = 0; i < strs.size(); i++) + { + str = findLongest(str, strs[i]); + } + return str; +} + + +//ķʽÿַһ +string longestCommonPrefix(vector& strs) { + for (int i = 0; i < strs[0].size(); i++) + { + char c = strs[0][i]; + for (int j = 0; j < strs.size(); j++) //j0ͷһԼ + { + if (strs[j][i] != c || i == strs[j].size()) + { + return strs[0].substr(0, i); + } + } + } + return strs[0]; //Ϊstrs[0]ַʼ +} + diff --git a/longestCommonSubsequence.cpp b/longestCommonSubsequence.cpp new file mode 100644 index 0000000..f26985a --- /dev/null +++ b/longestCommonSubsequence.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string str1, str2; +int dp(int a, int b) +{ + if (a == -1 || b == -1) + { + return 0; + } + if (str1[a] == str2[b]) + { + return dp(a - 1, b - 1) + 1; + } + else + { + return max(dp(a - 1, b), dp(a, b - 1)); + } +} + +int longestCommonSubsequence2(string text1, string text2) { + if (text1.empty() || text2.empty()) + { + return 0; + } + str1 = text1, str2 = text2; + return dp(text1.length(), text2.length()); +} + + + +int longestCommonSubsequence(string text1, string text2) { + int lens1 = text1.length(), lens2 = text2.length(); + vector> dp(lens1, vector(lens2, 0)); + + for (int i = 1; i < lens1; i++) + { + for (int j = 1; j < lens2; j++) + { + if (text1[i - 1] == text2[j - 1]) + { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[lens1 - 1][lens2 - 1]; +} \ No newline at end of file diff --git a/longestPalindrome.cpp b/longestPalindrome.cpp new file mode 100644 index 0000000..367950a --- /dev/null +++ b/longestPalindrome.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int longestPalindrome(string s) { + vector maps(128, 0); + int res = 0; + int flag = 0; + + for (int i = 0; i < s.length(); i++) + { + maps[s[i]]++; + } + + for (int i = 0; i < maps.size(); i++) + { + if (maps[i] % 2 == 0) + { + res += maps[i]; + } + else + { + res += maps[i] - 1; + flag = 1; + } + } + return res + flag; +} \ No newline at end of file diff --git a/lowestCommonAncestor.cpp b/lowestCommonAncestor.cpp new file mode 100644 index 0000000..32bd775 --- /dev/null +++ b/lowestCommonAncestor.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +void preorder(TreeNode* root, TreeNode* aim, vector& path, vector& res, int flag) +{ + if (!root || flag) + { + return; + } + + path.push_back(root); + + if (root == aim) + { + flag = 1; + res = path; + } + + preorder(root->left, aim, path, res, flag); + preorder(root->right, aim, path, res, flag); + path.pop_back(); +} + +TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + vector p_path; + vector q_path; + vector tmp_path; + TreeNode* res; + + preorder(root, p, tmp_path, p_path, 0); + tmp_path.clear(); + preorder(root, q, tmp_path, q_path, 0); + + int lens = p_path.size() < q_path.size() ? p_path.size() : q_path.size(); + + for (int i = 0; i < lens; i++) + { + if (p_path[i] == q_path[i]) + { + res = p_path[i]; + } + } + return res; +} \ No newline at end of file diff --git a/majorityElement.cpp b/majorityElement.cpp new file mode 100644 index 0000000..57f38f5 --- /dev/null +++ b/majorityElement.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +using namespace std; + +bool check(vector& nums, int num) +{ + int time = 0; + bool res = false; + for (int i = 0; i < nums.size(); i++) + { + if (nums[i] == num) + { + time++; + } + if (time * 2 > nums.size()) + { + res = true; + } + + } + return res; +} + +int majorityElement(vector& nums) { + if (nums.empty()) + { + return 0; + } + int res = nums[0]; + int times = 1; + for (int i = 1; i < nums.size(); i++) + { + if (times == 0) + { + res = nums[i]; + times = 1; + } + else if (nums[i] == res) + { + times++; + } + else + { + times--; + } + } + if (!check(nums, res)) + { + res = 0; + } + return res; +} \ No newline at end of file diff --git a/makesquare.cpp b/makesquare.cpp new file mode 100644 index 0000000..64ad096 --- /dev/null +++ b/makesquare.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +bool generates(int i, int lens, vector& nums, vector& buck) +{ + if (i >= nums.size()) + { + return (buck[0] == lens && buck[1] == lens && buck[2] == lens && buck[3] == lens); + } + + for (int j = 0; j < 4; j++) + { + if (buck[j] + nums[i] > lens) + { + continue; + } + buck[j] += nums[i]; + if (generates(i + 1, lens, nums, buck)) + { + return true; + } + buck[j] -= nums[i]; + } + return false; +} + +bool makesquare(vector& nums) { + if (nums.empty()) + { + return false; + } + + int res = 0; + + for (int i = 0; i < nums.size(); i++) + { + res += nums[i]; + } + + if (res % 4) + { + return false; + } + + sort(nums.begin(), nums.end(), greater()); + + vector buck(4, 0); + + bool ans = generates(0, res / 4, nums, buck); + return ans; +} \ No newline at end of file diff --git a/matchCore.cpp b/matchCore.cpp new file mode 100644 index 0000000..b88e267 --- /dev/null +++ b/matchCore.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + + +bool matchCore(char* str, char* pattern) +{ + if (*str == '\0' && *pattern == '\0') + { + return true; + } + + if (*str != '\0' && *pattern == '\0') + { + return false; + } + + if (*(pattern + 1) == '*') + { + if (*pattern == *str || (*pattern == '.' && *str != '\0')) + { + return matchCore(str + 1, pattern + 2) //ܹʾܸ*ƥ + || matchCore(str + 1, pattern) // + || matchCore(str, pattern + 2); // + } + else + { + return matchCore(str, pattern + 2); + } + } + if (*str == *pattern || (*pattern == '.' && *str != '\0')) + { + return matchCore(str + 1, pattern + 1); + } + return false; +} + +bool matches(string s, string p) +{ + if (p.empty()) + { + return s.empty(); + } + + if (p.size() > 1 && p[1] == '*' ) + { + if (s[0] == p[0] || (p[0] == '.' && !s.empty())) + { + return matches(s.substr(1),p); + } + else + { + return matches(s, p.substr(2)); + } + } + if (s[0] == p[0] || (p[0] == '.' && !s.empty())) + { + return matches(s.substr(1), p.substr(1)); + } + return false; +} + + +bool match(char* str, char* pattern) +{ + if (str == nullptr || pattern == nullptr) + { + return false; + } + return matchCore(str, pattern); + +} + +bool isMatch(string s, string p) +{ + if (p.empty()) + { + return s.empty(); + } + + if (p.size() > 1 && p[1] == '*' ) + { + if (s[0] == p[0] || (p[0] == '.' && !s.empty())) + { + return isMatch(s.substr(1),p)||isMatch(s, p.substr(2)); + } + else + { + return isMatch(s, p.substr(2)); + } + } + if (s[0] == p[0] || (p[0] == '.' && !s.empty())) + { + return isMatch(s.substr(1), p.substr(1)); + } + return false; +} + +int main() +{ + string a = "aaaa"; + string b = "a*a"; + bool res = isMatch(a,b); + return 0; +} \ No newline at end of file diff --git a/matchCore.exe b/matchCore.exe new file mode 100644 index 0000000..a9728d8 Binary files /dev/null and b/matchCore.exe differ diff --git a/maxPathSum.cpp b/maxPathSum.cpp new file mode 100644 index 0000000..e69de29 diff --git a/maxProfit.cpp b/maxProfit.cpp new file mode 100644 index 0000000..f8d9f83 --- /dev/null +++ b/maxProfit.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int maxProfit2(vector& prices) { + int maxRes = 0; + for (int i = 0; i < prices.size() - 1; i++) + { + for (int j = i + 1; j < prices.size(); j++) + { + maxRes = max(maxRes, prices[j] - prices[i]); + } + } + return maxRes; +} + +int maxProfit(vector& prices){ + int minPrice = 99999999999; + int maxRes = 0; + for (int i = 0; i < prices.size(); i++) + { + if (prices[i] < minPrice) + { + minPrice = prices[i]; + } + + maxRes = max(prices[i] - minPrice, maxRes); + } + return maxRes; +} + +int maxSubArray(vector& nums) +{ + int pre = 0; + int maxRes = nums[0]; + for (int i = 0; i < nums.size(); i++) + { + pre = max(pre, pre + nums[i]); + maxRes = max(maxRes, pre); + } + return maxRes; +} + diff --git a/maxScoreSightseeingPair.cpp b/maxScoreSightseeingPair.cpp new file mode 100644 index 0000000..54c5161 --- /dev/null +++ b/maxScoreSightseeingPair.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +using namespace std; + +int maxScoreSightseeingPair(vector& A) { + int maxs = A[0]; + int sum = 0; + for (int i = 1; i < A.size(); i++) + { + sum = max(sum, maxs + A[i] - i); + maxs = max(maxs, A[i] + i); + } + return sum; +} \ No newline at end of file diff --git a/maxSubArray.cpp b/maxSubArray.cpp new file mode 100644 index 0000000..b458066 --- /dev/null +++ b/maxSubArray.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int maxSubArray1(vector& nums) { + if (nums.empty()) + { + return 0; + } + int res = nums[0], cur = nums[0]; + for (int i = 1; i < nums.size(); i++) + { + cur = max(cur + nums[i], nums[i]); + res = max(res, cur); + } + return res; +} + +int maxSubArray(vector& nums) { + if (nums.empty()) + { + return 0; + } + vector dp(nums.size() + 3); + dp[0] = nums[0]; + int res = nums[0]; + for (int i = 1; i < nums.size(); i++) + { + dp[i] = max(nums[i], nums[i] + dp[i - 1]); + res = max(dp[i - 1], dp[i]); + } + return res; +} \ No newline at end of file diff --git a/mergeKLists.cpp b/mergeKLists.cpp new file mode 100644 index 0000000..d8ae1b2 --- /dev/null +++ b/mergeKLists.cpp @@ -0,0 +1,218 @@ +#include +#include +#include +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + + +void heapify(deque& list, int lens, int index) +{ + if(index >= lens) return; + int left = 2 * index + 1; + if(left >= lens) return; + int maxIndex = left + 1 < lens && list[left + 1]->val < list[left]->val ? left + 1 : left; + maxIndex = list[maxIndex]->val < list[index]->val ? maxIndex : index; + if(maxIndex == index) return; + swap(list[maxIndex], list[index]); + heapify(list, lens, maxIndex); +} + +ListNode* mergeKLists(vector& lists) +{ + if(lists.empty()) return nullptr; + + deque queue; + + for (int i = 0; i < lists.size(); i++) + { + ListNode* p = lists[i]; + while (p) + { + queue.push_back(p); + p = p->next; + } + } + + for (int i = queue.size()/2 - 1; i >= 0; i--) + { + heapify(queue, queue.size(), i); + } + + ListNode *ph = nullptr, *pr = nullptr; + + while (queue.size() > 0) + { + ListNode* p = new ListNode(queue[0]->val); + if(!ph){ + ph = p; + } + else + { + pr->next = p; + } + pr = p; + queue.pop_front(); + if (!queue.empty()) + { + heapify(queue, queue.size(), 0); + } + } + return ph; +} +// void heapify(vector& list, int lens, int index) +// { +// if (index > lens) +// { +// return; +// } + +// int left = index * 2 + 1; + +// if (left > lens) +// { +// return; +// } + +// int maxIndex = left + 1 < lens && list[left + 1]->val < list[left]->val ? left + 1 : left; +// maxIndex = list[maxIndex]->val > list[index]->val ? index : maxIndex; +// if (maxIndex == index) +// { +// return; +// } +// swap(list[maxIndex], list[index]); +// heapify(list, lens, maxIndex); +// } + +// ListNode* mergeKLists(vector& list) +// { +// vector node; +// for (int i = 0; i < list.size(); i++) +// { +// ListNode* p = list[i]; +// while (p) +// { +// node.push_back(p); +// p = p->next; +// } +// } + +// int lens = node.size(); +// for (int i = lens/2 - 1; i >= 0; i--) +// { +// heapify(node, lens, i); +// } + +// ListNode *ph = nullptr, *pr = nullptr; + +// while (node.size() > 0) +// { +// ListNode* p = new ListNode(node[0]->val); +// int length = node.size(); +// if (ph == nullptr) +// { +// ph = p; +// } +// else +// { +// pr->next = p; +// } +// ph->next = pr; +// node[0] = node[0]->next; +// if (!node[0]) +// { +// swap(node[0], node[length - 1]); +// node.pop_back(); +// } +// if (!node.empty()) +// { +// heapify(node, length, 0); +// } +// } + +// return node[0]; +// } + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: +void heapify(vector& list, int lens, int index) +{ + if (index > lens) + { + return; + } + + int left = index * 2 + 1; + + if (left >= lens) + { + return; + } + + int maxIndex = (left + 1 < lens) && list[left + 1]->val < list[left]->val ? left + 1 : left; + + maxIndex = list[maxIndex]->val > list[index]->val ? index : maxIndex; + if (maxIndex != index) + { + swap(list[maxIndex], list[index]); + heapify(list, lens, maxIndex); + } +} + +ListNode* mergeKLists(vector& list) +{ + vector node; + for (int i = 0; i < list.size(); i++) + { + ListNode* p = list[i]; + while (p) + { + node.push_back(p); + p = p->next; + } + } + + for (int i = node.size()/2 - 1; i >= 0; i--) + { + heapify(node, node.size(), i); + } + + ListNode *ph = nullptr, *pr = nullptr; + + while (node.size() > 0) + { + ListNode* p = new ListNode(node[0]->val); + + if (ph == nullptr) + { + ph = p; + } + else + { + pr->next = p; + } + pr = p; + swap(node[0], node[node.size() - 1]); + node.pop_back(); + if (!node.empty()) + { + heapify(node, node.size(), 0); + } + } + + return ph; +} +}; \ No newline at end of file diff --git a/mergeSort.cpp b/mergeSort.cpp new file mode 100644 index 0000000..222d1d1 --- /dev/null +++ b/mergeSort.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +using namespace std; + +// void mergeVector(vector& vec1, vector& vec2, vector& res) +// { +// int i = 0, j = 0; +// int n = min(vec1.size(), vec2.size()); +// while (i < n && j < n) +// { +// if (vec1[i] < vec2[j]) +// { +// res.push_back(vec1[i++]); +// } +// else +// { +// res.push_back(vec2[j++]); +// } +// } + +// for (; i < vec1.size(); i++) +// { +// res.push_back(vec1[i]); +// } +// for (; j < vec2.size(); j++) +// { +// res.push_back(vec2[j]); +// } +// } + +void merge(vector& arr, int left, int mid, int right) +{ + vector tmp; + int i = left, j = mid + 1, index = 0; + + while (i <= mid && j <= right) + { + int num = arr[i] < arr[j] ? arr[i++] : arr[j++]; + tmp.push_back(num); + cout<< arr[i] << "---"<< arr[j]<< "---"<< i << "---"<< j <& arr, int left, int right) +{ + if (left == right) + { + return; + } + int mid = left + (right - left) / 2; + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + merge(arr, left, mid, right); +} + +// void mergeSort(vector& arr) +// { +// mergeSort(arr, 0, arr.size()); +// } + +int main() +{ + vector nums = {5,2,6}; + mergeSort(nums, 0, 2); + + for (int i = 0; i < nums.size(); i++) + { + cout<< nums[i]; + } + + return 0; +} \ No newline at end of file diff --git a/mergeSort.exe b/mergeSort.exe new file mode 100644 index 0000000..b59d5ce Binary files /dev/null and b/mergeSort.exe differ diff --git a/mergeTwoLists.cpp b/mergeTwoLists.cpp new file mode 100644 index 0000000..c9ffd1f --- /dev/null +++ b/mergeTwoLists.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + + +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode *res = nullptr; + ListNode* p = new ListNode(0); + res = p; + while (l1 && l2) + { + if (l1->val < l2->val) + { + res->next = l1; + l1 =l1->next; + } + else + { + res->next = l2; + l2 = l2->next; + } + res = res->next; + } + + res->next = l1 == nullptr? l2 : l1; + return p->next; +} + +ListNode* create_node(ListNode*& head, vector& nums) +{ + head = new ListNode(0); + ListNode* p = head; + for (int i = 0; i < nums.size(); i++) + { + ListNode* p2 = new ListNode(nums[i]); + p->next = p2; + p = p2; + } + return head; +} + +int main() +{ + ListNode *root, *p; + vector a = {1,2,4}; + vector b = {1,3,4}; + root = create_node(root, a); + p = create_node(p, b); + mergeTwoLists(root->next, p->next); + return 0; +} + +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + if (l1 == nullptr) + { + return l2; + } + if (l2 == nullptr) + { + return l1; + } + + ListNode* p_merge; + if (l1->val <= l2->val) + { + p_merge = l1; + p_merge->next = mergeTwoLists(l1->next,l2); + } + else + { + p_merge = l2; + p_merge->next = mergeTwoLists(l1, l2->next); + } + return p_merge; +} + +// ListNode* create_node(ListNode*& head, vector& nums) +// { +// head = new ListNode(0); +// ListNode* p = head; +// for (int i = 0; i < nums.size(); i++) +// { +// ListNode* p2 = new ListNode(nums[i]); +// p->next = p2; +// p = p2; +// } +// return head; +// } + +// int main() +// { +// vector nums = {1,2,4}; +// vector num2 = {1,3,4}; +// ListNode* p1 = create_node(p1, nums); +// ListNode* p2 = create_node(p2, nums); +// ListNode* p3 = mergeTwoLists(p1,p2); +// return 0; +// } \ No newline at end of file diff --git a/mergeTwoLists.exe b/mergeTwoLists.exe new file mode 100644 index 0000000..34a3423 Binary files /dev/null and b/mergeTwoLists.exe differ diff --git a/minDiffInBST.cpp b/minDiffInBST.cpp index 5a86e92..f280f9a 100644 --- a/minDiffInBST.cpp +++ b/minDiffInBST.cpp @@ -33,7 +33,7 @@ int inorder(TreeNode* r) int main() { - int nums[] = {4,2,6,1,3,NULL,NULL}; - TreeNode* nodes = nums; - int res = minDiffInBST(nodes); + // int nums[] = {4,2,6,1,3,NULL,NULL}; + // TreeNode* nodes = nums; + // int res = minDiffInBST(nodes); } \ No newline at end of file diff --git a/minSetSize.cpp b/minSetSize.cpp new file mode 100644 index 0000000..a414d00 --- /dev/null +++ b/minSetSize.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int minSetSize(vector& arr) { + map maps; + vector nums; + + for (int i = 0; i < arr.size(); i++) + { + maps[arr[i]]++; + } + + for (auto x : maps) + { + nums.push_back(x.second); + } + + sort(nums.begin(), nums.end(), greater()); + + int res = 0, tmp_num = 0; + + for (auto x : nums) + { + tmp_num += x; + res++; + if (tmp_num >= arr.size() / 2) + { + return res; + } + } + return res; +} \ No newline at end of file diff --git a/minSubArrayLen.cpp b/minSubArrayLen.cpp new file mode 100644 index 0000000..c4d5930 --- /dev/null +++ b/minSubArrayLen.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +// +// int minSubArrayLen(int s, vector& nums) { +// if (nums.empty()) +// { +// return 0; +// } +// sort(nums.begin(), nums.end(), greater()); + +// int res = 0, num = 0; +// for (int i = 0; i < nums.size(); i++) +// { +// if (res >= s) +// { +// return num; +// } +// else +// { +// res += nums[i]; +// num++; +// } +// } +// return num; +// } + +int minSubArrayLen(int s, vector& nums) { + if (nums.empty()) + { + return 0; + } + int begin = 0, ans = 0, sum = 0; + for (int i = 0; i < nums.size(); i++) + { + sum += nums[i]; + if (sum >= s) + { + ans = min(ans, i - begin + 1); + sum -= nums[begin]; + begin--; + } + } + return ans; +} + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/minWindow.cpp b/minWindow.cpp new file mode 100644 index 0000000..db89559 --- /dev/null +++ b/minWindow.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +bool check(vector s, vector t, vector& vec) +{ + for (int i = 0; i < vec.size(); i++) + { + if (s[vec[i]] < t[vec[i]]) + { + return false; + } + } + return true; +} + +string minWindow(string s, string t) { + vector map_s(128, 0); + vector map_t(128, 0); + vector vec; + + for (int i = 0; i < t.size(); i++) + { + map_t[t[i]]++; + } + + for (int i = 0; i < 128; i++) + { + if (map_t[i] > 0) + { + vec.push_back(i); + } + } + + + int begin = 0; + string res = ""; + + for (int i = 0; i < s.length(); i++) + { + map_s[s[i]]++; + while (begin < i) + { + if (map_t[s[begin]] == 0) + { + begin++; + } + else if(map_t[s[begin]] < map_s[s[begin]]) + { + map_s[s[begin]]--; + begin++; + } + else + { + break; + } + } + if (check(map_s, map_t, vec)) + { + if (res.length() > i - begin + 1 || (res.length() == 0)) + { + res = s.substr(begin, i - begin + 1); + } + } + } + + return res; +} \ No newline at end of file diff --git a/minimumTotal.cpp b/minimumTotal.cpp new file mode 100644 index 0000000..9f5774c --- /dev/null +++ b/minimumTotal.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int minimumTotal(vector>& triangle) { + int lens = triangle.size(); + if (!lens) + { + return 0; + } + vector> dp(lens, vector(triangle[0].size(), 0)); + for (int i = 0; i < triangle[lens - 1].size(); i++) + { + dp[lens - 1][i] = triangle[lens - 1][i]; + } + + for (int j = lens - 2; j >= 0; j++) + { + for (int i = 0; i < triangle[j].size(); i++) + { + dp[j][i] = min(dp[j + 1][i], dp[j + 1][i + 1]) + triangle[j][i]; + } + } + return dp[0][0]; +} \ No newline at end of file diff --git a/mirrorTree.cpp b/mirrorTree.cpp new file mode 100644 index 0000000..1fcb715 --- /dev/null +++ b/mirrorTree.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +/* void swaps(TreeNode* a, TreeNode* b) +{ + TreeNode* tmp = a; + b = a; + b = tmp; +} */ + +TreeNode* mirrorTree(TreeNode* root) { + if (root == nullptr) + { + return root; + } + if (root->left == nullptr && root->right == nullptr) + { + return root; + } + + swap(root->left, root->right); + + if (root->left) + { + mirrorTree(root->left); + } + if (root->right) + { + mirrorTree(root->right); + } + return root; +} + + +TreeNode* mirror(TreeNode* root) +{ + stack s; + s.push(root); + while (!s.empty()) + { + TreeNode* node = s.top(); + s.pop(); + if (node == nullptr) + { + break; + } + swap(node->left, node->right); + s.push(node->left); + s.push(node->right); + } + return root; +} + +TreeNode* buildTree(TreeNode* &root, vector& nums, int lens, int i) +{ + if (i >= lens) + { + return root; + } + root = new TreeNode(nums[i]); + buildTree(root->left, nums, lens, i * 2 + 1); + buildTree(root->right, nums, lens, i * 2 + 2); + return root; +} +// +int main() +{ + vector one = {4,2,7,1,3,6,9}; + TreeNode* root; + buildTree(root, one, one.size(), 0); + mirror(root); + return 0; +} + diff --git a/mirrorTree.exe b/mirrorTree.exe new file mode 100644 index 0000000..7c7e01f Binary files /dev/null and b/mirrorTree.exe differ diff --git a/nuimIslands.cpp b/nuimIslands.cpp new file mode 100644 index 0000000..27bc851 --- /dev/null +++ b/nuimIslands.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +using namespace std; + +queue> queues; +vector> map; +int n,m; + +void add(int x, int y, vector>& gra) +{ + if (x >= 0 && y >= 0 && y < m && x < n && map[x][y] == '1' && !gra[x][y]) + { + gra[x][y] = 1; + queues.push({x, y}); + } +} + +void floodfill(int i, int j, vector>& gra) +{ + queues.push({i, j}); + gra[i][j] = 1; + + while (!queues.empty()) + { + auto q = queues.front(); + queues.pop(); + int x = q.first, y = q.second; + add(x + 1, y, gra); + add(x, y + 1, gra); + add(x - 1, y, gra); + add(x, y - 1, gra); + } +} + +int numIslands(vector>& grid) { + n = grid.size(); + m = grid[0].size(); + int numRes = 0; + if (!n) + { + return 0; + } + + map = grid; + vector> gra(n, vector(m, 0)); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (grid[i][j] == '1' && !gra[i][j]) + { + floodfill(i, j, gra); + numRes++; + } + } + } + return numRes; +} + +int main() +{ + vector> nums = {{"1","1","1","1","0"},{"1","1","0","1","0"}}; + numIslands(nums); + return 0; +} \ No newline at end of file diff --git a/nuimIslands.exe b/nuimIslands.exe new file mode 100644 index 0000000..246ec95 Binary files /dev/null and b/nuimIslands.exe differ diff --git a/numIslands-BFS.cpp b/numIslands-BFS.cpp new file mode 100644 index 0000000..aa4c6d5 --- /dev/null +++ b/numIslands-BFS.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +using namespace std; + +vector> map; +queue> Q; +int rows, cols; + +void bfs(vector>& visit, int i, int j) +{ + const int dx[] = {-1, 1, 0, 0}; + const int dy[] = {0, 0, -1, 1}; + int tmp_x, tmp_y; + + Q.push({i, j}); + while (!Q.empty()) + { + int x = Q.front().first; + int y = Q.front().second; + Q.pop(); + + for (int i = 0; i < 4; i++) + { + tmp_x = dx[i] + x; + tmp_y = dy[i] + y; + + if (tmp_x >= 0 && tmp_x < rows && tmp_y >= 0 && tmp_y < cols) + { + if (!visit[tmp_x][tmp_y] && map[tmp_x][tmp_y] == '1') + { + Q.push({tmp_x, tmp_y}); + visit[tmp_x][tmp_y] = 1; + map[tmp_x][tmp_y] = '0'; + } + } + } + } +} + +int numIslands(vector>& grid) { + if (grid.empty()) + { + return 0; + } + + rows = grid.size(); + cols = grid[0].size(); + int res = 0; + vector> visit(rows, vector(cols, 0)); + map = grid; + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (map[i][j] == '1' && !visit[i][j]) + { + bfs(visit, i, j); + res++; + } + } + } + return res; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// queue> queues; +// vector> map; +// int n,m; + +// void add(int x, int y, vector>& gra) +// { +// if (x >= 0 && y >= 0 && y < m && x < n && map[x][y] == '1' && !gra[x][y]) +// { +// gra[x][y] = 1; +// queues.push({x, y}); +// } +// } + +// void floodfill(int i, int j, vector>& gra) +// { +// queues.push({i, j}); +// gra[i][j] = 1; + +// while (!queues.empty()) +// { +// auto q = queues.front(); +// queues.pop(); +// int x = q.first, y = q.second; +// add(x + 1, y, gra); +// add(x, y + 1, gra); +// add(x - 1, y, gra); +// add(x, y - 1, gra); +// } +// } + +// int numIslands(vector>& grid) { +// n = grid.size(); +// if (!n) +// { +// return 0; +// } +// m = grid[0].size(); +// int numRes = 0; +// map = grid; +// vector> gra(n, vector(m, 0)); + +// for (int i = 0; i < n; i++) +// { +// for (int j = 0; j < m; j++) +// { +// if (grid[i][j] == '1' && !gra[i][j]) +// { +// floodfill(i, j, gra); +// numRes++; +// } +// } +// } +// return numRes; +// } + +// int main() +// { +// return 0; +// } \ No newline at end of file diff --git a/numIslands-DFS.cpp b/numIslands-DFS.cpp new file mode 100644 index 0000000..15a3418 --- /dev/null +++ b/numIslands-DFS.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +using namespace std; + +// int n,m; +// void floodfill(vector>& grid, vector>& map, int i, int j) +// { +// map[i][j] = 1; +// if (i - 1 >= 0 && !map[i - 1][j] && grid[i - 1][j] == '1') +// { +// floodfill(grid, map, i - 1, j); +// } +// if (i + 1 < n && !map[i + 1][j] && grid[i + 1][j] == '1') +// { +// floodfill(grid, map, i + 1, j); +// } +// if (j - 1 >= 0 && !map[i][j - 1] && grid[i][j -1] == '1') +// { +// floodfill(grid, map, i, j - 1); +// } +// if (j + 1 < m && !map[i][j + 1] && grid[i][j + 1] == '1') +// { +// floodfill(grid, map, i, j + 1); +// } +// } + +// int numIslands(vector>& grid) +// { +// n = grid.size(); +// if (!n){ +// return 0; +// } +// m = grid[0].size(); +// int nums = 0; + +// vector> map(n, vector(m,0)); + +// for (int i = 0; i < n; i++) +// { +// for (int j = 0; j < m; j++) +// { +// if (grid[i][j] == '1' && !map[i][j]) +// { +// floodfill(grid, map, i, j); +// nums++; +// } +// } +// } +// return nums; +// } + + +int rows, cols; +vector> map; + +void dfs(vector>& visit, int i, int j) +{ + map[i][j] = '0'; + visit[i][j] = 1; + if (i + 1 < rows && !visit[i + 1][j] && map[i + 1][j] == '1') + dfs(visit, i + 1, j); + + if (i - 1 >= 0 && !visit[i - 1][j] && map[i - 1][j] == '1') + dfs(visit,i - 1, j); + + if (j + 1 < cols && !visit[i][j + 1] && map[i][j + 1] == '1') + dfs(visit, i, j + 1); + + if(j - 1 >= 0 && !visit[i][j - 1] && map[i][j - 1] == '1') + dfs(visit, i, j - 1); +} + +int numIslands(vector>& grid) { + rows = grid.size(); + cols = grid[0].size(); + int res = 0; + vector> visit(rows, vector(cols, 0)); + map = grid; + + if (!rows) + { + return 0; + } + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + if (!visit[i][j] && map[i][j] == '1') + { + dfs(visit, i, j); + res++; + } + } + } + + return res; +} \ No newline at end of file diff --git a/numIslands.exe b/numIslands.exe new file mode 100644 index 0000000..978ee2e Binary files /dev/null and b/numIslands.exe differ diff --git a/one/.vscode/c_cpp_properties.json b/one/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..02aa62a --- /dev/null +++ b/one/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "intelliSenseMode": "gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/one/.vscode/launch.json b/one/.vscode/launch.json new file mode 100644 index 0000000..e76a034 --- /dev/null +++ b/one/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示 + "type": "cppdbg", // 配置类型,这里只能为cppdbg + "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加) + "program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径 + "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可 + "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false + "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录 + "environment": [], + "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台 + "MIMode": "gdb", + "miDebuggerPath": "D:\\VS Code\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应 + "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/one/.vscode/settings.json b/one/.vscode/settings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/one/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/one/.vscode/tasks.json b/one/.vscode/tasks.json new file mode 100644 index 0000000..6a73db7 --- /dev/null +++ b/one/.vscode/tasks.json @@ -0,0 +1,24 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "g++", //这里注意一下,见下文 + "command": "D:\\VS Code\\mingw64\\bin\\g++.exe", + "args": [ + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "D:\\VS Code\\mingw64\\bin" + }, + "problemMatcher": [ + "$gcc" + ] + } + ] +} \ No newline at end of file diff --git a/one/buildTree.cpp b/one/buildTree.cpp new file mode 100644 index 0000000..510f728 --- /dev/null +++ b/one/buildTree.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +vector res; + +void buildTree(TreeNode* root, vector& nums, int lens, int i) +{ + if (i >= lens) + { + return; + } + root = new TreeNode(nums[i]); + buildTree(root->left, nums, lens, i << 1 + 1); + buildTree(root->right, nums, lens, i << 1 + 2); +} + + +void inorderTravel(TreeNode* root) +{ + if (root == NULL) + { + return; + } + + if (root->left != NULL) + { + inorderTravel(root->left); + } + + res.push_back(root->val); + + if (root->right != NULL) + { + inorderTravel(root->right); + } +} + +void Trees(TreeNode** root, vector& nums) +{ + int lens = nums.size(); + buildTree(*root, nums, lens, 0); +} + +int main() +{ + vector nums = {1, 2, 3, 4, 5, 6, 7, 8}; + TreeNode* root; + Trees(&root, nums); + return 0; +} \ No newline at end of file diff --git a/one/buildTree.exe b/one/buildTree.exe new file mode 100644 index 0000000..6d4686e Binary files /dev/null and b/one/buildTree.exe differ diff --git a/one/exchange.cpp b/one/exchange.cpp new file mode 100644 index 0000000..466b2e4 --- /dev/null +++ b/one/exchange.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +using namespace std; + +vector exchange(vector& nums) +{ + int lens = nums.size() - 1; + int head, tail = 0, lens; + while (head < tail) + { + while(head < tail && nums[head] & 1) head++; + while(head < tail && !(nums[tail] & 1)) tail--; + if(head < tail) swap(nums[head], nums[tail]); + } + return nums; +} \ No newline at end of file diff --git a/one/getKthFromEnd.cpp b/one/getKthFromEnd.cpp new file mode 100644 index 0000000..2f2e0c6 --- /dev/null +++ b/one/getKthFromEnd.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* getKthFromEnd(ListNode* head, int k) { + if (head == nullptr || k == 0) + { + return head; + } + ListNode* p1 = head; + ListNode* p2 = head; + for (int i = 0; i < k - 1; i++) + { + if (p1->next != nullptr) + { + p1 = p1->next; + } + } + while (p1->next != nullptr) + { + p1 = p1->next; + p2 = p2->next; + } + + return p2; +} \ No newline at end of file diff --git a/one/reverseList.cpp b/one/reverseList.cpp new file mode 100644 index 0000000..6df8692 --- /dev/null +++ b/one/reverseList.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +// ListNode* reverseList(ListNode* head) { +// ListNode* p = head; +// ListNode* p_pre = nullptr; +// ListNode* p_head = nullptr; +// while ( p != nullptr) +// { +// if (p->next == nullptr) +// break; +// ListNode* p_next = p->next; +// p->next = p_pre; +// p_pre = p; +// p = p_next; +// } +// return p; +// } + +ListNode* reverseList(ListNode* head) { + ListNode* p = head; + ListNode* p_pre = nullptr; + ListNode* p_head = nullptr; + while (p != nullptr) + { + if(p->next == nullptr){ + p_head = p; + } + ListNode* p_next = p->next; + p->next = p_pre; + p_pre = p; + p = p_next; + } + return p_head; +} \ No newline at end of file diff --git a/partition.cpp b/partition.cpp new file mode 100644 index 0000000..63a3e6c --- /dev/null +++ b/partition.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* partition(ListNode* head, int x) { + ListNode *head1 = nullptr, *head2 = nullptr, *tmp, *p; + if (!head) + { + return nullptr; + } + + while (head) + { + if (head->val < x) + { + if (head1 == nullptr) + { + head1 = head; + p = head1; + } + else + { + head1->next = head; + head1 = head; + } + + } + else + { + if (head2 == nullptr) + { + head2 = head; + tmp = head; + } + else + { + head2->next = head; + head2 = head; + } + } + head = head->next; + } + + if (head1 != nullptr && head2 != nullptr) + { + head1->next = tmp; + head2->next = nullptr; + } + else if(head1 == nullptr) + { + return tmp; + } + + return p; +} + + +int main() +{ + ListNode *root, *p; + vector nums = {1,1}; + root = new ListNode(nums[0]); + p = root; + for (int i = 1; i < nums.size(); i++) + { + ListNode* tmp = new ListNode(nums[i]); + p->next = tmp; + p = p->next; + } + partition(root,0); + return 0; +} \ No newline at end of file diff --git a/partition.exe b/partition.exe new file mode 100644 index 0000000..bd7c20d Binary files /dev/null and b/partition.exe differ diff --git a/pathSum.cpp b/pathSum.cpp new file mode 100644 index 0000000..6ebe1a0 --- /dev/null +++ b/pathSum.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +vector> res; +void preorder(TreeNode* root, int pathVal, vector path, int sum) +{ + if(!root) return; + + pathVal += root->val; + path.push_back(root->val); + + if (!root->left && !root->right && pathVal == sum) + { + res.push_back(path); + } + + preorder(root->left, pathVal, path, sum); + preorder(root->right, pathVal, path, sum); + + pathVal -= root->val; + path.pop_back(); + +} + +vector> pathSum(TreeNode* root, int sum) { + vector path; + preorder(root, 0, path, sum); + return res; +} \ No newline at end of file diff --git a/patternMatching.cpp b/patternMatching.cpp new file mode 100644 index 0000000..109449a --- /dev/null +++ b/patternMatching.cpp @@ -0,0 +1,257 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +bool patternMatching(string pattern, string value) +{ + vector cnt(2, 0); + for(auto i: pattern){ + cnt[i - 'a']++; + } + + int cntA = cnt[0], cntB = cnt[1]; + + if (cntA < cntB) { + swap(cntA, cntB); + for (char& ch: pattern) { + ch = (ch == 'a' ? 'b' : 'a'); + } + } + + if (value.empty()) + { + return cntB == 0; + } + + if (pattern.empty()) + { + return false; + } + + for (int i = 0, j = 0; i * cntA <= value.size(); i++) + { + int allB = value.size() - i * cntA; + + if ((cntB == 0 && allB == 0) || (cntB != 0 && allB % cntB == 0)) + { + int lenB = cntB == 0 ? 0 : allB / cntB; + string strA = "", strB = ""; + bool res = true; + j = 0; // ԭǸˣΪÿѭjᱻ0 + for(auto x : pattern) + { + if (x == 'a') + { + string strs = value.substr(j, i); + if(strA == "") + { + strA = strs; + } + else if(strA != strs) + { + res = false; + break; + } + j += i; + } + else + { + string strs = value.substr(j, lenB); + if (strB == "") + { + strB = strs; + } + else if(strB != strs) + { + res = false; + break; + } + j += lenB; + } + } + if (strA != strB && res) + { + return true; + } + } + } + return false; +} + +bool patternMatching2(string pattern, string value) { + int count_a = 0, count_b = 0; + for (char ch: pattern) { + if (ch == 'a') { + ++count_a; + } else { + ++count_b; + } + } + if (count_a < count_b) { + swap(count_a, count_b); + for (char& ch: pattern) { + ch = (ch == 'a' ? 'b' : 'a'); + } + } + if (value.empty()) { + return count_b == 0; + } + if (pattern.empty()) { + return false; + } + + for (int len_a = 0, j = 0; count_a * len_a <= value.size(); ++len_a) { + int rest = value.size() - count_a * len_a; + if ((count_b == 0 && rest == 0) || (count_b != 0 && rest % count_b == 0)) { + int len_b = (count_b == 0 ? 0 : rest / count_b); + int j = 0; + bool correct = true; + string value_a, value_b; + for (char ch: pattern) { + if (ch == 'a') { + string sub = value.substr(j, len_a); + if (!value_a.size()) { + value_a = move(sub); + } else if (value_a != sub) { + correct = false; + break; + } + j += len_a; + } else { + string sub = value.substr(j, len_b); + if (!value_b.size()) { + value_b = move(sub); + } else if (value_b != sub) { + correct = false; + break; + } + j += len_b; + } + } + if (correct && value_a != value_b) { + return true; + } + } + } + return false; + } + +int main() +{ + string a = "abba", b = "dogcatcatdog"; + bool res = patternMatching2(a, b); + return 0; +} + + + + + + + + + + + + + + + + +// bool check(string pattern, string value, int lenA, int lenB) +// { +// string strA = "", strB = ""; +// for (int i = 0, j = 0; i < pattern.size(); i++) +// { +// if (pattern[i] == 'a') +// { +// if (strA == "") +// { +// strA = value.substr(j, lenA); +// } +// else if(strA != value.substr(j, lenA)) +// { +// return false; +// } +// j += lenA; +// } + +// if (pattern[i] == 'b') +// { +// if (strB == "") +// { +// strB = value.substr(j, lenB); +// } +// else if(strB != value.substr(j, lenB)) +// { +// return false; +// } +// j += lenB; +// } +// } +// return true; +// } + +// bool help(string value, int num) +// { +// int valLength = value.length(); + +// if (valLength % num != 0) +// { +// return false; +// } + +// int length = value.size() / num; +// string str = value.substr(0, length); + +// for (int i = length; i < value.size(); i += length) +// { +// if (value.substr(i, length) != str) +// { +// return false; +// } +// } + +// return true; +// } + +// bool patternMatching(string pattern, string value) { +// int n = pattern.size(), m = value.size(); +// vector cnt(2, 0); +// for(auto i: pattern) +// cnt[i - 'a']++; + +// int lenA = cnt[0], lenB = cnt[1]; + +// if (!lenA) +// { +// return help(value, lenB); +// } +// else if(!lenB) +// { +// return help(value, lenA); +// } + +// for (int i = 1; i * lenA <= m - lenB ; i++) +// { +// int numB = (m - i * lenA) / lenB; +// if ((m - i * lenA) % lenB == 0) +// { +// continue; +// } +// if (check(pattern, value, i, numB)) +// { +// return true; +// } +// } + +// return false; +// } + + diff --git a/patternMatching.exe b/patternMatching.exe new file mode 100644 index 0000000..89ba205 Binary files /dev/null and b/patternMatching.exe differ diff --git a/permutation.cpp b/permutation.cpp new file mode 100644 index 0000000..7c8a9b7 --- /dev/null +++ b/permutation.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +using namespace std; + +set sets; + +void permutations(string& s, int point) +{ + if (point == s.size() - 1) + { + sets.insert(s); + } + else + { + for (int i = point; i < s.size(); ++i) + { + swap(s[point], s[i]); + permutations(s, point+1); + swap(s[point],s[i]); + } + } +} + +vector permutation(string& s) +{ + if (s.size() == 0) + { + vector res; + return res; + } + + int point = 0; + permutations(s, point); + + vector res(sets.begin(), sets.end()); + return res; +} + + +int main() +{ + string str = "abcd"; + string tmp = str.substr(0, 2); + vector res = permutation(str); + return 0; +} \ No newline at end of file diff --git a/permutation.exe b/permutation.exe new file mode 100644 index 0000000..a0b8807 Binary files /dev/null and b/permutation.exe differ diff --git a/preorderTraversal.cpp b/preorderTraversal.cpp new file mode 100644 index 0000000..729bdcc --- /dev/null +++ b/preorderTraversal.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +vector res; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +TreeNode* buildTree(TreeNode*& p, vector& nums, int lens, int i) +{ + if (i >= lens) + { + return p; + } + p = new TreeNode(nums[i]); + buildTree(p->left, nums, lens, i * 2 + 1); + buildTree(p->right, nums, lens, i * 2 + 2); + return p; +} + +TreeNode* createTree(TreeNode* p, vector& nums) +{ + p = buildTree(p, nums, nums.size(), 0); + return p; +} + +vector preorderTraversal2(TreeNode* p) { + if (p == nullptr){ + return res; + } + res.push_back(p->val); + preorderTraversal2(p->left); + preorderTraversal2(p->right); +} + +vector preorderTraversal(TreeNode* root) +{ + stack stacks; + TreeNode* p = root; + while (p != nullptr || !stacks.empty()) + { + while(p != nullptr){ + stacks.push(p); + res.push_back(p->val); + p = p->left; + } + + if (!stacks.empty()) + { + p = stacks.top(); + stacks.pop(); + p = p->right; + } + } + return res; +} + +vector inorderTraversal2(TreeNode* root) { + if (root == nullptr) + { + return res; + } + inorderTraversal2(root->left); + res.push_back(root->val); + inorderTraversal2(root->right); + return res; +} + +vector inorderTraversal(TreeNode* root){ + if (root == nullptr) + { + return res; + } + stack stack; + TreeNode* p = root; + while (p != nullptr || !stack.empty()) + { + while (p != nullptr) + { + stack.push(p); + p = p->left; + } + if (!stack.empty()) + { + + res.push_back(stack.top()->val); + stack.pop(); + p = p->right; + } + } + return res; +} + +vector postorderTraversal2(TreeNode* root) { + if (root == nullptr) + { + return res; + } + postorderTraversal2(root->left); + postorderTraversal2(root->right); + res.push_back(root->val); + return res; +} + +vector postorderTraversal3(TreeNode* root) +{ + stack stack; + TreeNode *p = root, *q = nullptr; + while (p != nullptr || !stack.empty()) + { + while (p != nullptr) + { + stack.push(p); + p = p->left; + } + + p = stack.top(); + + if(p->right == q || p->right == nullptr) + { + stack.pop(); + res.push_back(p->val); + q = p; + p = nullptr; + } + else + { + p = p->right; + } + } + return res; +} + +// 后序遍历:左右根 +// 前序遍历:根左右 + +vector postorderTraversal(TreeNode* root) +{ + stack stack; + TreeNode *p =root; + while (p || !stack.empty()) + { + while (p) + { + res.push_back(p->val); + stack.push(p); + p = p->right; + } + if (!stack.empty()) + { + p = stack.top(); + stack.pop(); + p = p->left; + } + } +} + +int main() +{ + vector nums; + int arr[11] = {0,1,2,3,4,NULL,6,NULL,NULL,9,10}; + for (int i = 0; i < 11; i++) + { + nums.push_back(arr[i]); + } + + TreeNode* root = createTree(root, nums); + postorderTraversal3(root); + + // preorderTraversal(root); + + // stack BtStack; + // bool a = !BtStack.empty(); + // cout<< a; + return 0; +} \ No newline at end of file diff --git a/preorderTraversal.exe b/preorderTraversal.exe new file mode 100644 index 0000000..18d16fd Binary files /dev/null and b/preorderTraversal.exe differ diff --git a/qqq.cpp b/qqq.cpp new file mode 100644 index 0000000..bd0694b --- /dev/null +++ b/qqq.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +using namespace std; + +int main() +{ + string str = "abcd"; + string tmp = str.substr(0, str.size()); + cout< +#include +#include +#include +using namespace std; + +int patitation(vector& nums, int left, int right) +{ + if(nums.empty()) return 0; + int i = left, j = right; + int tmp = nums[left]; + + while (i < j) + { + while(i < j && nums[j] > tmp) j--; + if( i < j) nums[i++] = nums[j]; + + while(i < j && nums[i] < tmp) i++; + if(i < j) nums[j--] = nums[i]; + } + nums[i] = tmp; + return i; +} + +void quickSort(vector& nums, int left, int right) +{ + if(nums.empty()) return; + if (left < right) + { + int index = patitation(nums, left, right); + quickSort(nums, left, index - 1); + quickSort(nums, index + 1, right); + } +} + +int main() +{ + vector num = {72,6,57,88,60,42,83,73,48,85}; + int nums[10] = {72,6,57,88,60,42,83,73,48,85}; + quickSort(num, 0, 9); + return 0; + +} \ No newline at end of file diff --git a/quickSort.exe b/quickSort.exe new file mode 100644 index 0000000..05b3cc5 Binary files /dev/null and b/quickSort.exe differ diff --git a/rebuildTree.cpp b/rebuildTree.cpp new file mode 100644 index 0000000..f56951e --- /dev/null +++ b/rebuildTree.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +struct TreeNode{ + int val; + TreeNode* left; + TreeNode* right; + TreeNode(int x): val(x),left(NULL),right(NULL){} +}; + +TreeNode* rebuildTree(vector& preorder, vector& inorder, + int preBegin, int preEnd, int inBegin, int inEnd){ + if (inBegin > inEnd) + { + return nullptr; + } + int index = 0; + + for(;preorder[preBegin] != inorder[index]; index++); + + TreeNode* root = new TreeNode(inorder[index]); + + int leftLens = index - inBegin; + + root->left = rebuildTree(preorder, inorder, preBegin + 1, preBegin + leftLens, inBegin, index - 1); + root->right =rebuildTree(preorder, inorder, preBegin + leftLens + 1, preEnd,index + 1, inEnd); + + return root; +} + +TreeNode* buildTree(vector& preorder, vector& inorder) { + if (preorder.empty() || inorder.empty()) + { + return nullptr; + } + TreeNode* root = rebuildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1); + return root; +} \ No newline at end of file diff --git a/recoverFromPreorder.cpp b/recoverFromPreorder.cpp new file mode 100644 index 0000000..f083797 --- /dev/null +++ b/recoverFromPreorder.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +TreeNode* recoverFromPreorder(string S) { + int depth = 0; + vector nodes(S.size()); + nodes[0] = new TreeNode(stoi(S.substr(0))); + for (int i = 0; i < S.size(); i++) + { + if (S[i] == '-') + { + depth++; + } + else if (depth > 0) + { + TreeNode* node = new TreeNode(stoi(S.substr(i))); + if (nodes[depth - 1]->left == nullptr) + { + nodes[depth - 1]->left = node; + } + else + { + nodes[depth - 1]->right = node; + } + nodes[depth] = node; + depth = 0; + } + } + return nodes[0]; +} \ No newline at end of file diff --git a/removeDuplicateNodes.cpp b/removeDuplicateNodes.cpp new file mode 100644 index 0000000..2e6e65e --- /dev/null +++ b/removeDuplicateNodes.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* removeDuplicateNodes(ListNode* head) { + if (!head) + { + return head; + } + + ListNode* p = head; + ListNode* last; + set res; + + while (p) + { + if (res.count(p->val)) + { + // auto tmp = p; + p = p->next; + last->next = p; + } + else + { + res.insert(p->val); + last = p; + p = p->next; + } + } + return head; +} + +/*λòܷı +ListNode* removeDuplicateNodes(ListNode* head) { + vector res; + ListNode* p = head; + while (p) + { + res.push_back(head->val); + p = p->next; + } + + sort(res.begin(), res.end()); + set tmp(res.begin(), res.end()); + vector nums(tmp.begin(), tmp.end()); + + for (int i = 1; i < nums.size(); i++) + { + p->val = nums[i]; + p = p->next; + } + return head; +}*/ diff --git a/removeKdigits.cpp b/removeKdigits.cpp new file mode 100644 index 0000000..b8e8cb5 --- /dev/null +++ b/removeKdigits.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string removeKdigits(string num, int k) { + stack stacks; + string res = ""; + + for (int i = 0; i < num.length(); i++) + { + int number = num[i] - '0'; + while (!stacks.empty() && stacks.top() > number && k > 0) + { + stacks.pop(); + k--; + } + if (number != 0 || !stacks.empty()) + { + stacks.push(number); + } + } + + while (!stacks.empty() && k > 0) + { + stacks.pop(); + k--; + } + + while (!stacks.empty()) + { + res.append(1, '0' + stacks.top()); + stacks.pop(); + } + + reverse(res.begin(), res.end()); + return res; +} + +int main() +{ + string a = "1432219"; + removeKdigits(a, 3); + return 0; +} \ No newline at end of file diff --git a/removeKdigits.exe b/removeKdigits.exe new file mode 100644 index 0000000..f184c66 Binary files /dev/null and b/removeKdigits.exe differ diff --git a/reverseList.cpp b/reverseList.cpp new file mode 100644 index 0000000..af53df8 --- /dev/null +++ b/reverseList.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct ListNode { + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +ListNode* reverseList(ListNode* head) { + stack stacks; + ListNode* p = head; + + while (p->next) + { + stacks.push(p->val); + p = p->next; + } + + ListNode *tmp; + tmp = head; + while (!stacks.empty()) + { + tmp->next = new ListNode(stacks.top()); + stacks.pop(); + tmp = tmp->next; + + } + return head; +} + + +// ListNode* reverseList(ListNode* head) { +// ListNode* new_head = nullptr; +// while (head) +// { +// ListNode *next = head->next; +// head->next = new_head; +// new_head = head; +// head = next; +// } +// return new_head; +// } + +ListNode* reverseBetween(ListNode* head, int m, int n) +{ + int changeLens = n - m + 1; + ListNode *changeHead = nullptr; + ListNode * resHead = head; + + while (head && --m) + { + changeHead = head; + head = head->next; + // m--; + } + + ListNode* tail = head; + ListNode* newHead = nullptr; + while (head && changeLens) + { + ListNode* next = head->next; + head->next = newHead; + newHead = head; + head = next; + changeLens--; + } + tail->next = head; + if (changeHead) + { + changeHead->next = newHead; + } + else + { + resHead = newHead; + } + + return resHead; +} + + + +int main() +{ + ListNode *root, *p; + vector nums = {1,2,3,4,5}; + root = new ListNode(nums[0]); + p = root; + for (int i = 1; i < nums.size(); i++) + { + ListNode* tmp = new ListNode(nums[i]); + p->next = tmp; + p = p->next; + } + reverseBetween(root,3,4); + return 0; +} \ No newline at end of file diff --git a/reverseList.exe b/reverseList.exe new file mode 100644 index 0000000..5b8f9fd Binary files /dev/null and b/reverseList.exe differ diff --git a/reviewSort.cpp b/reviewSort.cpp new file mode 100644 index 0000000..39e01eb --- /dev/null +++ b/reviewSort.cpp @@ -0,0 +1,46 @@ +#include +#include +using namespace std; + +void bubbleSort(vector& nums) +{ + int lens = nums.size(); + for (int i = lens - 1; i >= 0 ; i--) + { + for (int j = 0; j < i; j++) + { + if (nums[j] > nums[j + 1]) + { + swap(nums[j], nums[j + 1]); + } + } + } +} + +void bubbleSort_two(vector& nums) +{ + int lens = nums.size(); + for (int i = 0; i < lens - 1; i++) + { + for (int j = 0; j < lens - i - 1; j++) + { + if (nums[j] > nums[j + 1]) + { + swap(nums[j], nums[j + 1]); + } + } + } +} + +void insertSort(vector& nums) +{ + +} + + +int main() +{ + vector nums = {4,3,2,1}; + bubbleSort_two(nums); + return 0; +} \ No newline at end of file diff --git a/reviewSort.exe b/reviewSort.exe new file mode 100644 index 0000000..929f615 Binary files /dev/null and b/reviewSort.exe differ diff --git a/rightSideView.cpp b/rightSideView.cpp new file mode 100644 index 0000000..d456807 --- /dev/null +++ b/rightSideView.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +vector res; +vector rightSideView(TreeNode* root) { + if (!root) + { + return res; + } + queue> Q; + Q.push({root, 0}); + + while (!Q.empty()) + { + TreeNode* tmp = Q.front().first; + int dep = Q.front().second; + Q.pop(); + + if (dep == res.size()) + { + res.push_back(tmp->val); + } + else + { + res[dep] = tmp->val; + } + + if (tmp->left) + { + Q.push({tmp->left, dep + 1}); + } + if (tmp->right) + { + Q.push({tmp->right, dep + 1}); + } + } + + return res; +} \ No newline at end of file diff --git a/rob.cpp b/rob.cpp new file mode 100644 index 0000000..f6a3a57 --- /dev/null +++ b/rob.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int rob1(vector& nums) { + if (nums.empty()) + { + return 0; + } + vector dp(nums.size() + 3); + dp[0] = nums[0], dp[1] = max(nums[0], nums[1]); + for (int i = 2; i < nums.size(); i++) + { + + dp[i] = max(dp[i], nums[i] + dp[i - 2]); + } + return nums[nums.size() - 1]; +} + +int rob(vector& nums) { + if (nums.empty()) + { + return 0; + } + if (nums.size() == 1) + { + return nums[0]; + } + + vector dp(nums.size() + 3); + int pre = nums[0], cur = max(nums[0], nums[1]); + for (int i = 2; i < nums.size(); i++) + { + int tmp = cur; + cur = max(cur, pre + nums[i]); + pre = tmp; + } + return cur; +} + +int main() +{ + vector nums = {1,2,3,1}; + rob(nums); + cout<<"dasdads"; + return 0; +} \ No newline at end of file diff --git a/rob.exe b/rob.exe new file mode 100644 index 0000000..6a6cf5b Binary files /dev/null and b/rob.exe differ diff --git a/serialize.cpp b/serialize.cpp new file mode 100644 index 0000000..b9e5064 --- /dev/null +++ b/serialize.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +string serialize(TreeNode* root) +{ + ostringstream out; + queue q; + q.push(root); + while (!q.empty()) + { + TreeNode* tmp = q.front(); + q.pop(); + if(tmp) + { + out<< tmp->val << " "; + q.push(tmp->left); + q.push(tmp->right); + } + else + { + out<< "null "; + } + } + return out.str(); +} + +TreeNode* deserialize(string data) +{ + istringstream input(data); + string val; + vector res; + while (input >> val) + { + if(val == "null") + { + res.push_back(nullptr); + } + else + { + res.push_back(new TreeNode(stoi(val))); + } + } + + int j = 1; + for (int i = 0; j < res.size(); ++i) + { + if (res[i] == nullptr) + { + continue; + } + if (j < res.size()) + { + res[i]->left = res[j++]; + } + if (j < res.size()) + { + res[i]->right = res[j++]; + } + } + return res[0]; +} + +vector res; + + vector countSmaller(vector& vec) { + + if (vec.empty()){ + return {}; + } + + vector> nums; + for (int i = 0; i < vec.size(); i++){ + nums.emplace_back(vec[i], i); + } + + res = vector(vec.size(), 0); + merge_sort(nums, 0, (int)nums.size() - 1); + + return res; + } + + void merge_sort(vector>& nums, int left, int right){ + if (left < right){ + int mid = left + (right - left) / 2; + + merge_sort(nums, left, mid); + merge_sort(nums, mid + 1, right); + merge(nums, left, mid, right); + } + } + + void merge(vector>& nums, int left, int mid, int right){ + int i = left, j = mid + 1; + int k = left; + + vector> sort_nums; + + while (i <= mid && j <= right){ + if (nums[i].first <= nums[j].first){ + res[nums[i].second] += j - mid - 1; + sort_nums.push_back(nums[i++]); + }else if (nums[i].first > nums[j].first){ + sort_nums.push_back(nums[j++]); + } + } + + while (i <= mid){ + res[nums[i].second] += j - mid - 1; + sort_nums.push_back(nums[i++]); + } + + while (j <= right){ + sort_nums.push_back(nums[j++]); + } + + for (int m = 0, n = left; m < sort_nums.size(); m++, n++){ + nums[n] = sort_nums[m]; + } + } \ No newline at end of file diff --git a/solveNQueens.cpp b/solveNQueens.cpp new file mode 100644 index 0000000..72c6a53 --- /dev/null +++ b/solveNQueens.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +void putDown(vector>& map, int x, int y) +{ + int n = map.size(); + // int lx[8] = {0, -1, 0, 1, -1, -1, 1, 1}; + // int ly[8] = {1, 0, -1, 0, 1, -1, 1, -1}; + const int lx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; + const int ly[] = {0, 0, -1, 1, -1, 1, -1, 1}; + // map[x][y] = 1; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < 8; j++) + { + int tmpX = x + i * lx[j]; + int tmpY = y + i * ly[j]; + + if(tmpX >= 0 && tmpX < n && tmpY >= 0 && tmpY < n) + map[tmpX][tmpY] = 1; + } + + } +} + +void solve(vector>& map, vector>& res, + vector& tmp, int k, int n) +{ + if (k == n) + { + res.push_back(tmp); + return; + } + + for (int i = 0; i < n; i++) + { + if (!map[k][i]) + { + tmp[k][i] = 'Q'; + cout<< i <<"------"<> tmpMap = map; + putDown(map, k, i); + solve(map, res, tmp, k + 1, n); + map = tmpMap; + tmp[k][i] = '.'; + } + } +} + +vector> solveNQueens(int n) { + vector> map(n, vector(n, 0)); + vector tmp(n); + for (int i = 0; i < n; i++) + { + tmp[i].append(n, '.'); + } + // cout<> res; + solve(map, res, tmp, 0 ,n); + return res; +} \ No newline at end of file diff --git a/sortArray.cpp b/sortArray.cpp new file mode 100644 index 0000000..7689994 --- /dev/null +++ b/sortArray.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +using namespace std; + +//ݹ +// void heapify(vector& nums, int lens, int index) +// { +// if (index > lens) +// { +// return; +// } + +// int left = index * 2 + 1; +// if (left >= lens) +// { +// return; +// } + +// int maxIndex = (left + 1 < lens) && nums[left + 1] > nums[left] ? left + 1 : left; +// maxIndex = nums[maxIndex] > nums[index] ? maxIndex : index; //һ + +// if (maxIndex == index) +// { +// return; +// } + +// swap(nums[maxIndex], nums[index]); +// heapify(nums, lens, maxIndex); +// } + + +//ǵݹ +void heapify(vector& nums, int lens, int index) +{ + int left = 2 * index + 1; + while (left < lens) + { + // int maxIndex = left + 1 < lens && nums[left + 1] > nums[left] ? left + 1: left; + int maxIndex = left + 1 < lens && nums[left + 1] < nums[left] ? left : left + 1; + // һ nums[left + 1] < nums[left] ? left : left + 1 ǰ˳Ӧ˳һ + maxIndex = nums[maxIndex] > nums[index] ? maxIndex : index; + if (maxIndex == index) + { + break; + } + swap(nums[maxIndex], nums[index]); + index = maxIndex; + left = 2 * index + 1; + } +} + +void heapSort(vector& nums) +{ + int lens = nums.size(); + for (int i = lens/2 - 1; i >= 0; i--) + { + heapify(nums, lens, i); + } + + for (int i = lens - 1; i > 0; i--) //ڶδ + { + swap(nums[i], nums[0]); + heapify(nums, i, 0); + } + +} + +vector sortArray(vector& nums) { + heapSort(nums); + return nums; +} + +int main() +{ + int arr[5] = {5,4,3,2,1}; + vector nums; + for (int i = 0; i < 5; i++) + { + nums.push_back(arr[i]); + } + heapSort(nums); + for (int i = 0; i < 5; i++) + { + printf("%d",nums[i]); + } + + return 0; +} \ No newline at end of file diff --git a/sortArray.exe b/sortArray.exe new file mode 100644 index 0000000..f462105 Binary files /dev/null and b/sortArray.exe differ diff --git a/sortedArrayToBST.cpp b/sortedArrayToBST.cpp new file mode 100644 index 0000000..5ecf3ad --- /dev/null +++ b/sortedArrayToBST.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +void buildTree(vector& nums, TreeNode*& root, int lens, int index) +{ + if (index >= lens) + { + return; + } + root = new TreeNode(nums[index]); + buildTree(nums, root->left, lens, index * 2 + 1); + buildTree(nums, root->right, lens, index * 2 + 2); +} + +TreeNode* createTree(vector& nums, TreeNode*& root, int left, int right) +{ + if (left > right) + { + return nullptr; + } + + int mid = (left + right) / 2; + root = new TreeNode(nums[mid]); + root->left = createTree(nums, root, left, mid - 1); + root->right = createTree(nums, root, mid + 1, right); + return root; +} + +TreeNode* sortedArrayToBST(vector& nums) { + if (nums.empty()) + { + return nullptr; + } + + TreeNode* root; + root = createTree(nums, root, 0, nums.size() - 1); + return root; +} \ No newline at end of file diff --git a/spiralOrder.cpp b/spiralOrder.cpp new file mode 100644 index 0000000..a3e5053 --- /dev/null +++ b/spiralOrder.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +vector res; +void printm(vector>& nums, int rows, int cols, int i) +{ + //iܱܲΪiȦ + int endx = cols - 1 - i; + int endy = rows - 1 - i; + // + for (int j = i; j <= endx; j++) + { + res.push_back(nums[i][j]); + } + //ұߣϵ´ӡ + if (i < endy) + { + for (int j = i + 1; j <= endy; j++) + { + res.push_back(nums[j][endx]); + } + } + //ұߣҵ + if (i < endx && i < endy) + { + for (int j = endx - 1; j >= i; j--) + { + res.push_back(nums[endy][j]); + } + } + + //µ + if (i < endx && i < endy - 1) + { + for (int j = endy - 1; j > i; j--) + { + res.push_back(nums[j][i]); + } + } +} + + +vector spiralOrder(vector>& matrix) { + int rows = matrix.size(); + int cols = matrix[0].size(); + + if (matrix.empty()) + { + return; + } + + int i = 0; + + while(cols > i * 2 && rows > i * 2){ + printm(matrix, rows, cols, i); + i++; + } + return res; +} diff --git a/subsets.cpp b/subsets.cpp new file mode 100644 index 0000000..532dca8 --- /dev/null +++ b/subsets.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector> res; + +//ʵַʽ +void generate(vector& nums, int i, vector& item) +{ + if (i > nums.size()) + { + return; + } + + item.push_back(nums[i]); + res.push_back(item); + generate(nums, i + 1, item); + item.pop_back(); + generate(nums, i + 1, item); +} + +vector> subsets1(vector& nums) { + vector item; + res.push_back(item); + generate(nums, 0, item); + return res; +} + + +//λķʽʵ +vector> subsets(vector& nums) { + + for (int i = 0; i < (1< item; + for (int j = 0; j < nums.size(); j++) + { + if ((i>>j) & 1) + { + item.push_back(nums[i]); + } + } + res.push_back(item); + } + return res; +} + diff --git a/subsetsWithDup.cpp b/subsetsWithDup.cpp new file mode 100644 index 0000000..422fe8e --- /dev/null +++ b/subsetsWithDup.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector> res; + +vector> subsets(vector& nums, int i, vector& item) +{ + if (i >= nums.size()) + { + return res; + } + item.push_back(nums[i]); + res.push_back(item); + subsets(nums, i + 1, item); + item.pop_back(); + subsets(nums, i + 1, item); +} + +bool adds(vector& nums, int target) +{ + int sum = 0; + for (int i = 0; i < nums.size(); i++) + { + sum += nums[i]; + } + if (sum == target) + { + return true; + } + return false; +} + +vector> subsetsWithDup(vector& nums) { + sort(nums.begin(), nums.end()); + vector item; + res.push_back(item); + subsets(nums, 0, item); + set> ans(res.begin(), res.end()); + vector> anwser(ans.begin(), ans.end()); + return anwser; +} + + +vector> combinationSum2(vector& nums, int target) { + sort(nums.begin(), nums.end()); + vector item; + res.push_back(item); + subsets(nums, 0, item); + set> ans(res.begin(), res.end()); + vector> answer(ans.begin(), ans.end()); + vector> a; + for (int i = 0; i < answer.size(); i++) + { + if (adds(answer[i], target)) + { + a.push_back(answer[i]); + } + } + + return a; +} \ No newline at end of file diff --git a/substr.cpp b/substr.cpp new file mode 100644 index 0000000..2e3c4bb --- /dev/null +++ b/substr.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +using namespace std; + +int main() +{ +string as = "12345asdf"; +string a = as.substr(0,5); //ַsдӵ0λʼijΪ5ַ + return 0; +} \ No newline at end of file diff --git a/tempCodeRunnerFile.cpp b/tempCodeRunnerFile.cpp new file mode 100644 index 0000000..2cf6bee --- /dev/null +++ b/tempCodeRunnerFile.cpp @@ -0,0 +1,34 @@ +struct ListNode { +// int val; +// ListNode *next; +// ListNode(int x) : val(x), next(NULL) {} +// }; + +// ListNode* reverseList(ListNode* head) { +// stack stacks; +// ListNode* p = head; +// while (p) +// { +// stacks.push(p); +// p = p->next; +// } + +// head = stacks.top(); +// stacks.pop(); +// ListNode* tmp; +// // head->next = tmp; +// while (!stacks.empty()) +// { +// if (tmp->next == nullptr) +// { +// head->next = stacks.top(); +// tmp = head->next; +// } +// else +// { +// tmp->next = stacks.top(); +// tmp = tmp->next; +// } +// stacks.pop(); +// } +// } \ No newline at end of file diff --git a/test3.cpp b/test3.cpp new file mode 100644 index 0000000..bdac67e --- /dev/null +++ b/test3.cpp @@ -0,0 +1,62 @@ +#include +#include +using namespace std; + +bool verifys(vector& nums, int lens) +{ + if (nums.empty() || lens <= 0) + { + return false; + } + + int root = nums[lens - 1]; + int i = 0; + for (; i < lens - 1; ++i) + { + if (nums[i] > root) + { + break; + } + } + + int j = i; + + for (; j < lens - 1; ++j) + { + if (nums[j] < root) + { + return false; + } + } + + bool left = true; + if (i > 0) + { + left = verifys(nums, i); + } + + bool right = true; + if (i < lens - 1) + { + vector tmp(nums.begin() + i, nums.end()); + right = verifys(tmp, lens - i - 1); + } + return left && right; +} + +bool verifyPostorder(vector& postorder) { + bool res = verifys(postorder, postorder.size()); + return res; +} + +int main() +{ + int nums[5] = {1,3,2,6,5}; + vector res; + for (int i = 0; i < 5; i++) + { + res.push_back(nums[i]); + } + bool re = verifyPostorder(res); + return 0; +} diff --git a/test3.exe b/test3.exe new file mode 100644 index 0000000..29a9e5f Binary files /dev/null and b/test3.exe differ diff --git a/threeSumClosest.cpp b/threeSumClosest.cpp new file mode 100644 index 0000000..c281c90 --- /dev/null +++ b/threeSumClosest.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int threeSumClosest(vector& nums, int target) +{ + int lens = nums.size(); + int sum = nums[0] + nums[1] + nums[2]; + int res = abs(sum - target); + int ans = sum; + + if (lens == 3) + { + return ans; + } + + sort(nums.begin(), nums.end()); + for (int i = 0; i < lens - 2; i++) + { + int front = i + 1; + int rear = lens - 1; + + while (front < rear) + { + sum = nums[i] + nums[front] + nums[rear]; + int mins = abs(sum - target); + + if (mins < res) + { + res = mins; + ans = sum; + } + + if (sum < target) + { + front++; + } + else if(sum > target) + { + rear--; + } + else + { + return ans; + } + } + } + return ans; +} \ No newline at end of file diff --git a/trapRainWater.cpp b/trapRainWater.cpp new file mode 100644 index 0000000..c7f04e3 --- /dev/null +++ b/trapRainWater.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct item +{ + int x; + int y; + int h; + item(int a, int b, int c) : x(a), y(b), h(c){}; +}; + +struct cmp +{ + bool operator()(const item &a, const item &b) + { + return a.h > b.h; + } +}; + +int rows, cols; +priority_queue, cmp> Q; + + +void construct(vector>& map, vector>& visit) +{ + for (int i = 0; i < rows; i++) + { + Q.push(item(i, 0, map[i][0])); + Q.push(item(i, cols - 1, map[i][cols - 1])); + visit[i][0] = 1; + visit[i][cols - 1] = 1; + } + + for (int j = 0; j < cols; j++) + { + Q.push(item(0, j, map[0][j])); + Q.push(item(rows - 1, j, map[rows - 1][j])); + visit[0][j] = 1; + visit[rows - 1][j] = 1; + } +} + +int trapRainWater(vector>& heightMap) { + rows = heightMap.size(); + if (!rows) + { + return 0; + } + cols = heightMap[0].size(); + + vector> visit(rows, vector(cols, 0)); + + const int dx[] = {-1, 1, 0, 0}; + const int dy[] = {0, 0, -1, 1}; + int res = 0; + + construct(heightMap, visit); + + while (!Q.empty()) + { + int x = Q.top().x; + int y = Q.top().y; + int h = Q.top().h; + + Q.pop(); + + for (int i = 0; i < 4; i++) + { + int tmp_x = x + dx[i]; + int tmp_y = y + dy[i]; + + if (tmp_x >= 0 && tmp_x < rows && tmp_y >= 0 && tmp_y < cols && !visit[tmp_x][tmp_y]) + { + if (h > heightMap[tmp_x][tmp_y]) + { + res += h - heightMap[tmp_x][tmp_y]; + heightMap[tmp_x][tmp_y] = h; + } + Q.push(item(tmp_x, tmp_y, heightMap[tmp_x][tmp_y])); + visit[tmp_x][tmp_y] = 1; + } + } + } + + return res; +} \ No newline at end of file diff --git a/treeToDoublyList.cpp b/treeToDoublyList.cpp new file mode 100644 index 0000000..4a2d8d6 --- /dev/null +++ b/treeToDoublyList.cpp @@ -0,0 +1,98 @@ +#include +#include +using namespace std; + +struct Node +{ + int val; + Node* left; + Node* right; + Node(int x):val(x),left(NULL),right(NULL){} +}; + +void listTree(Node* root, Node*& head, Node*& tail) +{ + if (root == nullptr) + { + return; + } + + Node *Lhead, *Ltail, *Rhead, *Rtail; + Lhead = root; + + if (root->left != nullptr) + { + listTree(root->left, Lhead, Ltail); + root->left = Ltail; + Ltail->right = root; + } + + Rtail = root; + + if (root->right != nullptr) + { + listTree(root->right, Rhead, Rtail); + root->right = Rhead; + Rhead->left = root; + } + head = Lhead; + tail = Rtail; +} + + +Node* treeToDoublyList(Node* root) +{ + if (root == nullptr) + { + return nullptr; + } + + Node *head, *tail; + listTree(root, head, tail); + head->left = tail; + tail->right = head; + return head; +} + + +// void convertNode(Node* point, Node** nodeList) +// { +// if (point == nullptr) +// { +// return; +// } + +// Node* curPoint = point; + +// if (curPoint->left != nullptr) +// { +// convertNode(curPoint->left, nodeList); +// } + +// curPoint->left = *nodeList; + +// if (*nodeList != nullptr) +// { +// (*nodeList)->right = curPoint; +// } + +// *nodeList = curPoint; + +// if (curPoint->right != nullptr) +// { +// convertNode(curPoint->right, nodeList); +// } +// } + +// Node* treeToDoublyList(Node* root) { +// Node* list = nullptr; +// convertNode(root, &list); +// Node* headList = list; +// while (headList != nullptr && headList->left != nullptr) +// { +// headList = headList->left; +// } +// return headList; +// } + + diff --git a/verifyPostorder.exe b/verifyPostorder.exe new file mode 100644 index 0000000..2952039 Binary files /dev/null and b/verifyPostorder.exe differ diff --git a/wiggleMaxLength.cpp b/wiggleMaxLength.cpp new file mode 100644 index 0000000..355520f --- /dev/null +++ b/wiggleMaxLength.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +// int wiggleMaxLength(vector& nums) { +// int lens = nums.size(); +// if (lens < 2) +// { +// return lens; +// } +// static const int BEGIN = 0; +// static const int UP = 1; +// static const int DOWN = 2; +// int STATE = BEGIN; +// int length = 1; + +// for (int i = 1; i < lens; i++) +// { +// switch (STATE) +// { +// case BEGIN: +// if(nums[i] < nums[i + 1]) +// { +// STATE = UP; +// length++; +// } +// else if(nums[i] > nums[i + 1]) +// { +// STATE = DOWN; +// length++; +// } +// break; +// case UP: +// if (nums[i] < nums[i + 1]) +// { +// STATE = DOWN; +// length++; +// } +// break; +// case DOWN: +// if (nums[i] > nums[i + 1]) +// { +// STATE = UP; +// length++; +// } +// break; +// } +// } +// return length; +// } + + +int wiggleMaxLength(vector& nums) { + int up = 1, down = 1; + int lens = nums.size(); + if (lens < 2) + { + return lens; + } + + for (int i = 1; i < nums.size(); i++) + { + if (nums[i - 1] < nums[i]) + { + up = down + 1; + } + else if (nums[i - 1] > nums[i]) + { + down = up + 1; + } + } + return max(up, down); +} + +//״̬ +int wiggleMaxLength(vector& nums) { + int lens = nums.size(); + if(lens < 2) return lens; + int res = 1; + + const int BEGIN = 0; + int state = BEGIN; + const int UP = 1; + const int DOWN = 2; + + for (int i = 1; i < lens; i++) + { + switch (state) + { + case BEGIN: + if(nums[i - 1] < nums[i]){ + state = UP; + res++; + } + else if(nums[i - 1] > nums[i]) + { + state = DOWN; + res++; + } + break; + + case UP: + if (nums[i - 1] > nums[i]) + { + state = DOWN; + res++; + } + break; + + case DOWN: + if (nums[i - 1] < nums[i]) + { + state = UP; + res++; + } + break; + } + } + return res; +} + + +string removeKdigits(string num, int k) { + int lens = num.length(); + string res = ""; + if(!lens) return num; + stack stacks; + stacks.push(num[0] - '0'); + for (int i = 1; i < lens; i++) + { + int number = num[i] - '0'; + + while (!stacks.empty() && stacks.top() > number && k >0) + { + stacks.pop(); + k--; + } + + if (!stacks.empty() || number != 0) + { + stacks.push(number); + } + } + + while (k > 0 && !stacks.empty()) + { + stacks.pop(); + k--; + } + int length = stacks.size(); + for (int i = 0; i < length; i++) + { + res.append(1, stacks.top() - '0'); + stacks.pop(); + } + reverse(res.begin(), res.end()); + return res; +} \ No newline at end of file diff --git a/wordBreak.cpp b/wordBreak.cpp new file mode 100644 index 0000000..4007093 --- /dev/null +++ b/wordBreak.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +bool wordBreak(string s, vector& wordDict) { + set words; + for(auto x: wordDict) words.insert(x); + vector visit(s.size() + 1, 0); + queue q; + + q.push(0); + + while (!q.empty())w + { + int i = q.front(); + q.pop(); + + if(visit[i]) continue; + for (int j = i; j < s.size(); j++) + { + string tmp = s.substr(i, j - i + 1); + if (words.count(tmp)) + { + if (j == s.size() - 1) + { + return true; + } + q.push(j + 1); + } + } + visit[i] = 1; + } + return false; +} + +int main() +{ + string a = "abcd"; + vector b = {"a","abc","b","cd"}; + wordBreak(a,b); + return 0; +} \ No newline at end of file diff --git a/wordBreak.exe b/wordBreak.exe new file mode 100644 index 0000000..b4da741 Binary files /dev/null and b/wordBreak.exe differ diff --git a/wordPattern.cpp b/wordPattern.cpp new file mode 100644 index 0000000..288eae6 --- /dev/null +++ b/wordPattern.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +bool wordPattern(string pattern, string str) { + map wordMap; + vector used(128, '0'); + string tmp_word; + int index = 0; + str.push_back(' '); + + for (int i = 0; i < str.length(); i++) + { + if (str[i] == ' ') + { + if (index == pattern.length()) + { + return false; + } + if (wordMap.find(tmp_word) == wordMap.end()) + { + if (used[pattern[index]] == 1) + { + return false; + } + else + { + wordMap[tmp_word] = pattern[index]; + used[pattern[index]] = 1; + } + } + else + { + if (wordMap[tmp_word] != pattern[index]) + { + return false; + } + } + tmp_word = ""; + index++; + } + else + { + tmp_word += str[i]; + } + } + if (index != pattern.size()) + { + return false; + } + return true; +} \ No newline at end of file diff --git "a/\345\221\275\344\273\244\350\241\214.txt" "b/\345\221\275\344\273\244\350\241\214.txt" new file mode 100644 index 0000000..e5d0167 --- /dev/null +++ "b/\345\221\275\344\273\244\350\241\214.txt" @@ -0,0 +1,4 @@ + zmk@zmkdeMacBook-Pro  ~/c++/now_coding   master  git config --global user.name "ZMK112" + zmk@zmkdeMacBook-Pro  ~/c++/now_coding   master  git config --global user.email "1109645606@qq.com" + zmk@zmkdeMacBook-Pro  ~/c++/now_coding   master  git remote add origin git@github.com:ZMK112/AlgorithmCode.git + zmk@zmkdeMacBook-Pro  ~/c++/now_coding   master  git fetch