医院网站的建设,建设婚恋网站用什么搭建,网站升级维护,规划设计 网站 网站结构Problem: 137. 只出现一次的数字 II 文章目录 思路#x1f496; 哈希#x1f496; 位数统计#x1f496; DFA 状态机 思路
#x1f468;#x1f3eb; 参考
#x1f496; 哈希
⏰ 时间复杂度: O ( n ) O(n) O(n) #x1f30e; 空间复杂度: O ( n ) O(n) O(n)
cl… Problem: 137. 只出现一次的数字 II 文章目录 思路 哈希 位数统计 DFA 状态机 思路
参考 哈希
⏰ 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( n ) O(n) O(n)
class Solution {public int singleNumber(int[] nums) {MapInteger, Integer map new HashMap();for (int x : nums) {map.put(x, map.getOrDefault(x, 0) 1);}for (int x : map.keySet()) {if (map.get(x) 1) return x;}return -1;}
}// 作者宫水三叶位数统计
⏰ 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( 1 ) O(1) O(1)
class Solution {public int singleNumber(int[] nums){int[] cnt new int[32]; // 记录每个数的每个位出现了多少个 1for (int x : nums)for (int i 0; i 32; i)if (((x i) 1) 1)cnt[i];int ans 0;for (int i 0; i 32; i)if ((cnt[i] % 3 1) 1)// 3 个一组消去ans (1 i);return ans;}
}DFA 状态机
⏰ 时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( 1 ) O(1) O(1)
class Solution {public int singleNumber(int[] nums) {int one 0, two 0;for(int x : nums){one one ^ x ~two;two two ^ x ~one;}return one;}
}//作者宫水三叶