8 rock4you

尚未进行身份认证

暂无相关简介

等级
TA的排名 2w+

leetcode 557. Reverse Words in a String III 反转字符串中的每个单词

https://leetcode.com/problems/reverse-words-in-a-string-iii/解法一:遇到字符就压栈,遇到空格或最后一个字符就弹出栈。class Solution {public: string reverseWords(string s) { int n = s.length(); if(n<...

2020-02-16 11:57:34

leetcode 151 / 186. Reverse Words in a String 将字符串中的单词反序

https://leetcode.com/problems/reverse-words-in-a-string/先整体反序,再逐个单词块进行反序。参考https://www.cnblogs.com/grandyang/p/4606676.htmlclass Solution {public: string reverseWords(string s) { ...

2020-02-16 11:28:07

leetcode 215. Kth Largest Element in an Array 数组中的第K大的元素

https://leetcode.com/problems/kth-largest-element-in-an-array/将数组看为没有调整到位的最大堆,由于堆是完全二叉树,因此根节点与子节点的下标存在对应关系。每次从堆中取出最大值,将其放至根节点,再与最后一个节点互换位置,然后将其从堆删除。class Solution {public: int findKthLar...

2020-02-14 10:29:28

4邻域、8邻域问题-DFS/BFS

leetcode 200 岛屿个数leetcode 695 最大岛屿面积leetcode 1091 八邻域,矩阵左上角至右下角最短路径

2020-02-12 15:56:30

leetcode 123. Best Time to Buy and Sell Stock III 股票最多两次买入、两次卖出的最大利润

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/数组的最大2子段和,允许在一天内买入又卖出,相当于不交易。因为题目规定的是最多交易两次,而不是必须交易两次。class Solution {public: int maxProfit(vector<int>& prices)...

2020-02-12 11:04:46

leetcode 122. Best Time to Buy and Sell Stock II 股票多次买入卖出的最大利润

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/把所有的大于零的价格差累加起来。class Solution {public: int maxProfit(vector<int>& prices) { int n = prices.size(); ...

2020-02-12 10:31:35

leetcode 121. Best Time to Buy and Sell Stock 股票一次买入一次卖出的最大利润

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/滑动更新最低价、最大收益值。class Solution {public: int maxProfit(vector<int>& prices) { int n = prices.size(); if...

2020-02-12 10:29:58

leetcode 448. Find All Numbers Disappeared in an Array

https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/题目要求不使用额外空间,并且时间复杂度为O(n)思路:若元素值a[ i -1 ]与其序号i 不等,并且a[i-1] 与a[ a[i-1] -1]不等,则将a[i-1] 与 a[ a[i-1] -1] 对调位置后,两者对调后至少有一个...

2020-02-11 17:34:48

leetcode 222. Count Complete Tree Nodes 统计完全二叉树节点个数

https://leetcode.com/problems/count-complete-tree-nodes/层次遍历二叉树,将节点压入队列,取出队列头部,将其左右子树压入队列。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

2020-02-10 16:08:34

leetcode 958. Check Completeness of a Binary Tree 完全二叉树的判定

https://leetcode.com/problems/check-completeness-of-a-binary-tree/层次遍历,依次将每层的所有节点压入队列,空节点也照常压入,每次从队列中取出一个节点,若该节点为空,则队列应该为空,或者剩余节点全部为空节点。/** * Definition for a binary tree node. * stru...

2020-02-10 16:06:13

leetcode 977. Squares of a Sorted Array 求一个数组的平方

https://leetcode.com/problems/squares-of-a-sorted-array/两个指针i,j,分别指向头和尾,平方后比较:若相等,则将元素的平方压入数组result,左指针i右移;反之,将较大的元素平方压入数组result,较大的指针向另一个指针移动;压入最后一个元素,将result数组取反。class Solution {public...

2020-02-08 15:44:45

leetcode 547. Friend Circles 并查集

https://leetcode.com/problems/friend-circles/朋友关系是双向的,因此矩阵M是关于对角线对称的,只需要处理对角线以上的半边即可。用一维数组 f [] 记录每个节点真实的根,使用并查集的方式,求出朋友圈的个数。class Solution {public: int findCircleNum(vector<ve...

2020-02-02 16:09:55

leetcode 72. Edit Distance 编辑距离-动态规划、滚动数组

https://leetcode.com/problems/edit-distance/1.动态规划,二维数组class Solution {public: int minDistance(string word1, string word2) { if(word1.empty() || word1.length()==0) retu...

2020-01-17 09:49:30

leetcode 117. Populating Next Right Pointers in Each Node II 给非完全二叉树每个节点增加一个水平指针

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/1.递归-对每层新建一个外部节点,用于将每层串起来。由于所有节点的next初始化为null,因此对于最右侧的节点不作处理。/*// Definition for a Node.class Node {public: i...

2020-01-07 15:31:02

leetcode 116. Populating Next Right Pointers in Each Node 完全二叉树每个节点增加一个水平指针

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/1.递归/*// Definition for a Node.class Node {public: int val; Node* left; Node* right; Node* next; ...

2020-01-07 10:54:46

leetcode 110. Balanced Binary Tree 判断二叉树是否为平衡二叉树

https://leetcode.com/problems/balanced-binary-tree/1.通过计算每个节点左右子树的深度,自顶向下,判断每个节点是否平衡这种方法存在大量重复计算,效率较低。/** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre...

2020-01-07 09:13:51

leetcode 101. Symmetric Tree 判断二叉树是否左右对称

https://leetcode.com/problems/symmetric-tree/1.递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(in...

2020-01-06 21:51:42

leetcode 100. Same Tree 判断两个二叉树是否相同

https://leetcode.com/problems/same-tree//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(...

2020-01-06 21:24:31

leetcode 103. Binary Tree Zigzag Level Order Traversal 二叉树之字型遍历-递归、非递归

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/掌握vector的头插法 v.insert(v.begin(),val)之后,这道题就变得很简单了。1.递归/** * Definition for a binary tree node. * struct TreeNode { *...

2020-01-06 21:06:46

leetcode 107. Binary Tree Level Order Traversal II 二叉树自底向上层次遍历-递归、非递归

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/在leetcode102题的基础上,将vector反序即可。1.递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * T...

2020-01-06 19:47:50

查看更多

勋章 我的勋章
  • GitHub
    GitHub
    绑定GitHub第三方账户获取
  • 持之以恒
    持之以恒
    授予每个自然月内发布4篇或4篇以上原创或翻译IT博文的用户。不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
  • 勤写标兵Lv4
    勤写标兵Lv4
    授予每个自然周发布9篇以上(包括9篇)原创IT博文的用户。本勋章将于次周周三上午根据用户上周的博文发布情况由系统自动颁发。