力扣
virusos
混子king
展开
-
二分查找、快速排序、冒泡排序、选择排序
class Solution: def search(self, nums: List[int], target: int) -> int: l = 0 r = len(nums) - 1 while l <= r: mid = l + (r - l) // 2 if nums[mid] == target: return mid eli原创 2021-05-26 23:17:00 · 70 阅读 · 0 评论 -
142. 环形链表 II
题目:给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。思路:哈希表,把遍历过的节点加入表中快慢指针设快指针为f,慢指针为s,非环形链表的长度为a(不包括环形链表的第一个节点),环形链表总长度为b(包括环形链表的第一个节点)。f每次走2,s每次走1.则f和s第一次相遇时,有:f=2s(因为f的速度为s两倍)f=a+ib(在环里绕了i圈)s=a+jb(在环里绕了j圈)第二、三式子合一下变成f=s+(j-i)b,这里j-i等于多少不关心,可以用n代替,也就是原创 2021-05-08 20:38:12 · 88 阅读 · 1 评论 -
6. Z 字形变换
思路:遍历字符串,用一个列表去模拟“变换后的每一行”,把遍历到的字符填入列表中它应在的行数。定义一个flag,用于记录当前填入行的方向是向下还是向上。在遇到最后一行或第一行的时候,反转flag的值。class Solution: def convert(self, s: str, numRows: int) -> str: if numRows == 1: return s line = ["" for _ in range(numRo原创 2021-05-08 16:29:24 · 57 阅读 · 0 评论 -
338. 比特位计数
暴力解法:class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ ans = [] for i in range(0, num + 1): n = i print (n) if n == 1:原创 2021-04-22 19:00:39 · 76 阅读 · 0 评论 -
【力扣】283. 移动零
283. 移动零关键词:双指针左指针指向排列好的部分的末尾右指针向右查询不为0的元素class Solution { public void moveZeroes(int[] nums) { int n = nums.length; int i = 0, j = 0; while(j < n){ if(nums[j] != 0){ int tmp = nums[j];原创 2021-01-30 21:58:36 · 51 阅读 · 0 评论 -
【力扣】11. 盛最多水的容器
11. 盛最多水的容器这次用java做的做法好奇妙,分析还没看完,想知道为什么这样一定能求出最大值class Solution { public int maxArea(int[] height) { // 双指针,绝了 int l = 0; int r = height.length - 1; // java中数组长度用h.length得到 int ans = 0; while (l < r){原创 2021-01-23 23:18:38 · 116 阅读 · 0 评论 -
【力扣】350. 两个数组的交集 II
350. 两个数组的交集 II注意,题中有个进阶要求:如果 nums2 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?这时推荐哈希表解法。因为无法一次加载nums2的元素到内存中的话,无法对nums2进行排序;而哈希表算法中只对nums2进行查询操作。解法一:先排序,然后用指针class Solution(object): def intersect(self, nums1, nums2): """ 先排序,后指针原创 2021-01-01 21:28:08 · 228 阅读 · 0 评论 -
【力扣】136. 只出现一次的数字
136. 只出现一次的数字class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int 位运算 -- 异或 """ single = 0 for i in nums: single = single ^ i return sin原创 2020-12-30 22:29:24 · 77 阅读 · 0 评论 -
【力扣】20. 有效的括号
20. 有效的括号这是我刷了几天题以来第一道标着“简单”同时我也觉得还算简单的题,蛋疼class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] # 抄别人的,本来我是用if一个个判断的,好傻 mapping = { ')': '(',原创 2020-12-26 13:01:10 · 92 阅读 · 0 评论 -
【力扣】14. 最长公共前缀
14. 最长公共前缀class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ length = len(strs) if length == 0: return "" prefix = strs[0]原创 2020-12-26 12:22:05 · 99 阅读 · 0 评论 -
13. 罗马数字转整数
题目13. 罗马数字转整数我的答案(比较弱智2333)class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ table = { 'I': 1, 'V': 5, 'X': 10, 'L': 50,原创 2020-12-22 21:40:54 · 78 阅读 · 2 评论 -
【力扣】746. 使用最小花费爬楼梯
题目746. 使用最小花费爬楼梯我的答案(虽然题目要求不新创矩阵但是其他解法我还不会哈)class Solution(object): def minCostClimbingStairs(self, cost): """ :type cost: List[int] :rtype: int """ n = len(cost) minCost = [0] * n minCost[1] =原创 2020-12-22 21:13:33 · 79 阅读 · 0 评论 -
【力扣】48. 旋转图像
题目48. 旋转图像我的答案(虽然题目要求不新创矩阵但是其他解法我还不会哈)class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: None Do not return anything, modify matrix in-place instead. """ n = len(matrix原创 2020-12-19 22:33:01 · 132 阅读 · 1 评论 -
【力扣】389.找不同
题目389. 找不同我的答案(不是最精简的)class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ list_s = list(s) for i in t: try: list原创 2020-12-18 20:05:06 · 137 阅读 · 1 评论 -
【力扣】49. 字母异位词分组
题目49. 字母异位词分组我的答案(不是最精简的)class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ table = dict() for current_str in strs: if ''.join(sort原创 2020-12-17 19:55:44 · 107 阅读 · 0 评论