Map和Set习题
Posted *平芜尽处是春山*
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Map和Set习题相关的知识,希望对你有一定的参考价值。
Map和Set习题
力扣138. 复制带随机指针的链表
class Solution
public Node copyRandomList(Node head)
if(head == null)
return null;
Map<Node,Node> nodeMap = new HashMap<> ();
for(Node x = head; x != null;x = x.next)
Node newNode = new Node(x.val);
nodeMap.put(x,newNode);
for(Node x = head; x != null;x = x.next)
nodeMap.get(x).next = nodeMap.get(x.next);
nodeMap.get(x).random = nodeMap.get(x.random);
return nodeMap.get(head);
运行截图:
力扣771. 宝石与石头
class Solution
public int numJewelsInStones(String jewels, String stones)
Set<Character> jewelSet = new HashSet<> ();
for(int i = 0;i < jewels.length();i++)
jewelSet.add(jewels.charAt(i));
int ret = 0;
for(int i = 0;i < stones.length();i++)
if(jewelSet.contains(stones.charAt(i)))
ret++;
return ret;
运行截图:
牛客网.旧键盘 (20)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main
public static void main(String[] args)
Scanner scanner=new Scanner(System.in);
String expectedStr=null;
String actualStr=null;
while(scanner.hasNext())
expectedStr=scanner.next();
actualStr=scanner.next();
expectedStr =expectedStr.toUpperCase();
actualStr =actualStr.toUpperCase();
Set<Character> actualSet=new HashSet<>();
for (int i = 0; i < actualStr.length(); i++)
actualSet.add(actualStr.charAt(i));
Set<Character> ret=new HashSet<>();
for (int i = 0; i < expectedStr.length(); i++)
char c=expectedStr.charAt(i);
if (! actualSet.contains(c))
if (ret.add(c))
System.out.print(c);
System.out.println();
运行截图:
以上是关于Map和Set习题的主要内容,如果未能解决你的问题,请参考以下文章