7.4 - medium总结8

133. Clone Graph: 先克隆所有的node,再克隆所有的边
134. Gas Station: 基于一个推论:如果gas总量大于cost总量,那么总是能够找到一个起始点满足环绕一圈的需求
137. Single Number II: 这道题的想法就是,对于一个32bit表示的int,对于每一位,比如说0号位,把所有数的0号位加起来mod3的余数,就是那个单独的数在这一位上的值,不可能出现2,只会剩下0或者1

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = 0;
        for i in range(32):
            s = 0
            for j in range(len(nums)):
                if (nums[j] >> i) & 1 == 1:
                    s += 1
                    s %= 3
            if s != 0:
                res += s << i;
        return self.convert(res)
    
    def convert(self, x):
        if x >= 2**31:
            x -= 2**32
        return x

138. Copy List with Random Pointer: 这题和clone graph很类似,一部分一部分clone
139. Word Break: 典型的dp,if s[j:i] in wordDict and dp[j]
142. Linked List Cycle II: 一道证明题,没啥做的
143. Reorder List: 切中点,翻转后面后半截然后依次链接
144. Binary Tree Preorder Traversal: 基本结构,熟练掌握
147. Insertion Sort List: insertion sort就是取出一个值,然后和这个值前面的所有值(已经sorted)进行对比并且插入相应的位置。
148. Sort List: 对链表进行排序的时候,用merge sort,如果不考虑stack的空间,可以认为是constant space,再手写一遍,熟悉下merge sort

你可能感兴趣的:(7.4 - medium总结8)