日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

single-number-ii

發布時間:2025/4/16 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 single-number-ii 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/**

*
* @author gentleKay
* Given an array of integers, every element appears three times except for one. Find that single one.
* Note:
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*
* 給定一個整數數組,除一個元素外,每個元素都出現三次。找到那個。
* 注:
* 您的算法應該具有線性運行時復雜性。你能在不使用額外內存的情況下實現它嗎?
*/

獲取map集合中鍵和值的三種方式:

https://www.cnblogs.com/strive-19970713/p/11282676.html

import java.util.*;/*** * @author gentleKay* Given an array of integers, every element appears three times except for one. Find that single one.* Note: * Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?* * 給定一個整數數組,除一個元素外,每個元素都出現三次。找到那個。* 注:* 您的算法應該具有線性運行時復雜性。你能在不使用額外內存的情況下實現它嗎?*/public class Main27 {public static void main(String[] args) {// TODO Auto-generated method stubint[] A = {3,3,3, 5,5,5, 6,6,6,7,7,8,7};System.out.println(Main27.singleNumber(A));}public static int singleNumber(int[] A) {Map<Integer, Object> map = new HashMap<>();for (int i=0;i<A.length;i++) {if (map.containsKey(A[i])) {map.put(A[i], 2); //一旦有map里面重復這個key, 就將它的值改成 2 或者其他值。}else {map.put(A[i], 1); //沒有包含這個key鍵的話, 就將這個鍵的值為1。}}//方法一 // Set<Integer> set = map.keySet(); // Iterator<Integer> it = set.iterator(); // while (it.hasNext()) { // Integer key = (Integer)it.next(); // Integer value = (Integer)map.get(key); // if (value == 1) { // return key; // } // }//方法二
//         Set<Integer> set = map.keySet(); // for (Integer i : set) { // Integer value = (Integer) map.get(i); // if (value == 1) { // return i; // } // }//方法三Set<Map.Entry<Integer, Object>> set = map.entrySet();for (Map.Entry<Integer, Object> m : set) {if ((Integer)m.getValue() == 1) {return m.getKey();}}return 0;}}

  

轉載于:https://www.cnblogs.com/strive-19970713/p/11282727.html

總結

以上是生活随笔為你收集整理的single-number-ii的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。