leetcode Single Number II python

Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

python code:

class Solution:
# @param {integer[]} nums
# @return {integer}
def singleNumber(self, nums):
  B={}
  for eachint in nums:
    if eachint not in B:      #建立一个dict,遍历list,统计每个key出现的次数
      B[eachint]=1
    else:
      B[eachint]+=1
  for eachint in B:
    if B[eachint] is not 3:    #找出出现次数不为3的key,return
      break
  return eachint

时间: 2024-10-15 17:51:25

leetcode Single Number II python的相关文章

leetCode: Single Number II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode——Single Number II(找出数组中只出现一次的数2)

问题: 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?   Single Number I 升级版,一个数组中其它数出现了

[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? Hide Tags Bit Manipulation 数组中的数均出现3次,

LeetCode Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. 题意:有一个数组,只有一个数出现一次,其他的都出现三次,找出一次的数 思路:首先我们想到是每次把每一位二进制上1的个数都mod3,然后就能找出一个的了,但是这样空间太大了,所以我们想能记录每一次出现三次的时候就清0,那么我们需要先记录1次的,然后记录2次的,这样就能求出三次的了,最后再更新出现1次和

LeetCode——Single Number II

Description: Given an array of integers, every element appears three times except for one. Find that single one. 只有一个出现一次的数字,其他的都出现了3次,找出出现一次的那个数字. public class Solution { public int singleNumber(int[] nums) { Map<Integer, Integer> map = new HashMap

[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? 这道题是之前那道单独的数字的延伸,那道题的解法就比较独特,是利用计算机按位储

LeetCode Single Number II 单元素2

题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这个位肯定为1. 22ms 1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) { 4 int times[32]={0}; 5 for(int i=0; i<nums.size(); i++) 6 fo

leetcode:Single Number【Python版】

1.用双重循环逐个遍历(超时) 2.用list B的append和remove函数(超时) 3.用dict B(AC) 1 class Solution: 2 # @param A, a list of integer 3 # @return an integer 4 def singleNumber(self, A): 5 B = {} 6 for i in A: 7 if i not in B: 8 B[i] = 1 9 else: 10 B[i] = 2 11 for i in B: 12

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路: