力扣题:哈希表的查找、插入及删除-10.25

力扣题-10.25

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:205. 同构字符串

解题思想:同昨天的题目,维护两个字符串的映射关系即可

在这里插入图片描述

class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        ch12ch2 = dict()
        ch22ch1 = dict()
        if len(s) != len(t):
            return False

        for ch1,ch2 in zip(s,t):
            if (ch1 in ch12ch2 and ch12ch2[ch1]!=ch2) or (ch2 in ch22ch1 and ch22ch1[ch2]!=ch1):
                return False
            ch12ch2[ch1]=ch2
            ch22ch1[ch2]=ch1
        return True
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char> ch12ch2;
        unordered_map<char, char> ch22ch1;
        int len = t.length();
        for(int i =0;i<len;++i){
            char ch1 = s[i],ch2 = t[i];
            if((ch12ch2.count(ch1) && ch12ch2[ch1] != ch2)||(ch22ch1.count(ch2) && ch22ch1[ch2] != ch1)){
                return false;
            }
            ch12ch2[ch1]=ch2;
            ch22ch1[ch2]=ch1;
        }
        return true;
    }
};

力扣题2:138. 复制带随机指针的链表

解题思想:第一遍遍历先创建与原先节点相对应的节点链表,第二遍遍历填写random指针的地址

在这里插入图片描述

"""
# Definition for a Node.
class Node:
    def __init__(self, x, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random
"""

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        temp = head
        node_list = dict()
        new_head = Node(0,None,None)
        pre_node = new_head

        while temp!=None:
            new_node = Node(temp.val,None,None)
            pre_node.next = new_node
            pre_node = pre_node.next
            node_list[temp]=new_node
            temp = temp.next

        temp = head
        temp2 = new_head.next
        while temp!=None:
            if temp.random!=None:
                temp2.random = node_list[temp.random]
            temp = temp.next
            temp2 = temp2.next
        return new_head.next
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        Node* temp = head;
        unordered_map<Node*, Node*> node_list;
        Node* new_head = new Node(0);
        Node* pre_node = new_head;
        while(temp!=NULL){
            Node* new_node = new Node(temp->val,NULL,NULL);
            pre_node->next = new_node;
            pre_node = pre_node->next;
            node_list[temp] = new_node;
            temp = temp->next;
        }
        temp = head;
        Node* temp2 = new_head->next;
        while(temp!=NULL){
            if(temp->random!=NULL){
                temp2->random = node_list[temp->random];
            }
            temp = temp->next;
            temp2 = temp2->next;
        }
        return new_head->next;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值