力扣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();
}
}
运行截图: