LeetCode
shell33168
这个作者很懒,什么都没留下…
展开
-
142. 环形链表 II + 287. 寻找重复数
142. 环形链表 II给定一个链表,返回链表开始入环的第一个节点。如果链表无环,则返回null。快慢指针走到相等,然后再从头节点,慢节点一起走,一直走到相交的地方class Solution: def detectCycle(self, head: ListNode) -> ListNode: slow,fast = head,head while fast and fast.next: fast = fast.ne..原创 2020-05-26 19:57:42 · 232 阅读 · 0 评论 -
bfs 和 dfs 汇总
1.dfs(深度优先搜索)就是暴力把所有的路径都搜索出来,它运用了回溯,保存这次的位置,深入搜索,都搜索完了便回溯回来,搜下一个位置,直到把所有最深位置都搜一遍,要注意的一点是,搜索的时候有记录走过的位置,标记完后可能要改回来;也可以递归处理左右子节点,不需要回溯257. 二叉树的所有路径给定一个二叉树,返回所有从根节点到叶子节点的路径。class Solution: def dfs(self,root,path,result): if not root:原创 2020-05-24 21:31:29 · 265 阅读 · 0 评论 -
双指针 滑动窗口题目汇总
3.无重复字符的最长子串给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。示例1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s)<=1: return len(s) res = 0...原创 2020-05-23 12:17:10 · 231 阅读 · 0 评论 -
构造二叉树汇总
Lc 105.从前序与中序遍历序列构造二叉树根据一棵树的前序遍历与中序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出前序遍历 preorder =[3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]递归求解,把中序遍历的值和下标存入hash表中:# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# ..原创 2020-05-22 13:45:23 · 201 阅读 · 0 评论 -
Lc42 接雨水
从左到右扫描记录当前位置的最大值,存入数组A从右到左扫描记录当前位置的最大值,存入数组BA,B对应位置的较小值减去雨水量,就是当前位置可以接的雨水class Solution: def trap(self, height: List[int]) -> int: if not height or len(height) == 0: return 0 lr,rl =[0]*len(height),[0]*len原创 2020-05-20 20:18:10 · 188 阅读 · 0 评论 -
Lc200 小岛数量
每次碰到‘1’就用DFS遍历联通的1,把遍历的1标记为已访问; dfs单独写成一个函数class Solution: def _init_2d(self,row,col): d= [] for i in range(row): d.append([]) for j in range(col): d[i].append(False) return d .原创 2020-05-19 19:20:01 · 148 阅读 · 0 评论 -
回文字符串判断汇总
先写一个单独验证回文字符串的函数A,然后判断的时候如果不相等则调用A判断 [i+1,j] 或者 [i,j-1]是否为回文class Solution: def judgestr(self,s,i,j): while i<j: if s[i] != s[j]: return False i+=1 j-=1 return True原创 2020-05-20 09:57:20 · 546 阅读 · 0 评论 -
LC146. LRU缓存
usedlist保存访问的顺序, capacity保存lru的容量,mapping保存映射的数组class LRUCache: def __init__(self, capacity: int): self.usedlist = [] self.capacity = capacity self.mapping = {} def get(self, key: int) -> int: if not self.map原创 2020-05-18 17:16:16 · 127 阅读 · 0 评论 -
课程安排汇总
lc 210把课程关系转成有向图,对图进行深度优先遍历,checked标记已经访问过的节点,order保存访问顺序也用到了回溯算法:原创 2020-05-18 10:33:28 · 240 阅读 · 0 评论 -
Lc 反转链表【递归】
206. 反转链表: def reverseList(head): pre = None p = head while p: nextNode = p.next p.next = pre pre = p p = nextNode return pre原创 2020-05-16 10:22:01 · 176 阅读 · 0 评论 -
前缀和的经典题目
leet560求和等于 K 的子数组数量(1)暴力解法(2)前缀和sum(i~j) = s(j) - s(i-1) = ks(j) - k = s(i-1)保存三个状态: sum,sum-k,cntdef subarraySum(self, nums: List[int], k: int) -> int: if not nums or len(nums) == 0: return 0 mapping = {0:.原创 2020-05-15 15:42:36 · 1114 阅读 · 1 评论 -
[LeetCode]Reverse Integer
Reverse Integer原创 2017-10-23 20:27:59 · 196 阅读 · 0 评论 -
[LeetCode]两行代码快速实现回文数的判断
Determine whether an integer is a palindrome. Do this without extra space.博主在上辅导班的时候就讲过类似的题目,当时以此各个位上的数,再奇偶判断…… 但是最近发现了一个毁三观的方法,总结起来就是:转换成字符串再判断!涉及到的几种方法: -String.valueOf(any type) -StringBuffer.rev原创 2017-10-24 01:06:49 · 542 阅读 · 0 评论 -
[LeetCode]罗马数字转为普通数字
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.首先,我们需要了解罗马数字的命名规则(感觉这样考有点没意思了): 罗马数字是阿拉伯数字传入之前使用的一种数码。罗马数字采用七个罗马字母作数字、即Ⅰ(1)、V(5)、X(10)、L(原创 2017-10-24 23:53:14 · 296 阅读 · 0 评论 -
[LeetCode]268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in line原创 2017-11-20 23:58:14 · 154 阅读 · 0 评论 -
[LeetCode]Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.平衡二叉树: 1、若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 2、若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 3、它是一棵空树或它的左右两个子树的高度原创 2017-11-16 23:20:48 · 150 阅读 · 0 评论 -
刷题中遇到的java的坑
1.字符的输入问题(牛客网用的比较多)nextLine() 会自动读取 字符串末尾的\n,因此以后刷题遇到输入数字的 直接用 scan.nextInt() ,nextDouble() , nextFloat() 输入字符串的 用 scan.next() 或者 scan.nextLine(),但是 nextLine()不能在next()或者nextInt(),nextDouble...原创 2019-01-21 09:01:32 · 353 阅读 · 0 评论 -
两个理解KMP的博文
http://kb.cnblogs.com/page/176818/ http://blog.csdn.net/hyjoker/article/details/51190726转载 2017-11-01 00:00:04 · 160 阅读 · 0 评论 -
二叉搜索树与双向链表(java版)
https://www.jianshu.com/p/7e30fb656d63转载 2019-03-15 22:57:05 · 373 阅读 · 0 评论 -
[LeetCode]Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.The brackets must close in the correct order, “()” and “()[]{}” are all **valid bu原创 2017-10-29 10:05:09 · 177 阅读 · 0 评论