Leetcode: singleNumber只出现一次的数字

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:输入: [2,2,1]
输出: 1

思路:进行异或^运算

a ^ a = 0           a ^ 0 = a  并且满足交换律 所以 所有数异或 最后就等于只出现一次的那个数

代码:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        for(int i = 1; i < nums.size(); i++)
        {
            nums[0] ^= nums[i];
        }
        return nums[0];
    }
};

  • 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、付费专栏及课程。

余额充值