leetcode
freak_zone
这个作者很懒,什么都没留下…
展开
-
349. 两个数组的交集
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ res = list(set(nums1)&set(nums2)) return res原创 2021-12-08 12:04:04 · 91 阅读 · 0 评论 -
1002. 查找常用字符
先统计第一个字符串所有字符出现的次数,放在hash 中。接下来,把其他字符串里字符的出现次数也统计出来一次放在hashOtherStr中。然后hash 和 hashOtherStr 取最小值,这是本题关键所在,此时取最小值,就是 一个字符在所有字符串里出现的最小次数了。class Solution(object): def commonChars(self, words): """ :type words: List[str] :rtype: L原创 2021-12-08 11:56:08 · 331 阅读 · 0 评论 -
242. 有效的字母异位词
class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ dict = [0]*26 for i in range(len(s)): dict[ord(s[i])-ord('a')] += 1 for i in r原创 2021-12-08 10:53:06 · 103 阅读 · 0 评论 -
142. 环形链表 II
使用双指针,慢指针每次走一步,快指针走两步,如果快慢指针相遇,则有环。若有环,设定速度相同的双指针,一个从头节点出发,一个从快慢指针相遇节点出发,相遇点就是环的入口证明过程:https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.md# Definition for singly-linked list.# class Lis原创 2021-12-07 22:43:06 · 781 阅读 · 0 评论 -
面试题 02.07. 链表相交
根据快慢法则,走的快的一定会追上走得慢的。在这道题里,有的链表短,他走完了就去走另一条链表,我们可以理解为走的快的指针。那么,只要其中一个链表走完了,就去走另一条链表的路。如果有交点,他们最终一定会在同一个位置相遇# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = None原创 2021-12-07 21:19:28 · 118 阅读 · 0 评论 -
19. 删除链表的倒数第 N 个结点
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def removeNthFromEnd(self, head, n): """ :type head:.原创 2021-12-07 20:31:14 · 97 阅读 · 0 评论 -
24. 两两交换链表中的节点
按下图步骤倒过来# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def swapPairs(self, head): """ :type head:原创 2021-12-07 18:04:26 · 82 阅读 · 0 评论 -
206. 反转链表
迭代法 / 双指针法# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def reverseList(self, head): """ :type hea原创 2021-12-07 17:37:50 · 100 阅读 · 0 评论 -
707. 设计链表
单链表class Node: def __init__(self, val): self.val = val self.next = Noneclass MyLinkedList(object): def __init__(self): self._head = Node(0) self._count = 0 def get(self, index): """ :t原创 2021-12-07 12:04:08 · 325 阅读 · 0 评论 -
203. 移除链表元素
添加虚拟头结点# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def removeElements(self, head, val): """ :typ原创 2021-12-06 17:40:02 · 324 阅读 · 0 评论 -
59. 螺旋矩阵 II
class Solution(object): def generateMatrix(self, n): """ :type n: int :rtype: List[List[int]] """ matrix = [[0]*n for _ in range(n) ] up, down, left, right = 0, n-1, 0, n-1 number = 1 whi原创 2021-12-06 10:07:58 · 389 阅读 · 0 评论 -
209. 长度最小的子数组
209. 长度最小的子数组class Solution(object): def minSubArrayLen(self, target, nums): """ :type target: int :type nums: List[int] :rtype: int """ min_len = float('inf') for i in range(len(nums)):原创 2021-12-05 17:37:19 · 176 阅读 · 0 评论 -
977.有序数组的平方
class Solution(object): def sortedSquares(self, nums): """ :type nums: List[int] :rtype: List[int] """ len_nums = len(nums) i = 0 j = k = len_nums-1 res = [-1]*len_nums while(i<原创 2021-11-16 00:56:02 · 407 阅读 · 0 评论 -
0027.移除元素
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ slow = fast = 0 while (fast < len(nums)): if nums[fast] != val:原创 2021-11-16 00:37:04 · 207 阅读 · 0 评论 -
704.二分查找
704.二分查找class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ left = 0 right = len(nums)-1 while (left <= right):原创 2021-11-16 00:21:32 · 209 阅读 · 0 评论