编程语言
首页 > 编程语言> > 【手把手带你刷Leetcode力扣】1.算法 - 双指针

【手把手带你刷Leetcode力扣】1.算法 - 双指针

作者:互联网


141.环形链表

class Solution:
	# Time Complexity: O(N)
	# Space Complexity: O(1)
	def hasCycle(self, head: ListNode) -> bool:
		if head is None:
			return False
		slow = head
		fast = head
		while fast is not None and fast.next is not None:
			fast = fast.next.next
			slow = slow.next
			if slow == fast:
				return True
		
		retrun False

881.救生艇

class Solution:
	# Time Complexity: O(NlogN)
	# Space Complexity: O(1)
	def numRescueBoats(self, people: List[int], limit: int) -> int:
		if people is None or len(people) == 0:
			retrun 0
		people.sort()
		i = 0
		j = len(people) - 1
		res = 0
		while (i <= j):
			if people[i]+people[j] <= limit:
				i = i + 1
			j = j - 1
			res = res + 1
		return res

学习视频来源B站—爱学习的饲养员—手把手带你刷Leetcode力扣

标签:None,slow,people,手把手,fast,力扣,Complexity,Leetcode,指针
来源: https://blog.csdn.net/ZZBOOM_/article/details/120689431