LeetCode:26

26. 删除有序数组中的重复项

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

示例 1:

输入:nums = [0,0,1,1,1,2,2,3,3,4]
输出:5, nums = [0,1,2,3,4]
解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素

解题思路:双指针
慢指针指向数组下标为0的第一个元素,快指针指向慢指针后面的元素。比较慢指针与快指针元素值的大小

  • 如果两个元素不相同,则慢指针nums[slow]后移一位,该位置的值为快指针指向的值nums[fast],快指针通过循环相应的+1;
  • 如果比较两个元素的值相同,则慢指针不变,慢指针位置的值和快指针指向位置的值相同,快指针的值+1;
class Solution {
    public int removeDuplicates(int[] nums) {
        // 慢指针第一个元素
        int slow = 0;
        // 快指针
        for(int fast = 1; fast < nums.length; fast++){
        	// 判断快指针指向的值和慢指针指向的值是否相同
        	//虽然应该比较相邻两个元素的值,但是如果快慢指针元素相同的时候,快指针的前一个元素和慢指针指向的元素值相同
            if(nums[fast] != nums[slow]){
            	//不相等则慢指针+1,并且该位置的值为刚刚快指针元素位置的值
                nums[++slow] = nums[fast];
            }            
        }
        // slow从0开始,最后结束时指向的是最后慢指针的下标值,反回长度+1
        return slow+1;
    }
}
这是一个用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、付费专栏及课程。

余额充值