leetcode刷题(10.8总结)

1、移除链表元素

题目描述:https://leetcode.cn/problems/remove-linked-list-elements/

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if head is None:
            return head
        
        # removeElement方法会返回下一个Node节点
        head.next = self.removeElements(head.next, val)
        if head.val == val:
            next_node = head.next 
        else:
            next_node = head
        return next_node

2、同构字符串

题目描述:https://leetcode.cn/problems/isomorphic-strings/


class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        slen = len(s)
        tlen = len(t)
        if slen != tlen:
            return False
        dic1 = {}
        dic2 = {}
        for i in range(slen):
            if s[i] in dic1 and dic1[s[i]] != t[i]:
                return False
            if t[i] in dic2 and dic2[t[i]] != s[i]:
                return False
            if s[i] not in dic1:
                dic1[s[i]] = t[i]
            if t[i] not in dic2:
                dic2[t[i]] = s[i]
        return True

3、反转链表

题目描述:https://leetcode.cn/problems/reverse-linked-list/

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        dummy = None
        pre = dummy
        cur = head
        while cur:
            # 记录下nxt节点,避免丢失
            nxt = cur.next
            # cur节点指向pre,反转cur节点的指向
            cur.next = pre
            # pre前移一位;cur前移一位
            pre = cur
            cur = nxt
        return pre

4、存在重复元素一

题目描述:https://leetcode.cn/problems/contains-duplicate/

class Solution:
		    def containsDuplicate(self, nums: List[int]) -> bool:
		        numset={}
		        for i in nums:
		            if i not in numset:
		                numset[i]=1
		            else:
		                return True
		        return False

4、存在重复元素二

题目描述:https://leetcode.cn/problems/contains-duplicate-ii/

class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        hash={}
        for i in range(len(nums)):
            if(nums[i] not in hash):
                hash[nums[i]]=i
            else:
                if(i-hash[nums[i]]<=k):
                    return True
                else:
                    hash[nums[i]]=i
        return False

5、用队列实现栈

题目描述:https://leetcode.cn/problems/implement-stack-using-queues/

class MyStack {
public:
    queue<int> que1;
    queue<int> que2; // 辅助队列,用来备份
    /** Initialize your data structure here. */
    MyStack() {

    }

    /** Push element x onto stack. */
    void push(int x) {
        que1.push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int size = que1.size();
        size--;
        while (size--) { // 将que1 导入que2,但要留下最后一个元素
            que2.push(que1.front());
            que1.pop();
        }

        int result = que1.front(); // 留下的最后一个元素就是要返回的值
        que1.pop();
        que1 = que2;            // 再将que2赋值给que1
        while (!que2.empty()) { // 清空que2
            que2.pop();
        }
        return result;
    }

    /** Get the top element. */
    int top() {
        return que1.back();
    }

    /** Returns whether the stack is empty. */
    bool empty() {
        return que1.empty();
    }
};

6、翻转二叉树

题目描述:https://leetcode.cn/problems/invert-binary-tree/

class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root == None:
            return root
        Q = deque([root])
        while Q:
            r = Q.popleft()
            if r.left or r.right:
                r.left, r.right = r.right, r.left
                if r.left and r.right:
                    Q.append(r.left)
                    Q.append(r.right)
                elif r.right and not r.left:
                    Q.append(r.right)
                else:
                    Q.append(r.left)
        return root

7、汇总区间

题目描述:https://leetcode.cn/problems/summary-ranges/

class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        n = 0
        res = []
        while n < len(nums):
            if n+1 < len(nums) and nums[n]+1 == nums[n+1]:
                m = n
                while n+1 < len(nums) and nums[n]+1 == nums[n+1]:
                    n += 1
                res.append("{}->{}".format(nums[m],nums[n]))
            else:
                res.append(str(nums[n]))
            n +=1
        return res



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值