日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

single-number-ii

發布時間:2025/4/16 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

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