MapAndSet例题

771.给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/jewels-and-stones
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i));
        }
        //遍历 stones,判定当前字符是否存在
        int count = 0;
        for (int i = 0; i < stones.length(); i++) {
            if(set.contains(stones.charAt(i))){
                count++;
            }
        }
        return count;
    }
}

138.给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/copy-list-with-random-pointer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/

class Solution {
        public Node copyRandomList(Node head) {
            //1.先遍历旧链表,把每个旧节点都创建对应的新节点,并且插入到 Map 中
            Map<Node,Node> map = new HashMap<>();
            for(Node cur = head; cur != null; cur = cur.next){
                map.put(cur,new Node(cur.val));
            }
            //2.再次遍历旧链表,构造 next / random 的指向
            for(Node oldNode = head; oldNode != null; oldNode = oldNode.next){
                //把 next 相互连接
                Node newNode = map.get(oldNode);
                Node newNodeNext = map.get(oldNode.next);
                newNode.next = newNodeNext;
                //把 random 相互连接
                Node newNodeRandom = map.get(oldNode.random);
                newNode.random = newNodeRandom;
            }
            return map.get(head);
        }
    }
根据引用和引用的内容,precision(查准率)和recall(召回率)可以通过混淆矩阵中的TP、FP和FN来计算。TP表示将正样本预测为正样本,FP表示将负样本预测为正样本,FN表示将正样本预测为负样本。 假设有一个二分类问题,混淆矩阵如下所示: ``` 预测(列) / 标签(行) 正样本 负样本 正样本 TP FN 负样本 FP TN ``` Precision(查准率)可以通过以下公式计算: ``` P = TP / (TP + FP) ``` Recall(召回率)可以通过以下公式计算: ``` R = TP / (TP + FN) ``` 例如,对于混淆矩阵中的例子: ``` label: 0 0 1 0 1 1 0 1 0 1 predict: 1 0 1 0 0 1 0 0 0 1 ``` 可以计算得到:TP=3,FP=1,FN=2。 因此,Precision为P = 3 / (3 + 1) = 0.75,Recall为R = 3 / (3 + 2) = 0.6。 F1值是综合评价Precision和Recall的一个指标,可以通过以下公式计算: ``` F1 = 2 * (P * R) / (P + R) ``` 所以F1值为F1 = 2 * (0.75 * 0.6) / (0.75 + 0.6) ≈ 0.67。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [二分类中的precision,recall,F1值计算(举例)](https://blog.csdn.net/buchidanhuang/article/details/100833055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wmx-98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值