数组中含有n个数,其中一个数只出现1次,其余个数均出现3次,就只出现1次的数。
首先应该想到的就是计数法,先对数组排序,然后统计每个数出现的次数,找出出现次数为1个数;
更高级一点的方法还是用位运算,充分发掘数字二进制中的0和1出现的规律。一个数字是由0和1组成的,如果这个数字出现3次,那么这个数字中各个位上0和1出现的次数也应该是3次。按照这样统计数组中所有数的各个位1的个数,如果个数是1个倍数,这所求数字该位不为1。
举例如下:
数组: 1, 3, 1, 5, 1, 6, 5, 6, 6, 5
对应的...
分类:
其他好文 时间:
2014-06-20 12:45:44
阅读次数:
185
数组含有n个数,其中有一个数只出现1次,其余的数都出现两次,求只出现一次的数。 这个主要考察的是位运算中的异或运算的性质-----当两个相等的数做异或运算他们的值为0(a^a = 0)。本题中对数组中所有的数做异或,那么最后异或的结果就是只出现1次的数。思想很简单代码如下:...
分类:
其他好文 时间:
2014-06-20 10:02:03
阅读次数:
281
Find the contiguous subarray within an array
(containing at least one number) which has the largest sum.For example, given
the array[?2,1,?3,4,?1,2,1,...
分类:
其他好文 时间:
2014-06-20 08:59:38
阅读次数:
235
Implement an algorithm to print all valid (
properly opened and closed) combinations of n-pairs of parentheses.思路:比如 n = 3,
((())) 就是一个valid combinati...
分类:
其他好文 时间:
2014-06-11 13:19:00
阅读次数:
306
原题地址:https://oj.leetcode.com/problems/plus-one/题意:Given
a non-negative number represented as an array of digits, plus one to the
number.The digits are...
分类:
编程语言 时间:
2014-06-11 11:25:27
阅读次数:
304
成员变量buf是存储数据的缓冲区 count是缓冲区中的有效字节数。 /** * The
buffer where data is stored. */ protected byte buf[]; /** * The number of valid
byte...
分类:
编程语言 时间:
2014-06-11 11:03:44
阅读次数:
269
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update
max_len,由于要保存之前的‘(’的index,所以spacecomplexity 是O(n) 1 // 使用栈,时间复杂度 O(n),空间复杂度 O(n)
2 class Solution { 3 publ...
分类:
其他好文 时间:
2014-06-11 09:16:10
阅读次数:
197
基本数据类型都有自己的包装对象:如 String 、Number、Boolean包装对象如何来: 因为
str有它的包装对象Sting对象,过程是基本类型会找到对应的包装对象类型,然后把包装对象的属性和方法继续过来,最后包装对象消失,所以str.charAt(0);可以正常使用.原型链:实例对象与原...
分类:
其他好文 时间:
2014-06-11 09:01:43
阅读次数:
237
原题地址:https://oj.leetcode.com/problems/edit-distance/题意:Given
two wordsword1andword2, find the minimum number of steps required to
convertword1toword2....
分类:
编程语言 时间:
2014-06-11 08:58:49
阅读次数:
293
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
A partially fille...
分类:
其他好文 时间:
2014-06-07 12:51:12
阅读次数:
225