LeetCode每周刷题(2019.7.1-2019.7.7)

69. x 的平方根

利用二分法,如果中值平方大于x则在左边寻找,如果中值+1的平方小于等于x则在右边寻找,如果中值平方小于等于x且中值+1的平方大于x,则该中值就是返回的整数平方根。

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x == 0:
            return 0
        i = 1
        j = x
        mid = int(x/2)
        while i <= j:
            if mid**2 <= x and (mid+1)**2 > x:
                return mid
            elif mid**2 > x:
                j = mid - 1
                mid = int((i+j)/2)
            elif (mid+1)**2 <= x:
                i = mid + 1
                mid = int((i+j)/2)

160. 相交链表

设置两个指针pA,pB指向两个链表头,遍历两次,如果pA指向了None,则指到headB,如果pB指向了None,则指到headA,两次遍历结束后,消除了长度差,此时继续遍历,如果得到了pA==pB,则该节点为相交节点,返回该节点,如果遍历结束pA和pB都指向了None,则没有相交节点,返回None。

ç¸äº¤é¾è¡¨.png

图片参考题解:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        pA = headA
        pB = headB
        for _ in range(2):
            while pA != None and pB != None:
                pA = pA.next
                pB = pB.next
            if pA == None:
                pA = headB
            if pB == None:
                pB = headA
        while pA != None and pB != None:
            if pA == pB:
                return pA
            pA = pA.next
            pB = pB.next
        return None

1. 两数之和

遍历一次数组,利用哈希表,将遍历过的值与索引存入哈希表中,遍历时查找目标值与当前值的差是否在哈希表中,如果在,则输出差和当前值的索引,直到遍历结束,输出None。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        d = {}
        for i,v in enumerate(nums):
            if target-v in d:
                return [i, d[target-v]]
            d[v] = i
        return None

242. 有效的字母异位词

python可以判断两个字典是否相等,所以建立两个字典(哈希表),统计两个字符串出现的字符频次,如果两个字典相等,则返回True,否则返回False。题解中写的是另一种方法,只需要建立一个哈希表,对应26个字母的数量,遍历s时在每个字母的数量上加一,遍历t时在每个字母的数量上减一,如果该哈希表的每个映射值都为0,则返回True,否则返回False。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        d1 = {}
        d2 = {}
        if len(s) != len(t):
            return False
        for i in s:
            d1[i] = d1.get(i, 0) + 1
        for j in t:
            d2[j] = d2.get(j, 0) + 1
        if d1 == d2:
            return True
        else:
            return False

232. 用栈实现队列

使用python的list,push操作在列表尾加上元素,pop操作为返回list[0]并更新列表为list[1:],peek操作返回list[0],empty操作判断列表长度是否为0。

class MyQueue(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue = []

    def push(self, x):
        """
        Push element x to the back of queue.
        :type x: int
        :rtype: None
        """
        self.queue = self.queue + [x]

    def pop(self):
        """
        Removes the element from in front of queue and returns that element.
        :rtype: int
        """
        key = self.queue[0]
        self.queue = self.queue[1:]
        return key
        
    def peek(self):
        """
        Get the front element.
        :rtype: int
        """
        return self.queue[0]

    def empty(self):
        """
        Returns whether the queue is empty.
        :rtype: bool
        """
        if len(self.queue) == 0:
            return True
        else:
            return False


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值