![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
双指针法
princey2100
这个作者很懒,什么都没留下…
展开
-
Leetcode 剑指 Offer 58 - II. 左旋转字符串 LCOF - Python
class Solution: def reverseLeftWords(self, s: str, n: int) -> str: m = len(s) temp = list(s) temp.extend(['']*n) front, rear = 0, m for i in range(0,n): temp[rear] = temp[front] rear +.原创 2022-03-22 20:35:19 · 305 阅读 · 0 评论 -
Leetcode 151.颠倒字符串中的单词 Reverse Words in a String - Python 双指针法
class Solution: def reverseWords(self, s: str) -> str: def trim_space(s:str): res = [] front = 0 rear = len(s)-1 while s[front] == ' ': front += 1 while s[rear] == .原创 2022-03-22 17:17:09 · 560 阅读 · 0 评论 -
Leetcode 18.四数之和 4Sum - Python 双指针法
class Solution: def fourSum(self, nums: List[int], target: int) -> List[List[int]]: result = [] nums.sort() length = len(nums) for i in range(length-3): if i == 0 or nums[i] != nums[i-1]: #当nums[i]与nums[.原创 2022-03-02 12:16:55 · 334 阅读 · 0 评论 -
双指针法为什么不适用于Leetcode 1.Two Sum 两数之和? - Python
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: result = [] nums.sort() for i in range(len(nums)): front = i rear = len(nums)-1 while front < rear: .原创 2022-02-28 21:49:34 · 178 阅读 · 0 评论 -
Leetcode 15. 三数之和 3Sum - Python 双指针法
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: result = []#将数组从小到大排序,便于双指针法的实施 nums.sort() for i in range(len(nums)): if i == 0 or nums[i] != nums[i-1]: #第一个元素 或 跟前一个i不相同的i才能进入(去重) .原创 2022-02-28 21:25:49 · 350 阅读 · 0 评论 -
Leetcode 9. 回文数 Palindrome Number - Python 双指针法
class Solution: def isPalindrome(self, x: int) -> bool: to_string = str(x) i = 0 q = len(to_string)-1 front = to_string[i] rear = to_string[q] while front == rear: if i==q or abs(i-q)=.原创 2022-02-26 13:57:47 · 359 阅读 · 0 评论 -
Leetcode 142. 环形链表 II Linked List Cycle II - Python 双指针法
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def detectCycle(self, head: ListNode) -> ListNode: fast, slow = head, head #让快慢指针.原创 2022-12-06 22:57:02 · 332 阅读 · 0 评论 -
Leetcode 面试题 02.07. 链表相交 Intersection of Two Linked Lists LCCI - Python 双指针法
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: curA, curB =.原创 2022-02-24 22:19:19 · 184 阅读 · 0 评论 -
Leetcode 19.删除链表的倒数第 N 个结点 Remove Nth Node From End of List - Python 双指针法Vs笨蛋法
笨蛋法# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def removeNthFromEnd(self, head: ListNode, n: int) : size = 1 c原创 2022-02-24 17:11:36 · 58 阅读 · 0 评论