![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
Nahida_nora
这个作者很懒,什么都没留下…
展开
-
leetcode 2671
使用哈希, unordered_map 是基于hash 实现的key,val 存储。原创 2024-03-23 15:20:03 · 386 阅读 · 0 评论 -
leetcode 1466
如图 node 4 -> node 0 -> node 1因为节点数是n, 边长数量是n-1。所以如果是从0出发的路线,都需要修改,反之,如果是通向0的节点,例如节点4,则把节点4当作父节点的节点,之间的路线的方向都需修改。两个节点间只有一条方向,所以可以确定如何修改,取决和0节点的关系。如图 node 0 -> node 1 -> node 3原创 2023-12-08 14:31:54 · 137 阅读 · 0 评论 -
c++字符串 size和length的区别
可能length 快些。原创 2023-10-24 10:23:15 · 97 阅读 · 0 评论 -
leetcode 1657
【代码】leetcode 1657。原创 2023-11-30 10:28:08 · 155 阅读 · 0 评论 -
leetcode 979
leetcode 979原创 2023-10-17 19:44:01 · 134 阅读 · 0 评论 -
leetcode 1670
使用2个deque作为类的成员变量。原创 2023-11-28 12:33:50 · 283 阅读 · 0 评论 -
leetcode 2300
函数在vector中查找符合条件的元素的位置。如果找到了这样的元素,我们将输出它的位置;否则,我们将输出没有找到的消息。如果将double 转为long long, 会损失精度。所以使用upper_bound, 可以得到符合预期的范围。在这个示例中,我们创建了一个包含整数的vector,并使用。函数都用于在有序序列中进行二分查找。原创 2023-11-28 10:26:50 · 176 阅读 · 0 评论 -
leetcode 62
int例如:m=3, n=4由于机器人智能向右和向下行动,所以从(0,0) 到(0,1) 只能是向右1格,从(0,0)到(2,0) 只能是向下2格。从(0,1) 到(1,1) 也只能是向下1格,从(1,0) 到(1,1) 只能是向右1格。由此的到,动态转移方程,限定条件是f(i,0) 和 f(0,j) 都是1.原创 2023-11-10 10:28:58 · 76 阅读 · 0 评论 -
leetcode 318
【代码】leetcode 318。原创 2023-11-08 15:28:31 · 82 阅读 · 0 评论 -
leetcode1751
因为{1,1,10} 和 {1,1,5}, 是相同的路径,f[1] 和 f[2] 是相同的,这个动态规划可以解决路径一样的冲突。events[2] 基于 events[1] 和 events[0], 所以f[3] 是基于 f[2]的。原创 2023-11-08 14:45:17 · 85 阅读 · 0 评论 -
leetcode 1235
作者:力扣官方题解经典的数据结构转变,通过3个vector, 转换成二维的vector,3 5 40 , k = 1, 也就是 dp[1] = 503 6 70 , k = 1, 也就是 dp[1] = 50所以是dp[n]== 120。原创 2023-11-06 22:14:20 · 164 阅读 · 0 评论 -
leetcode2054
(ith start_time, ith end_time, ith val) 需要找到前i-1 end_time 的最大值,加上ith val, 遍历所有时间,找出最大值。原创 2023-11-06 19:36:19 · 130 阅读 · 0 评论 -
leetcode 动态规划专题
需要使用排序算法的动态规划。原创 2023-11-06 10:32:20 · 64 阅读 · 0 评论 -
leetcode 187
的字符串变量,并将其初始化为二进制字符串 “00000000000001101111”。A 使用 "00"表示,C使用"01"表示,G使用"10"表示,T使用"11"表示,int 是32bit, 字符串是20bit所以可以用int表示字符串的序列,函数返回的是字符串中字符的数量,而不是字符串占用的字节数。类型使用 UTF-8 编码存储字符串,每个字符可能占用不同数量的字节。类型还提供了其他用于获取字符串信息的成员函数,如。类型的值,表示字符串中字符的数量。成员函数来获取字符串的长度。原创 2023-11-05 13:55:31 · 123 阅读 · 0 评论 -
leetcode47
通过gdb, 第一个1和第二个1是重复的,第一个1的perm没有加入ans。第一行和第二行是perm [1], vis[1,0,0] 之后递归的输出;第三行和第四行是perm[1], vis[0,1,0] 之后递归的输出;是递归,问题是出现重复元素,对全排列的结果会造成影响。第五行是perm[2,1], vis[1,0,1];第六行是perm[2,1], vis[0,1,1];所以这道题考查的是递归如何去重复;原创 2023-10-29 19:59:14 · 134 阅读 · 0 评论 -
Mac docker+vscode
通过vscode 可以使用docker容器的环境。可以在容器安装gdb, 直接调试代码。原创 2023-10-29 14:42:39 · 905 阅读 · 0 评论 -
leetcode 45
假设我们已经知道了从起点跳到位置i的最小跳跃次数,那么从起点跳到位置i+1的最小跳跃次数可以通过以下方式计算:首先判断位置i是否超过了当前能够跳跃的边界位置end,如果超过了,说明需要进行一次跳跃,将步数加1,并更新边界位置为maxPosition。总之,贪心算法是一种简单而高效的算法思想,可以用于解决一些特定类型的问题,但需要注意贪心策略的正确性和问题的性质。贪心算法是一种基于贪心策略的算法思想,它在每一步选择中都采取当前状态下最优的选择,从而希望能够得到全局最优解。没怎么仔细看这个dp, 粘贴个答案。原创 2023-10-26 18:08:43 · 171 阅读 · 0 评论 -
leetcode87 扰乱字符串
leetcode87参考方法1:动态规划如果s1和s2的长度一致,并且所包含的字符种类,以及字符的数量一致,检查s1和s2是否和谐。和谐:s1=l(s1) + r(s1)s2 =l(s2) + r(s2)其中l(s1)和l(s2),r(s1)和r(s2) 以及 l(s1)和...原创 2022-02-08 02:44:50 · 92 阅读 · 0 评论 -
leetcode 稀疏相似度
leetcode 稀疏相似度方法1:将所有的word存入map, map[word] 存储含有该word的文章id 。使用二位数组记录, map[i][j], 第i篇文章和第j篇文章的交集元素数量。计算i和j的smiliarity = i和j的交集的数量/ i 的数量 + j的数量 - 交集的数量。c++答案方式2:使用归并排序。将<单词, 文章id> 存入vector, 在跟据单词进行排序,找到连续的的单词,对相应的进行交集的累加。c++答案方法3:隐式排序。先对文章原创 2021-05-30 19:16:49 · 98 阅读 · 0 评论 -
leetcode101 对称二叉树
现在更新leetcode 主要是提高对c++的熟悉。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(null原创 2021-02-21 00:04:53 · 76 阅读 · 0 评论 -
Leetcode 316 去除重复字母
Leetcode 316 去除重复字母尽可能确保字符串重的字符单调递增。确保字符串没有重复字符使用栈结构:不完全是单调栈。class Solution {public: string removeDuplicateLetters(string s) { vector<int> visited(26), nums(26); for(char ss : s){ nums[ss - 'a']++; }原创 2020-12-20 14:45:28 · 61 阅读 · 0 评论 -
Leetcode 46全排列
方法一: Backtrack例如:nums [1,2, 3]swap(0,0) -> swap(1,1) -> swap(2,2) -> Backtrack(3,3); nums[1,2,3]swap(1,2)->swap(2,2) -> Backtrack(3,3); nums[1,3,2]swap(0,1) -> swap(1,1) -> swap(2,2) -> Backtrack(3,3); nums[2,1,3]swap(1,2)->原创 2020-11-29 20:20:55 · 59 阅读 · 0 评论 -
Leetcode 10. Regular Expression Matching
Leetcode 10. Regular Expression MatchingApproach 1: Recursion比较p 和 s第一位,如果相同,继续比较(s.substring(1),p);如果不相同, 继续比较(s, p.substring(2))class Solution { public boolean isMatch(String s, String p) { if(p.isEmpty()) return s.isEmpty();原创 2020-08-05 02:00:09 · 107 阅读 · 0 评论 -
Leetcode 314. Binary Tree Vertical Order Traversal
Leetcode 314. Binary Tree Vertical Order TraversalApproach 2: BFS without Sortingclass Solution { public List<List<Integer>> verticalOrder(TreeNode root) { List<List<Integer>> output = new ArrayList(); if (root == null)原创 2020-08-04 03:52:58 · 99 阅读 · 0 评论 -
Leetcode 722. Remove Comments
Leetcode 722. Remove CommentsIntuition and AlgorithmWe need to parse the source line by line. Our state is that we either are in a block comment or not. 一行一行的粘贴,状态只有被注释和没有被注释,两种状态。If we start a block comment and we aren’t in a block, then we will skip原创 2020-08-04 03:16:10 · 65 阅读 · 0 评论 -
Leetcode 41. First Missing Positive
Leetcode 41. First Missing Positive算法:Check if 1 is present in the array. If not, you’re done and 1 is the answer. If nums = [1], the answer is 2. Replace negative numbers, zeros, and numbers larger than n by 1s. 检查1是否存在,不存在返回1。如果1存在,且数组只有一个元素,则返回2。把所有0原创 2020-08-03 03:53:19 · 140 阅读 · 0 评论 -
Leetcode 742. Closest Leaf in a Binary Tree
Leetcode 742. Closest Leaf in a Binary TreeApproach #1: Convert to Graph把树转换为以k节点为中心的图,从k节点开始层序遍历,返回最先遇到的叶子结点。class Solution { public int findClosestLeaf(TreeNode root, int k) { Map<TreeNode, List<TreeNode>> graph = new HashMap()原创 2020-08-03 00:03:58 · 92 阅读 · 0 评论 -
Leetcode 963. Minimum Area Rectangle II
Leetcode 963. Minimum Area Rectangle IIApproach 1: Iterate TrianglesSay the first 3 points are p1, p2, p3, and that p2 and p3 are opposite corners of the final rectangle. p2和p3是对称的。The 4th point must be p4 = p2 + p3 - p1 (using vector notation) because p原创 2020-07-07 05:50:21 · 93 阅读 · 0 评论 -
Leetcode 1485. Clone Binary Tree With Random Pointer
Leetcode 1485. Clone Binary Tree With Random Pointer使用HashMap记录,那些节点已经被访问过了,因为有random节点,所以可能出先重复访问。递归class Solution { public NodeCopy copyRandomBinaryTree(Node root) { Map<Node, NodeCopy> map = new HashMap<>(); return doC原创 2020-07-06 02:11:36 · 256 阅读 · 0 评论 -
Leetcode 395. Longest Substring with At Least K Repeating Characters
Leetcode 395. Longest Substring with At Least K Repeating Characters目前,除了递归没有想到特别好的想法去解决这个问题。Input:s = “ababbc”, k = 2a->2b->3c->1cur = 0, start =0;…cur = 4, map[c] < 2result = s.substring(start, cur), return reslut.length();public c原创 2020-06-26 00:48:04 · 86 阅读 · 0 评论 -
Leetcode 152. Maximum Product Subarray
Leetcode 152. Maximum Product Subarrayif there’s no zero in the array, then the subarray with maximum product must start with the first element or end with the last element.例子:关于0:[0, 100, 0] return 100;关于负号:[-1, 2, 3][1, 2, -3][-1, 2, -3]class So原创 2020-06-25 23:27:25 · 81 阅读 · 0 评论 -
Leetcode 713. Subarray Product Less Than K
Leetcode 713. Subarray Product Less Than KInput: nums = [10, 5, 2, 6], k = 100Output: 8index:0[10], [10, 5], ans = 2index: 1[5], [5,2], [5,2,6], ans = 5index: 2[2], [2, 6], ans = 7index: 3[6], ans =8Approach #2: Sliding Window [Accepted]Input:原创 2020-06-23 00:43:48 · 91 阅读 · 0 评论 -
Leetcode 325. Maximum Size Subarray Sum Equals k
Leetcode 325. Maximum Size Subarray Sum Equals kInput: nums = [1, -1, 5, -2, 3], k = 3Output: 4Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.Input: nums = [1, -1, 5, -2, 3], k = 3Map: [1, 0], [0,1], [5,2], [3,3], [6,4]return m原创 2020-06-23 00:16:12 · 93 阅读 · 0 评论 -
Leetcode 525. Contiguous Array
Leetcode 525. Contiguous Array使用数组写起来麻烦一点,但hashmap运行速度会慢一点。Approach #2 Using Extra Array [Accepted]For example:input: [1,1,1,1, 0]return 2count: [1,2,3,4,3]3-3 ==0 return 4(index of next 2) -2(index of first 2)public class Solution { public in原创 2020-06-22 23:54:08 · 720 阅读 · 0 评论 -
Leetcode 771. Jewels and Stones
Leetcode 771. Jewels and Stones一个字符代表一种是宝石(Jewels),一串(Stones)字符串有多少个宝石。Letters are case sensitive. 区分大小写。For example:“aA”“abAaAAbbbb”return 5Approach #1: Brute Force [Accepted]class Solution { public int numJewelsInStones(String J, String S) {原创 2020-06-22 06:39:48 · 84 阅读 · 0 评论 -
Leetcode 725. Split Linked List in Parts
Leetcode 725. Split Linked List in PartsIf there are N nodes in the linked list root, then there are N / k items in each part, plus the first N%k parts have an extra item.有N个节点,前N%k个部分有 (N/k + N%k )个数字,其余有N/k。Approach #1: Create New Lists [Accepted]cl原创 2020-06-22 06:16:56 · 110 阅读 · 1 评论 -
Leetcode 369. Plus One Linked List
Leetcode 369. Plus One Linked ListSentinel node可能遇到:head 是9的情况, head == 9->9->9, 所以需要引入sentinel node, 0 (sentinel) -> 9 -> 9 -> 9, 然后发现最右边第一个不为9的节点,就是sentinel node, 1 -> 0 -> 0 ->0, return sentinelhead 不是9的情况, 8 -> 9 -&原创 2020-06-22 02:08:28 · 107 阅读 · 0 评论 -
Leetcode 708. Insert into a Sorted Circular Linked List
Leetcode 708. Insert into a Sorted Circular Linked ListApproach 1: Two-Pointers IterationGiven a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal into the list such that it remains a sorted原创 2020-06-22 01:02:39 · 198 阅读 · 0 评论 -
Leetcode 1257. Smallest Common Region
Leetcode 1257. Smallest Common RegionBuild family tree from offsprings to their parents;Use a HashSet to construct ancestry history of region1;Retrieve ancestry of region2 by family tree till find the first common ancestry in ancestry history of region1原创 2020-06-10 00:33:57 · 123 阅读 · 0 评论 -
Leetcode 1124. Longest Well-Performing Interval
Leetcode 1124. Longest Well-Performing IntervalWe starts with a score = 0,If the working hour > 8, we plus 1 point.Otherwise we minus 1 point.We want find the maximum interval that have strict positive score.After one day of work, if we find the to原创 2020-06-08 08:23:31 · 152 阅读 · 0 评论