![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
oldwong77
这个作者很懒,什么都没留下…
展开
-
leetcode139. 单词拆分(dp)
class Solution { public boolean wordBreak(String s, List<String> wordDict) { int len = s.length(); // 完全背包问题 // 每个单词可以选择无数次 // dp[i]表示下标为i-1结尾的string,能否被dict划分 boolean[] dp = new boolean[len+1]; dp.原创 2021-06-11 09:11:23 · 196 阅读 · 0 评论 -
leetcode 560. 和为K的子数组
开始的写法通过了前缀和,时间复杂度O(n∗n)O(n*n)O(n∗n),空间复杂度O(n)O(n)O(n)看了别人的解答,可以通过map存储前缀和,优化时间复杂度为O(n)O(n)O(n)1 2 4 3 5比如我们要找k=6的结果可以理解为current_sum - before_sum = k也就是current_sum-k = before_sum我们可以把before_sum事先存储在map中class Solution { public int subarraySum(in原创 2021-06-09 09:59:49 · 168 阅读 · 0 评论 -
leetcode309. 最佳买卖股票时机含冷冻期(dp)
class Solution { public int maxProfit(int[] prices) { // 1 2 3 0 2 /* have[i]表示第i天持有股票的最大值 no[i]表示第i天没有持有股票的最大值 // 如果是不包含冷冻期 have[i] = max(have[i-1],no[i-1]-price[i]) (*) no[i] = max(no[i-1],hav原创 2021-06-04 15:24:45 · 138 阅读 · 0 评论 -
leetcode 146. LRU缓存机制
时间复杂度o(1)的写法还不会目前是通过一个hash表+一个链表实现的import java.util.HashMap;import java.util.LinkedList;import java.util.List;class LRUCache { int cap; HashMap<Integer, Integer> hashMap; LinkedList<Integer> lst; public LRUCache(int cap原创 2020-10-23 22:31:32 · 149 阅读 · 0 评论 -
leetcode 124. 二叉树中的最大路径和(dfs)
题目意思表达仔细读清楚!重点看两个更新一个是当前结点的权重贡献值的更新是:max(max1,max2,max3)一个是以当前结点为根,能取到的最大值是:max(max1,max2,max3,max4) int maxx = Integer.MIN_VALUE; //学习了如何表示maxx public int maxPathSum(TreeNode root) { if(root==null) return 0; dfs(root);原创 2020-10-23 21:30:39 · 144 阅读 · 0 评论 -
20200409-leetcode-22. 括号生成(dfs)
传送门class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ def dfs(ans, s, left, right): if left ==...原创 2020-04-09 09:38:58 · 193 阅读 · 0 评论 -
20200408-leetcode-面试题13. 机器人的运动范围(bfs)
class Solution(object): def movingCount(self, m, n, k): def add_coor(a, b): ans = 0 while a != 0: ans += a % 10 a //= 10 ...原创 2020-04-08 10:55:13 · 157 阅读 · 0 评论 -
20200329-剑指offer-面试题29. 顺时针打印矩阵(模拟)
传送门这个循环写的太丑了,还是暴力过了。class Solution(object): def spiralOrder(self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """ # 原矩阵n行m列 n =...原创 2020-04-05 11:00:44 · 155 阅读 · 0 评论 -
20200404-leetcode-42. 接雨水
传送门开始直接暴力会一直超时O(n∗n)O(n*n)O(n∗n)可以先创建两个数组,保存从左向右和从右向左的最大值,空间换时间。O(n)O(n)O(n)class Solution(object): def trap(self, height): """ :type height: List[int] :rtype: int ...原创 2020-04-04 11:30:51 · 186 阅读 · 0 评论 -
20200402-丧丧的笔试两道题
笔试第二道题题目意思,给定数n,输入n个字符串,n个字符串可能存在相同的,统计每个字符串出现的个数,并降序排序。个数相同则按照字符串字典序排序(小的在前)拿到题觉着很简单,我觉着就是一道结构体排序,但是一直没能过,不知道原因#include<bits/stdc++.h>using namespace std;typedef struct { string sid;...原创 2020-04-02 21:49:37 · 174 阅读 · 0 评论 -
20200319-leetcode-1111. 有效括号的嵌套深度
传送门分析:所谓括号的深度,就是指的是(((((( 和))))))的深度。要想让深度最小,只需要将((((((这种连续的两个左、右括号放在不同的集合内。class Solution(object): def maxDepthAfterSplit(self, seq): """ :type seq: str :rtype: List[in...原创 2020-04-01 09:43:27 · 210 阅读 · 0 评论 -
20200329-剑指offer-面试题48. 最长不含重复字符的子字符串(滑动窗口)
传送门借鉴的评论区的思路,滑动窗口class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ maxx = 1 if len(s) == 0: ...原创 2020-03-31 22:04:30 · 296 阅读 · 0 评论 -
20200330-leetcode-面试题62. 圆圈中最后剩下的数字(约瑟夫环)
传送门直接建立一个队列进行模拟。class Solution(object): def lastRemaining(self, n, m): """ :type n: int :type m: int :rtype: int """ list = [] flag = [0 ...原创 2020-03-30 11:19:55 · 160 阅读 · 0 评论 -
20200329-剑指offer-面试题54. 二叉搜索树的第k大节点
传送门分析:二插搜索树的中序遍历结果是有序的,中序遍历的结果放在列表中,返回list[-k]即可。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# ...原创 2020-03-29 15:37:11 · 182 阅读 · 0 评论 -
20200319-leetcode-1162. 地图分析(BFS)
传送门class Solution(object): def maxDistance(self, grid): """ :type grid: List[List[int]] :rtype: int """ # 宽度n,长度为m n=len(grid) m=len(grid[...原创 2020-03-29 12:05:46 · 122 阅读 · 0 评论 -
20200319-leetcode-820. 单词的压缩编码(后缀匹配)
传送其实要排除列表中后缀相重合的字符,开始直接匹配后缀是否重合,超时了。思路是先把单词反转过来,就变成了匹配前缀了,但是这样需要遍历两遍,那么不妨先排序,然后只需要比较相邻的两个字母。class Solution(object): def minimumLengthEncoding(self, words): """ :type words: Lis...原创 2020-03-28 11:24:39 · 166 阅读 · 0 评论 -
20200326-剑指offer-面试题42. 连续子数组的最大和(dp)
题目传送class Solution {public: int maxSubArray(vector<int>& nums) { int dp[110000]; memset(dp,0,sizeof(dp)); //dp[i]表示以nums[i]结尾的前i项最大的和 dp[0]=nums[0]; ...原创 2020-03-26 09:58:32 · 132 阅读 · 0 评论 -
20200323-剑指offer-面试题49. 丑数
题目链接:链接class Solution {public: int nthUglyNumber(int n) { vector<int> res; res.push_back(1); if(n==1) return 1; int i=0,j=0,k=0; for(int id=0;id&l...原创 2020-03-23 22:01:56 · 125 阅读 · 0 评论 -
20200319-945. 使数组唯一的最小增量
添加链接描述class Solution {public: int minIncrementForUnique(vector<int>& A) { int len=A.size(); int move=0; if(len==0||len==1) return 0; sort(A.begin(),A.e...原创 2020-03-22 15:36:03 · 119 阅读 · 0 评论 -
20200319-leetcode-365. 水壶问题
题目链接倒水问题,应该用BFS可以做,不知道为什么写的代码一直没有返回的结果。wa当前两个水杯里的水为ans1和ans2,那么下一步的操作,只有8个状态,bfs八种状态即可。判断一个状态是否已经存在,可以用set判断,但是不会写set里面存放结构体的时候,如何比较于是就自己写了一个findHash,来判断是否存在先前的状态。,#include <iostream>#...原创 2020-03-21 11:34:00 · 149 阅读 · 0 评论 -
20200403-leetcode-剑指offer-面试题67 把字符串转换成整数
题目链接:class Solution {public: bool check(char s) { if (s == ' ' || s == '+' || s == '-' || (s >= '0' && s <= '9')) return true; return false; } //处理0-9 long long solveNumber(st...原创 2020-03-19 11:10:03 · 168 阅读 · 0 评论 -
20200306-leetcode-offer-面试题22 链表第k个结点
分析:第一次直接蛮力写的,看看分析后知道可以通过快慢指针来写,两个指针的间距为k,当快指针到结尾时,慢指针即在开始的位置先前的写法:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(...原创 2020-03-06 10:40:48 · 112 阅读 · 0 评论 -
20191027-leetcode-55跳跃游戏(思维)
分析:从前向后遍历nums数组,index记录从当前位置,以及当前以前位置跳,最多能跳多远。如果遍历到一个nums[i]==0nums[i]==0nums[i]==0的地方,倘若当前index不能大于i,证明不可能跳过这个地方。class Solution {public: bool canJump(vector<int>& nums) { ...原创 2019-10-26 11:12:29 · 184 阅读 · 0 评论 -
20191026-leetcode-19. 删除链表的倒数第N个节点
分析:链表删除操作。两种方法,可以两遍遍历删除,也可以一遍遍历(双指针方法)。第一种,两遍遍历。链表删除操作,记得在头结点前加一个预处理结点,用来防止链表就头结点一个元素,删除为空的情况!一开始就是因为忘记加预处理结点错了。/**c * Definition for singly-linked list. * struct ListNode { * int val; * ...原创 2019-10-25 11:04:03 · 136 阅读 · 0 评论 -
20191026-leetcode-62不同路径(简单排列)
class Solution {public: int uniquePaths(int m, int n) { if(m==0||n==0||m+n==1) return 0; return C(m-1,m+n-2); } int C(int m,int n) { long long res=1...原创 2019-10-25 10:15:01 · 182 阅读 · 0 评论 -
20191025-leetcode-61旋转链表(链表操作)
思路:(1)先把链表改成一个循环链表,方便后续操作。(2)遍历找到完成后链表的头结点,然后遍历记录即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL...原创 2019-10-24 13:42:33 · 137 阅读 · 0 评论 -
20191024-leetcode54螺旋矩阵(模拟)
思路:模拟,定义四个方向up,right,down,leftup,right,down,leftup,right,down,left的值分别为1,题目要求顺时针转,那么就是当前方向+1。倘若值为5的话,就变为1即可。一直朝着一个方向前进,一旦当前方向走不通了,那么转变方向继续走。class Solution {public: bool check(int col,int row,in...原创 2019-10-23 16:07:51 · 121 阅读 · 0 评论 -
20191023-leetcode-43字符串相乘(大数相乘,int转换成string)
代码还有误,在改string multiply(string num1, string num2) { if(num1==""||num2=="")return ""; string sum=""; int a[1100]; for(int i=num1.length()-1;i>=0;i--) { ...原创 2019-10-22 16:03:03 · 179 阅读 · 0 评论 -
20191022-leetcode-33搜索旋转排序数组
分析:时间复杂度O(logn)O(logn)O(logn),于是想到了二分查找。把一个字符串从mid处切开,那么必定一部分是有序的,另一部分是无序的。不断切割,直到待查找的数落在了一个有序的区间内,二分查找即可。class Solution {public: int search(vector<int>& nums, int target) { ...原创 2019-10-22 11:14:35 · 111 阅读 · 0 评论 -
20191021-leetcode-16最接近的三数之和
代码wa了,99 / 125 个通过测试用例,按照leetcode三数之和的思路写的,哪里想的不对?class Solution {public: int threeSumClosest(vector<int>& nums, int target) { // if(nums.size()<3) return 0; int minn...原创 2019-10-21 21:28:05 · 109 阅读 · 0 评论 -
20191020-leetcode-15三数之和
报错了,貌似设边界问题。class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ve; if(nums.size()<3) return ve; ...原创 2019-10-20 11:51:18 · 116 阅读 · 0 评论 -
20191019-leetcode-2两数相加(链表操作)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* a...原创 2019-10-19 16:18:04 · 134 阅读 · 0 评论