【leetcode78】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?

Subscribe to see which companies asked this question

思路:

  • 设置一个32位的数组,然然后对数组,Array【i】代表i位的1个数,统计整个数组nums【】的数字,对于每个Array【i】做mod3的运算
  • 遍历nums【】数组时候,是左移&1取出,加到Array【i】上面,然后mod3
  • 最后右移加到result上面

代码:

// Single Number II
// 方法1,时间复杂度O(n),空间复杂度O(1)
public class Solution {
    public int singleNumber(int[] nums) {
        final int W = Integer.SIZE; // 一个整数的bit数,即整数字长
        int[] count = new int[W];  // count[i]表示在在i位出现的1的次数
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < W; j++) {
                count[j] += (nums[i] >> j) & 1;
                count[j] %= 3;
            }
        }
        int result = 0;
        for (int i = 0; i < W; i++) {
            result += (count[i] << i);
        }
        return result;
    }
};

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

时间: 2024-10-08 03:36:50

【leetcode78】Single Number II的相关文章

【LeetCode】Single Number II (3 solutions)

Single Number II Given an array of integers, every element appears threetimes 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记录次数 class So

【LeetCode】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? 思路: 用 ones, twos, threes 分别记录数字在二

【Leetcode长征系列】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? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除

【LeetCode】Single Number (2 solutions)

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? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Single Number I &amp; II

Single Number I : 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? Solution: 解法不少,贴一种: 1 cla

【LeetCode】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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

【LeetCode从零单刷】Single Number II

题目: Given an array of integers, every element appears three times except for one. Find that single one. 解答: 如果是挑出非偶数次数的元素,可以利用每个元素依次异或的方法.但是非奇数次数(3,5,7,9--)的元素? 如果要快,可以hash_map存储.然后遍历一遍hash_map找到Single Number. 换种思路,按位找不同的Single Number对应的位.细节上: bit =

【leetcode79】Single Number III

题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: G

【Leetcode】Ugly Number II

题目链接:https://leetcode.com/problems/ugly-number-ii/ 题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first