![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode刷题
purple 桐
这个作者很懒,什么都没留下…
展开
-
刷题总结——算法思想
LeetCode——双指针T167. 两数之和 II - 输入有序数组class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int left = 0; int right = numbers.size()-1; while(left < right){ int num = numbe原创 2020-08-03 11:29:48 · 336 阅读 · 0 评论 -
刷题总结——数据结构相关
LeetCode—链表T160. 相交链表我们让两条链表分别从各自的开头开始往后遍历,当其中一条遍历到末尾时,我们跳到另一个条链表的开头继续遍历。两个指针最终会相等,而且只有两种情况,一种情况是在交点处相遇,另一种情况是在各自的末尾的空节点处相等。为什么一定会相等呢,因为两个指针走过的路程相同,是两个链表的长度之和,所以一定会相等。/** * Definition for singly-linked list. * struct ListNode { * int val; *原创 2020-07-29 11:45:53 · 283 阅读 · 0 评论 -
LeetCode—搜索
LeetCode—搜索BFS1、计算在网格中从原点到特定点的最短路径长度T1091. Shortest Path in Binary Matrix(Medium)2、组成整数的最小平方数数量T279. Perfect Squares (Medium)class Solution {public: int numSquares(int n) { vector<int> result(n+1, 0x7FFFFFFF); // 每个数的最优解都存在result原创 2020-06-22 16:56:51 · 192 阅读 · 0 评论 -
LeetCode—数学
LeetCode—数学素数分解整除最大公约数最小公倍数1、生成素数序列T204. Count Primes (Easy)class Solution {public: int countPrimes(int n) { int count = 0; //初始默认所有数为质数 vector<bool> signs(n, true); for (int i = 2; i < n; i++) { if (signs[i原创 2020-06-22 12:02:04 · 266 阅读 · 0 评论 -
LeetCode—分治
LeetCode—分治1、给表达式加括号T241. Different Ways to Add Parentheses (Medium)class Solution {public: vector<int> diffWaysToCompute(string input) { vector<int> res; for (int i = 0; i < input.size(); ++i) { if (inpu原创 2020-06-22 11:21:05 · 132 阅读 · 0 评论 -
LeetCode—动态规划
LeetCode—动态规划斐波那契数列1、爬楼梯T70. Climbing Stairs (Easy)class Solution {public: int climbStairs(int n) { if (n <= 1) return 1; vector<int> dp(n); dp[0] = 1; dp[1] = 2; for (int i = 2; i < n; ++i) {原创 2020-06-22 11:03:19 · 369 阅读 · 0 评论 -
LeetCode——二分查找
LeetCode——二分查找1、求开方T69. Sqrt(x) (Easy)原创 2020-06-20 12:09:21 · 126 阅读 · 0 评论 -
LeetCode—贪心思想
LeetCode—贪心思想保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。1、分配饼干T455. Assign Cookies (Easy)class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { int res=0; int p=0; sort(g.begin(), g.end())原创 2020-06-20 11:30:44 · 147 阅读 · 0 评论 -
LeetCode—树—Tire
LeetCode—树—TireTrie,又称前缀树或字典树,用于判断字符串是否存在或者是否具有某种字符串前缀。1、实现一个 TrieT208. Implement Trie (Prefix Tree) (Medium)class TrieNode {public: TrieNode *child[26]; bool isWord; TrieNode(): isWord(false) { for (auto &a : child) a = nullp原创 2020-06-19 15:29:52 · 225 阅读 · 0 评论 -
LeetCode—树—BST
LeetCode—树—BST二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。二叉查找树中序遍历有序。1、修剪二叉查找树T669. Trim a Binary Search Tree (Easy)/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo原创 2020-06-19 15:23:09 · 116 阅读 · 0 评论 -
LeetCode—树—前中后序遍历
LeetCode—树—前中后序遍历层次遍历使用 BFS 实现,利用的就是 BFS 一层一层遍历的特性;而前序、中序、后序遍历利用了 DFS 实现。前序、中序、后序遍只是在对节点访问的顺序有一点不同,其它都相同。① 前序void dfs(TreeNode root) { visit(root); dfs(root.left); dfs(root.right);}② 中序void dfs(TreeNode root) { dfs(root.left); v原创 2020-06-19 11:36:31 · 143 阅读 · 0 评论 -
LeetCode—树—层次遍历
LeetCode—树—层次遍历使用 BFS 进行层次遍历。不需要使用两个队列来分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。1、一棵树每层节点的平均数T637. Average of Levels in Binary Tree (Easy)/** * Definition for a binary tree node. * struct TreeNode { * in原创 2020-06-19 11:15:48 · 199 阅读 · 0 评论 -
LeetCode——位运算
LeetCode——位运算1、统计两个数的二进制表示有多少位不同T461 Hamming Distance (Easy)class Solution {public: int hammingDistance(int x, int y) { int res=0; for(int i=0; i<32; ++i){ if(x & (1<<i) ^ y & (1<<i)){原创 2020-06-19 10:36:25 · 348 阅读 · 0 评论 -
LeetCode—图
LeetCode—图二分图(可以将图中的所有顶点分成两个不想交的集合,使得同一个集合的顶点不相连)如果可以用两种颜色对图中的节点进行着色,并且保证相邻的节点颜色不同,那么这个图就是二分图。1、判断是否为二分图T785. Is Graph Bipartite? (Medium)class Solution {public: bool isBipartite(vector<vector<int>>& graph) { vector<in原创 2020-06-17 20:13:02 · 245 阅读 · 0 评论 -
LeetCode—数组与矩阵
LeetCode—数组与矩阵1、把数组中的 0 移到末尾T283 Move Zeroes (Easy)class Solution {public: void moveZeroes(vector<int>& nums) { for(int i=0,j=0; i<nums.size(); ++i){ if(nums[i]) swap(nums[i],nums[j++]); }原创 2020-06-17 19:28:35 · 111 阅读 · 0 评论 -
LeetCode—字符串
LeetCode—字符串1、字符串循环移位包含编程之美 3.1s1 = AABCD, s2 = CDAAReturn : true给定两个字符串 s1 和 s2,要求判定 s2 是否能够被 s1 做循环移位得到的字符串包含。s1 进行循环移位的结果是 s1s1 的子字符串,因此只要判断 s2 是否是 s1s1 的子字符串即可。2、字符串循环移位编程之美 2.17s = “abcd123” k = 3Return “123abcd”将字符串向右循环移动 k 位。将 abcd123 中的原创 2020-06-17 14:45:12 · 173 阅读 · 0 评论 -
LeetCode——哈希表
LeetCode——哈希表1、数组中两个数的和为给定值T1 Two Sum (Easy)class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> m; vector<int> res; for(int i=0; i<nums.size();原创 2020-06-15 10:25:46 · 158 阅读 · 0 评论 -
LeetCode—栈和队列
LeetCode—栈和队列1、用栈实现队列T.232 Implement Queue using Stacks (Easy)class MyQueue {public: stack<int> st1,st2; /** Initialize your data structure here. */ MyQueue() { while(!st1.empty()) st1.pop(); while(!st2.empty原创 2020-06-14 18:30:21 · 188 阅读 · 0 评论 -
LeetCode—链表
LeetCode—链表1、找出两个链表的交点T.160 Intersection of Two Linked Lists (Easy)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:原创 2020-06-14 15:55:38 · 130 阅读 · 0 评论 -
LeetCode—排序
LeetCode—排序快速选择用于求解 Kth Element 问题,也就是第 K 个元素的问题。可以使用快速排序的 partition() 进行实现。需要先打乱数组,否则最坏情况下时间复杂度为 O(N2)。堆用于求解 TopK Elements 问题,也就是 K 个最小元素的问题。可以维护一个大小为 K 的最小堆,最小堆中的元素就是最小元素。最小堆需要使用大顶堆来实现,大顶堆表示堆顶元素是堆中最大元素。这是因为我们要得到 k 个最小的元素,因此当遍历到一个新的元素时,需要知道这个新元素是否比堆中原创 2020-06-14 12:09:08 · 116 阅读 · 0 评论 -
LeetCode刷题——树——递归
LeetCode——树——递归1、树的高度T.104 Maximum Depth of Binary Tree (Easy)/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *原创 2020-06-13 19:16:41 · 175 阅读 · 0 评论 -
LeetCode刷题——双指针
LeetCode刷题——双指针1、有序数组的 Two SumT167 Two Sum II - Input array is sorted (Easy)class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int l = 0; int r = numbers.size()-1; while(l<r){原创 2020-06-13 18:53:09 · 201 阅读 · 0 评论