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             if B[i] == 1:
13                 ret = i
14                 break
15         return ret
时间: 2024-09-29 10:03:52

leetcode:Single Number【Python版】的相关文章

leetcode Single Number python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for i in nums: if i not in B: #建

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

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 Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

LeetCode: Single Number [136]

[题目] 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)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

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

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] 思路:

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz