自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(37)
  • 收藏
  • 关注

原创 窗口的最大值

class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { if (nums.size() == 0) { return {}; } deque<int> q; vector<int> ans; for (int i = 0; i <

2020-10-28 18:33:57 217

原创 移除重复元素 双指针题目

双指针一定要理解好slow 和 fast的指针的含义slow之前的(不包含slow)为已经处理好的数据fast指针只是个遍历用的指针class Solution { public int removeDuplicates(int[] nums) { if (nums.length <= 2) { return nums.length; } int fast = 2; int slow = 2; .

2020-10-24 21:52:40 216

原创 56合并区间

class Solution { public int[][] merge(int[][] intervals) { List<int[]> ans = new ArrayList<int[]>(); Arrays.sort(intervals, new Comparator<int[]>(){ public int compare(int[] o1, int[] o2) {

2020-10-24 18:06:34 98

原创 树的子结构, 不是子树

public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) { boolean result = false; if (root1 != null && root2 != null) { if (root1.val == root2.val) { result = tree1ContainsTre

2020-10-23 12:20:10 219

原创 每日一题[763] 划分字母区间

题目字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一个字母只会出现在其中的一个片段。返回一个表示每个字符串片段的长度的列表。示例 1:输入:S = “ababcbacadefegdehijhklij”输出:[9,7,8]解释:划分结果为 “ababcbaca”, “defegde”, “hijhklij”。每个字母最多出现在一个片段中。像 “ababcbacadefegde”, “hijhklij” 的划分是错误的,因为划分的片段数较少。 class Solutio

2020-10-23 00:18:53 213

原创 26删除数组中重复的元素

双指针经典题目这是一个同向移动的双指针设定[0, index)区间为已经处理好的数据, 当前index位置是下一个可以替换的位置, [index, len)为待处理的数据, i为数据处理使用的游标此对index的位置恰好代表已经处理好的数据的长度, 可以直接返回indexclass Solution { public int removeDuplicates(int[] nums) { int len = nums.length; int index = .

2020-10-21 18:00:15 130

原创 二叉搜索树中的众数

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* prev = nullptr; v

2020-10-18 03:17:01 101

原创 删除链表中倒数第n个节点

思路删除倒数第n个节点, n保证是有效数字需要找到倒数第n+1的节点, 采用dummyNode防止删除的是头结点修改指针指向删掉多余的节点代码class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode Node = ListNode(-1); ListNode* dummyNode = &Node, *fast = &Node

2020-10-18 01:21:01 112

原创 最长有效括号

一维动态规划, 没有想到dp的含义是包含当前位置的有效长度思路比较难, 区分成两种情况考虑, 一般想不到.比较考验数组下标操作以及越界检查class Solution {public: int longestValidParentheses(string s) { int n = s.length(); vector<int> dp(n, 0); int result = 0; for (int i = 1; .

2020-10-18 00:10:12 119

原创 统计数字中的二进制位数

自己构造一个mask, mask的二进位中最多只有一个是1, 不断的左移, 如果和n与不是0, 那么说明这一位是1class Solution {public: int NumberOf1(int n) { int mask = 1; int count = 0; while (mask) { if (n & mask) { count++; .

2020-10-17 14:12:32 262

原创 每天一题 977. 有序数组的平方

class Solution {public: vector<int> sortedSquares(vector<int>& A) { if (A.empty()) { return {}; } priority_queue<int, vector<int>, greater<int>> q; for (int num : A) {

2020-10-16 12:37:27 158

原创 判断完全二叉树

主要是考察层次遍历记住之前的元素, 如果之前的元素已经是nullptr了而且当前元素不是nullptr,那么这就不是完全二叉树,具体可以手动模拟一下这个操作./** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), ri.

2020-10-16 12:30:50 235

原创 24题两两交换链表节点

class Solution {public: ListNode* swapPairs(ListNode* head) { ListNode* dummyNode = new ListNode(-1); dummyNode->next = head; ListNode* tmp = dummyNode; while (tmp->next && tmp->next->next) {

2020-10-14 00:00:23 115

原创 二叉树的直径

设置一个全局变量,不断更新这个值辅助函数中的返回值不能作为最终的返回值, 它只是在遍历过程中处理节点与节点的关系, 最终应该返回全局变量class Solution {public: int result = 0; int diameterOfBinaryTree(TreeNode* root) { helper(root); return result; } int helper(TreeNode* root) { .

2020-10-12 13:24:46 114

原创 leetcode151单词翻转

两次翻转处理空格比较恶心 容易出错class Solution {public: string reverseWords(string s) { if(s.empty()) return ""; reverse(s.begin(), s.end()); int begin = 0; int n = s.length(); int index = 0; while (begin < n) {.

2020-10-11 13:40:19 186

原创 找到环形链表的第一个节点

class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while (slow && fast && fast->next) { fast = fast->next->next; slow =

2020-10-10 01:35:14 276

原创 Find All Anagrams in a String

自己定义一个长度为26的数组 每个位置代表一个字母,这样比使用unordered_map要快, 学到了unordered_map比较 要注意同一个key的value 为0 和 根本就没有这个key的区别, 这两者是不一样的class Solution {public: vector<int> findAnagrams(string s, string p) { vector<int> goal(26), cur(26), res; f.

2020-10-10 01:09:17 57

原创 word break DP解法

注意字符串下标和dp下标的对应关系, 因为dp的空间比s的大小多1, 所以字符串下标i对应到dp中的下标就是i+1class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { int size = s.length(); vector<int> dp(size+1, 0); dp[0] = 1; unor.

2020-10-09 21:02:29 100

原创 接雨水 暴力算法

思路在当前位置, 考虑左右两边的最大高度, 然后取其中的最小值, 最小值减去当前位置的高度就是可以存的水的体积, 一直累加就可以Codeclass Solution {public: int trap(vector<int>& height) { if (height.size() <= 2) { return 0; } int n = height.size(); int

2020-10-09 15:13:01 142

原创 盛水最多的容器

思路双指针一开始选择左右两个边界是因为r-l为最大, 两个指针往中间移动的时候, 这两个指针指向的高度要大于之前的最小高度才可以, 因为r-l变小了, 要装更多的水, 那么高度必须要提高.对于height[l] == height[r]的情况, 此时的高度固定, 所以[l+1, r] 以及 [l, r-1]都不是一个更好的解, 因为宽度变小了Codeclass Solution {public: int maxArea(vector<int>& height) {

2020-10-09 14:34:16 59

原创 判断链表有环

class Solution {public: bool hasCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while (slow && fast && fast->next) { fast = fast->next->next; slow = slow-&g

2020-10-09 13:44:42 97

原创 maxSlidingWindow 单调队列应用

class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { if (nums.empty() || k > nums.size()) { return {}; } vector<int> result; // 这里存储的下标, 一定注意 dequ

2020-10-09 12:24:40 101

原创 两个栈实现队列

思路栈的特点是先进后出, 但是队列是先进先出, 所以需要一个辅助栈, 这个栈的目的就是push的元素进行翻转, 可以直接pop, 从辅助栈pop得到的结果就是最先push进去的元素.Codeclass MyQueue {public: /** Initialize your data structure here. */ stack<int> input; stack<int> output; MyQueue() {

2020-10-09 11:59:19 70

原创 130. 被围绕的区域

思路奇特, 逆向思维, 以四周上的’O’为突破口, 标记为访问过, 然后重新遍历数据, 将没有标记过得’O’变成X就可以了本质上还是一个4个方向的回溯, 可以说就是4叉树的遍历, 再本致电说就是个递归class Solution {public: void solve(vector<vector<char>>& board) { if (board.empty() || board[0].empty()) { retu.

2020-10-08 12:55:13 63 1

原创 岛屿数量

class Solution {public: int numIslands(vector<vector<char>>& grid) { if (grid.empty() || grid[0].empty()) { return 0; } int rows = grid.size(); int cols = grid[0].size(); vector<ve

2020-10-08 12:32:25 61

原创 ip分割

用c++写就是有点费力class Solution {public: vector<string> restoreIpAddresses(string s) { int len = s.length(); if (len < 4 || len > 12) { return {}; } dfs(s, 0, ans); return ans; } vo.

2020-10-08 02:37:56 366

原创 N皇后问题

核心问题就是一个回溯算法, 并不困难另一个问题在于如何简单有效的判断住对角线和副对角线以及行列上不存在冲突的问题行冲突在递归中使用depth变量(等同于row)解决了列冲突使用col数组解决每一条主对角线上的数字相加的结果是一样的, 会有2n-1主对角线每一条副对角线上的数字相减的结果是一样的class Solution {public: vector<int> tmp; vector<vector<string>> ans; .

2020-10-08 01:53:25 72

原创 三色问题

class Solution {public: void sortColors(vector<int>& nums) { if (nums.empty()) { return; } int l = -1; int r = nums.size(); int idx = 0; while (idx < r) { if (nums[id

2020-10-07 20:23:40 948

原创 39组合综合

题目要求输入:candidates = [2,3,6,7], target = 7,所求解集为:[[7],[2,2,3]]分析考察回溯算法拿上边的输入距离,就相当于一棵4叉树, 节点分别是2, 3, 6, 7,每一层都是这样重点在于如何去重会出现的重复的是[2, 2, 3], [2, 3, 2,], [3, 2, 2], 所以档处理到第i个数字的时候,下一层只能考虑[i, candidates.size()-1]中的数字,这样就避免了[3, 2, 2]的出现以及[2, 3,

2020-10-07 12:45:34 107

原创 盛水最多的容器

第一次见双指针用法,处理非有序数组class Solution {public: int maxArea(vector<int>& height) { int l = 0; int r = height.size() - 1; int maxSum = 0; while (l < r) { int n = min(height[l], height[r]); .

2020-10-06 15:11:48 50

原创 三数之和 有大学问

class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { int n = nums.size(); if (n < 3) { return {}; } unordered_set<int> us; vector<vector<int&

2020-10-05 22:51:52 115

原创 leetcode 1047. 删除字符串中的所有相邻重复项

爱消除,对对碰的基本思路, 一个栈解决class Solution {public: string removeDuplicates(string S) { if (S.empty()) { return string(""); } stack<char> stk; for (int i = 0; i < S.length(); i++) { if (!stk.e.

2020-10-05 20:13:53 53

原创 合并两个有序链表

/** * 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)

2020-10-05 11:43:46 107

原创 倒数第k个节点

class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode dummyNode; dummyNode.next = head; ListNode* fast = &dummyNode; ListNode* slow = &dummyNode; while (n+1) {

2020-10-04 23:39:54 40

原创 leetcode链表相加

/** * 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)

2020-10-04 23:07:45 59

原创 438 Find All Anagrams in a String

//// Created by 麻艺龙 on 2020/10/4.//#include<iostream>#include<vector>#include<queue>#include<set>#include<unordered_map>using namespace std;class Solution {public: vector<int> findAnagrams(string s, stri

2020-10-04 21:47:42 80

原创 和为S的连续正数序列

原题链接就是使用前缀和 + 滑动窗口class Solution {public: vector<vector<int> > FindContinuousSequence(int sum) { vector<int> nums(sum, 0); vector<vector<int>> ans; for (int i = 1; i < sum; i++) {

2020-10-03 18:28:22 38

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除