HashTable哈希表与统计594、350、|554、609、454、18

350. 两个数组的交集 II

给定两个数组,编写一个函数来计算它们的交集。

示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]
说明:
输出结果中每个元素出现的次数,应与元素在两个数组中出现次数的最小值一致。
我们可以不考虑输出结果的顺序。
题解:
1)两个set去重后遍历其中一个。因为set.toArray有点问题没有实现
2)hashmap,实质同1
3)排序后双指针,分别对两个数组进行排序,两个指针从头遍历两个数组,相等的记录

sloution:

HashMap<Integer,Integer> map = new HashMap<>();
        //1.统计nums1中各个元素的出现次数
        for (int i = 0; i < nums1.length; i++) {
            //当Map集合中有这个key时,就使用这个key值,如果没有就使用默认值defaultValue
            int count = map.getOrDefault(nums1[i],0)+1;
            map.put(nums1[i],count);
        }
        //2. 遍历nums2查找存在map中的元素同时更新map的key的次数
        int[] result = new int[nums1.length];//既然是两数组的交集,那么长度一定小于等于任意一个数组
        int index = 0;
        for (int target : nums2) {
            int count = map.getOrDefault(target,0);
            //证明元素target存在于map,就说明nums2中的target是和nums1有交集的元素
            if (count > 0){
                result[index++] = target;//添加到结果集中
                count --;
                map.put(target,count);//更新map
            }
        }
       //将指定的数组指定的范围复制到一个新的数组中
        return Arrays.copyOfRange(result,0,index);
class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        // 对两个数组进行排序。
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int len1 = nums1.length, len2 = nums2.length;
        int[] ans = new int[Math.min(len1, len2)];
        int index1 = 0, index2 = 0, index = 0;
        // 只要有一个数组遍历完就跳出循环。
        while (index1 < len1 && index2 < len2) {
            // 遍历两个数组,谁小就移动谁的指针,相等就记录并同时移动指针。
            if (nums1[index1] < nums2[index2]) {
                index1++;
            } else if (nums1[index1] > nums2[index2]) {
                index2++;
            } else {
                ans[index] = nums1[index1];
                index1++;
                index2++;
                index++;
            }
        }
        // 遍历完成返回重复元素长度的结果数组。
        return Arrays.copyOfRange(ans, 0, index);
    }
}

594. 最长和谐子序列

和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度。

示例 1:
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].

题解:
1)hash映射
2)排序后双指针 类似滑动窗口
sloution:

class Solution {
    public int findLHS(int[] nums) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int LL = 0;
        for(int i : nums)
        {
            if(map.containsKey(i))  
                map.put(i, map.get(i)+1);
            else                    
                map.put(i, 1);
            if(map.containsKey(i - 1))
                LL = Math.max(LL, map.get(i) + map.get(i - 1));
            if(map.containsKey(i + 1))
                LL = Math.max(LL, map.get(i) + map.get(i + 1));
        }
        return LL;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,哈希表Hashtable)是一种常见的数据结构,用于快速存储和检索数据。它基于哈希函数将键(Key)映射到存储桶(Bucket)中,每个桶中存储一个键值对。下面是一个简单的哈希表的定义和使用场景: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 100 typedef struct { char* key; int value; } KeyValue; typedef struct { KeyValue* data[TABLE_SIZE]; } Hashtable; Hashtable* createHashtable() { Hashtable* hashtable = (Hashtable*)malloc(sizeof(Hashtable)); memset(hashtable->data, 0, sizeof(hashtable->data)); return hashtable; } unsigned int hash(const char* key) { unsigned int hashValue = 0; while (*key) { hashValue = (hashValue << 5) + *key++; } return hashValue % TABLE_SIZE; } void insert(Hashtable* hashtable, const char* key, int value) { unsigned int index = hash(key); KeyValue* keyValue = (KeyValue*)malloc(sizeof(KeyValue)); keyValue->key = strdup(key); keyValue->value = value; hashtable->data[index] = keyValue; } int find(Hashtable* hashtable, const char* key) { unsigned int index = hash(key); KeyValue* keyValue = hashtable->data[index]; if (keyValue && strcmp(keyValue->key, key) == 0) { return keyValue->value; } return -1; } void destroyHashtable(Hashtable* hashtable) { for (int i = 0; i < TABLE_SIZE; i++) { KeyValue* keyValue = hashtable->data[i]; if (keyValue) { free(keyValue->key); free(keyValue); } } free(hashtable); } int main() { Hashtable* hashtable = createHashtable(); // 插入键值对 insert(hashtable, "apple", 5); insert(hashtable, "banana", 3); insert(hashtable, "orange", 7); // 查找值 printf("The value of apple is: %d\n", find(hashtable, "apple")); printf("The value of banana is: %d\n", find(hashtable, "banana")); printf("The value of orange is: %d\n", find(hashtable, "orange")); destroyHashtable(hashtable); return 0; } ``` 这个示例代码实现了一个简单的哈希表,可以插入键值对和查找对应的值。通过`createHashtable`函数可以创建一个空的哈希表,通过`insert`函数可以插入一个键值对,通过`find`函数可以查找对应键的值,通过`destroyHashtable`函数可以销毁哈希表哈希表的使用场景包括: 1. 缓存:哈希表可以用于快速存储和检索缓存数据,提高访问速度。 2. 字典:哈希表可以用于实现字典,将键映射到对应的值。 3. 数据索引:哈希表可以用于构建数据索引,根据键快速查找对应的数据。 4. 频率统计哈希表可以用于统计数据的频率,记录某个元素出现的次数。 希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值