寻找只出现一次的数字(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?

        一个数组中有一个元素只出现一次,其他元素均出现3次,找出这个数,并且不能使用额外的内存空间。

     

1.首先可以想到暴力破解,时间复杂度为O(n2),复杂度太高。

        2.题目提示利用比特级操作(Bit Manipulation)。从比特级别上看这个问题,对于int型数中的任一比特位x,可以用变量frequency来统计这一比特位上1出现的次数,到达3则清零。那么最终那个奇异数(只出现一次的数)上的这一比特位必定累加到了frequency上,就是frequency的值。

        实现:frequency最大值为2,所以frequency至少为两比特。所以总共需要sizeof(int)*2比特位来统计所有的比特位上1出现的次数。那么就可以用2个int a,b来表示(a对应2bit中的高位比特,b对应低位比特)。

        可以针对每一特定比特位,可以画出这样一个真值表(我一定是在做数字电路-_-)


其实是一个(0,0)->(0,1)->(1,0)->(0,0)的状态机循环。

从上表可以推导出:b = ~a&(b^x); a = a^x&(~b);

class Solution {
public:
	int singleNumber(int A[], int n) {
		if(A==NULL || n<=0)
			return -1;
		int a=0,b=A[0];
		for(int i=1;i<n;++i){
			b = ~a&(b^A[i]);
			a = a^A[i]&~b;
		}
		return b;
	}
};


最终返回b,就是所求的数,b上的每一比特都与所求的数相同。

        这一方法的时间复杂度为O(n),空间复杂度为O(1);但只是用于其他的数都是出现三次这种特定情况,如果是其他一般情况,就要使用int[32]数组来统计每一比特位上频率次数了,所需的内存空间会更多一些。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个用C语言实现的LeetCode题目:Group Anagrams。 题目描述: 给定一个字符串数组,将其分组,使得包含相同字符的字符串在同一组中。返回结果应按照字母顺序排列。 示例: 输入: ["eat", "tea", "tan", "ate", "nat", "bat"] 输出: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 解题思路: - 遍历字符串数组,将每个字符串转换为字符数组并排序。 - 使用哈希表将排序后相同的字符串分组。 - 遍历哈希表,将每个分组的字符串数组添加到结果数组中。 代码实现: /** * Note: The returned array must be malloced, assume caller calls free(). */ char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){ char ***res = (char ***)malloc(sizeof(char **) * strsSize); *returnSize = 0; *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize); int cnt[26] = {0}; int index = 0; struct Node *hashTable[strsSize]; memset(hashTable, 0, sizeof(hashTable)); for (int i = 0; i < strsSize; i++) { memset(cnt, 0, sizeof(cnt)); for (int j = 0; j < strlen(strs[i]); j++) { cnt[strs[i][j] - 'a']++; } char *key = (char *)malloc(sizeof(char) * 27); int idx = 0; for (int j = 0; j < 26; j++) { if (cnt[j] != 0) { key[idx++] = j + 'a'; key[idx++] = cnt[j] + '0'; } } key[idx] = '\0'; int hash = getHash(key); if (hashTable[hash] == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = NULL; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } else { struct Node *p = hashTable[hash]; while (p != NULL) { if (strcmp(p->key, key) == 0) { p->count++; p->strings = (char **)realloc(p->strings, sizeof(char *) * p->count); p->strings[p->count - 1] = strs[i]; break; } p = p->next; } if (p == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = hashTable[hash]; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } } } for (int i = 0; i < strsSize; i++) { if (hashTable[i] != NULL) { struct Node *p = hashTable[i]; while (p != NULL) { res[index] = p->strings; (*returnColumnSizes)[index] = p->count; (*returnSize)++; p = p->next; index++; } } } return res; } int getHash(char *key) { int hash = 0; for (int i = 0; i < strlen(key); i++) { hash = (hash * 31 + key[i]) % 100000; } return hash; } struct Node { char *key; struct Node *next; char **strings; int count; };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值