In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly analyzes the relevant knowledge points of how to understand Map and Set in the Java data structure, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to understand Map and Set in the Java data structure".
A number that appears only once in ①
Given a non-empty integer array, each element appears twice except for an element that appears only once. Find out the element that only appeared once.
Input: [2pyrrine 2pyr1]
Output: 1
Prime Minister, we may want to solve the problem directly with bit operations, but we can also use hash color bars to solve the problem.
Public int singleNumber (int [] nums) {int single = 0; for (int num: nums) {single ^ = num;} return single;}
Hashset has also easily solved this problem by putting elements in the entire array into set, because numbers that appear only once are removed from multiple occurrences of the same number.
Public int singleNumber (int [] nums) {HashSet set = new HashSet (); for (int I = 0; I
< nums.length; i++){ if (set.contains(nums[i])){ set.remove(nums[i]); }else { set.add(nums[i]); } } for (int i = 0; i < nums.length; i++){ if (set.contains(nums[i])){ return nums[i]; } } return -1; }②宝石与石头 给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。 输入:jewels = "aA", stones = "aAAbbbb" 输出:3 这道题和第一道题一样,这里是统计不同的个数。因为要区分大小写,所以我们将小写转大写,不影响我们做出判断 public int numJewelsInStons(String jewels,String stons){ stons.toUpperCase(Locale.ROOT).toCharArray(); HashSet set = new HashSet(); for (int i = 0; i < jewels.length(); i++){ set.add(jewels.charAt(i)); } int count = 0; for (char ch:stons.toCharArray()) { if(set.contains(ch)){ count++; } } return count; }③坏键盘打字 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。 输入 7_This_is_a_test _hs_s_a_es 输出 7TI 这道题我们要分两个set,一个setActual记录真实打出的字母,一个setBroken统计坏掉的字母,判断条件是符合的字母既不是包含setActual中已经存在的,也不是setActual与setBroken相同的字母。主要是同时对比setActual与setBroken。 public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str1 = scan.nextLine(); String str2 = scan.nextLine(); HashSet setActual = new HashSet(); for (char ch:str2.toUpperCase(Locale.ROOT).toCharArray()) { setActual.add(ch); } HashSet setBroken = new HashSet(); for (char ch: str1.toUpperCase(Locale.ROOT).toCharArray()) { if(!setActual.contains(ch) && !setBroken.contains(ch)){ setBroken.add(ch); System.out.print(ch);; } } }④复制带随机指针的链表 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点。 例如,如果原链表中有 X 和 Y 两个节点,其中 X.random -->Y . So the two corresponding nodes x and y in the copy list also have x.random-- > y.
Input: head = [[7], [13], [11], [10], [1]]
Output: [[7], [13], [11], [10], [1]]
Class Node {int val; Node next; Node random; public Node (int val) {this.val = val; this.next = null; this.random = null;}} public Node copyRandomList (Node head) {if (head = = null) return null; HashMap map = new HashMap (); Node cur = head While (cur! = null) {Node node = new Node (cur.val); map.put (cur,node); cur = cur.next;} cur = head; while (cur! = null) {map.get (cur). Next = map.get (cur.next); map.get (cur). Random = map.get (cur.random) Cur = cur.next;} return map.get (head);} about "how to understand Map and Set in Java data structure", this is the end of the introduction, more related content can be searched for previous articles, hope to help you answer questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.