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 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?

给出一个数组,除了一个元素出现了一次,其余元素均出现了3次。找出这个元素。

给出3中解法

  1. 位操作。取所有元素的某一位上的和,对3取模,余数便是目标元素在该位的值。
  2. 用map。重复元素第二次塞进map时,value置为Long的最大值。第三次塞入的时候把这个entry删掉。最后只剩一个元素便是目标元素了。
  3. 排序。找只重复的那个。

给出我的AC代码:

package bit.manipulation;

import java.util.*;

/**
 * project  : LeetCodeOJ
 * package  : bit.manipulation
 * author   : lvsheng
 * date     : 16/7/31 下午2:28
 */
public class SingleNumber2 {

	public static void main(String[] args) {
		int[] arr  = {-1, 5, 5, 5, 7, 7, 7, 90, 90, 90};
		int[] arr2 = {7};
		int[] arr3 = {0, 0, 0, 5};

		System.out.println(singleNumber(arr));
		System.out.println(singleNumber(arr2));
		System.out.println(singleNumber(arr3));

		System.out.println(singleNumber2(arr));
		System.out.println(singleNumber2(arr2));
		System.out.println(singleNumber2(arr3));

		System.out.println(singleNumber3(arr));
		System.out.println(singleNumber3(arr2));
		System.out.println(singleNumber3(arr3));
	}

	public static int singleNumber(int[] nums) {
		// if(nums.length == 1) return nums[0];
		int result = 0;
		for (int i = 0; i < 32; i++) {
			int cnt = 0;
			int bit = 1 << i;
			for (int num : nums) {
				if ((num & bit) != 0) {
					cnt++;
				}
			}
			cnt %= 3;
			if (cnt == 1) result |= bit;
		}
		return result;
	}

	public static int singleNumber2(int[] nums) {

		Map<Integer, Long> map = new HashMap<>(nums.length);
		for (int n : nums) {
			if (map.get(n) != null) {
				Long val = map.get(n);
				if (val == Long.MAX_VALUE) map.remove(n);
				else map.put(n, Long.MAX_VALUE);
			} else map.put(n, (long) n);
		}
		List<Integer> list = new ArrayList<>(map.keySet());
		return list.get(0);
	}

	public static int singleNumber3(int[] nums) {
		if (nums.length == 1) return nums[0];
		Arrays.sort(nums);
		int result = nums[0];
		int cnt    = 1;
		for (int i = 1; i < nums.length; i++) {
			if (nums[i] == result) cnt++;
			else {
				if (cnt == 3) {
					result = nums[i];
					cnt = 1;
				} else {
					break;
				}
			}
		}
		return result;
	}
}
时间: 2024-08-04 11:12:28

LeetCodeOJ 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

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

[leedcode 137] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. public class Solution { public int singleNumber(int[] nums) { //int的32个bit逐个处理,每一个bit进行相加,然后mod3,即可判断多余的一位是1或0.此思想同样适用于均出现三次,一个出现两次场景 int res=0; for(i