技術分享

LeetCode

Leetcode

35. Search Insert Position

35. Search Insert Position

Leetcode 分享筆記35. Search Insert Position題目 class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ # 找到數字即跳出索引,未找到數字則找到排序之索引 for i in range(0,len(nums)): if (nums[i] == target): return i if (i ==...

1. Two Sum

1. Two Sum

Leetcode 分享筆記1. Two Sum題目 class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ # 第一輪:第一個數字進去,後面一個一個加,如果等於目標物,即停止,否則,進行第二輪(第二個數字) i = 0 while(i < len(nums)): for j in range(i, len(nums)): total = 0 if...

21. Merge Two Sorted Lists

21. Merge Two Sorted Lists

Leetcode 分享筆記21. Merge Two Sorted Lists題目 class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ ans = ListNode(None) prev = ans while l1 and l2: if(l1.val <= l2.val): prev.next = l1 l1 = l1.next...

160. Intersection of Two Linked Lists

160. Intersection of Two Linked Lists

Leetcode 分享筆記160. Intersection of Two Linked Lists題目 # 答題線索: 被找到的點後面的Linked List的值會相同,所以可以用Set存已經存在的Linked List,從中找出是否有接續一樣的值,如果有代表這個ListNode即是交點 nodeSet = set() while headA != None: if headA not in nodeSet: nodeSet.add(headA) headA = headA.next while headB: if headB...

817. Linked List Components

817. Linked List Components

Leetcode 分享筆記817. Linked List Components題目 count = 0 hasConnection = False while head: if head.val in nums: hasConnection = True elif hasConnection: count += 1 hasConnection = False head = head.next if hasConnection: count += 1 return...

234. Palindrome Linked List

234. Palindrome Linked List

Leetcode 分享筆記234. Palindrome Linked List題目 class Solution(object): def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ # 從頭開始與從尾巴開始,如果一樣代表回文。用兩個List來做比對 curr = head pre = None head_list = [] while curr != None: head_list.append(curr.val) next =...

divider2 3 scaled

Get Instant Access to Financial News & Advice

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation

77 / 100