LeetCode.844.986.11(双指针)
844.比较含退格的字符串class Solution { public: bool backspaceCompare(string S, string T) { int sSkipNum = 0; // 记录S的#数量 int tSkipNum = 0; // 记录T的#数量 int i = S.size() - 1; int j = T.size() - 1; while (1) { while (i >= 0) { // 从后向前,消除S的# if (S[i] == '#') sSkipNum++; else { if (sSkipNum > 0) sSkipNum--; else ...
LeetCode.82.15(双指针)
82.删除排序链表中的重复元素-ii/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */class Solution { public: ListNode *deleteDuplicates(ListNode *head) { if (!head || !head->next) return head; ListNode *front = head->next ...
LeetCode.153.162(二分查找)
153.寻找旋转排序数组中的最小值class Solution { public: int findMin(vector<int> &nums) { /*int left = 0, right = nums.size() - 1; while (right >= left) { int mid = left + (right - left) / 2; if (nums[left] > nums[mid]) right = mid; else if (nums[right] < nums[mid]) left = mid + 1; if (nums[left] <= nums[right]) return nums[left]; } return nums[left] ...
LeetCode.34.33.74(二分查找)
34.在排序数组中查找元素的第一个和最后一个位置class Solution { public: vector<int> searchRange(vector<int> &nums, int target) { int start = 0, end = nums.size() - 1; while (end >= start) { int mid = start + (end - start) / 2; if (nums[mid] > target) end = mid - 1; else if (nums[mid] < target) start = mid + 1; else { if (nums[start] < target) { ...
LeetCode.231.191.190.136(位运算)
231.2-的幂class Solution { public: bool isPowerOfTwo(int n) { /*for (; n % 2 == 0 && n != 0; n /= 2) { } return n == 1 ? true : false;*/ // 1108/1108 cases passed (0 ms) // Your runtime beats 100 % of cpp submissions // Your memory usage beats 71.69 % of cpp submissions (5.8 MB) //???位运算??? // return (n > 0) && (n & -n) == n; // 1108/1108 cases passed (0 ms) // Your runtime beats 10 ...
LeetCode.70.198.120(动态规划)
终于有点明白了为什么动规叫做用空间换时间
70.爬楼梯原来写过,又写了一遍,发现果然忘了==
#include <vector>using namespace std;class Solution { public: int climbStairs(int n) { int times = 1, temp1 = 0, temp2 = 0; for (int i = 0; i < n; i++) { temp2 = times; times += temp1; temp1 = temp2; } return times; } // 45/45 cases passed (0 ms) // Your runtime beats 100 % of cpp submissions // Your memory usage beats 71.84 % of cpp submis ...
LeetCode.77.46.784(递归&回溯)
回溯法模板void backtracking(参数) { if (终止条件) { 存放结果; return; } for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) { 处理节点; backtracking(路径,选择列表); // 递归 回溯,撤销处理结果 }}
77.组合class Solution { public: /*vector<vector<int>> result; // 存放符合条件结果的集合 vector<int> path; // 用来存放符合条件结果 void backtracking(int n, int k, int startIndex) { if (path.size() == k) { result.push_back(path); ...
LeetCode.21.206(递归&回溯)
21.合并两个有序链表好久之前写过,因为学习规划又写了一遍,这次用了递归/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (!l1) { return l2; } if (! ...
LeetCode.542.994(广度优先搜索)
542.01-矩阵/*struct coord { int x; int y; coord(int x, int y) : x(x), y(y) {}};*/class Solution { public: /*vector<vector<int>> updateMatrix(vector<vector<int>> &mat) { vector<bool> visitedx(mat[0].size(), 0); vector<vector<bool>> visited(mat.size(), visitedx); for (int i = 0; i < mat.size(); i++) { for (int j = 0; j < mat[0].size(); j++) { for (in ...
LeetCode.733.695.617.116(广度优先搜索&深度优先搜索)
今天的题目真滴难,一天都花在这上面,UE4都没碰
733.图像渲染class Solution { public: int n, m, w; vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int newColor) { n = image.size(); m = image[0].size(); w = image[sr][sc]; if (w != newColor) // 起点像素与新颜色值不同则以此为起点开始深搜 DFS(image, sr, sc, newColor); return image; // 277/277 cases passed (8 ms) // Your runtime beats 74.59 % of cpp submissions / ...