137. Single Number II

把每一位分别加起来,超过十也存起来,然后取3的余数,多余的部分就是单独那个的。

未操作:

取某一位

(x >>i)&1

先把这个数x往右移i位,然后想要取的那一位就位于个位数上,和1去且意思是和0000...0001取且,所以只保留了最后一位

 1     public int singleNumber(int[] nums) {
 2         if(nums.length == 0) {
 3             return 0;
 4         }
 5         int[] digits = new int[32];
 6         for(int i = 0; i < 32; i++) {
 7             for(int j = 0; j < nums.length; j++) {
 8                 digits[i] += (nums[j] >> i) & 1;
 9             }
10         }
11         int res = 0;
12         for(int i = 0; i < 32; i++) {
13             res += (digits[i] % 3) << i;
14         }
15         return res;
16     }
时间: 2024-08-08 13:54:28

137. Single Number II的相关文章

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

LeetCodeOJ 137. Single Number II 一题三解

原题链接:https://leetcode.com/problems/single-number-ii/ 题目如下: 137. Single Number II QuestionEditorial Solution My Submissions Total Accepted: 91049 Total Submissions: 235598 Difficulty: Medium Given an array of integers, every element appears three time

137. Single Number II(js)

137. Single Number II Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/single-number-ii/ 题目描述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 co

137.Single Number II(法1排序法2STL容器map哈希法3位运算法4改进的位运算)

Given an array of integers, every element appears three timesexcept for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory? HideTags Bit Manipulation #pragma once

Leetcode 137. Single Number II JAVA语言

Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题意:一个数组中除

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 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 im

137. Single Number II (Bit)

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? 思路: Step1:从Single Number我们知道,通过易或操作可以用

LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 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

【Leetcode】137. Single Number II

题目: 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? 题解: The code seems tricky and hard