8.8 哈希表简单 1 Two Sum 141 Linked List Cycle

1 Two Sum

在这里插入图片描述

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        //给的target是目标sum 要返回vector<int> res(2,0);是在num中找加数
        //首先假设每个输入都是由唯一的结果,而且不适用相同的元素两次一共有n*(n-1)种情况
        //按照顺序返回ans
        vector<int> res(2,0);
        //暴力解题
        int n = nums.size();
        for(int i = 0 ; i < n ; i++){
            for(int j = i+1 ; j < n ; j++){
                if(nums[i] + nums[j] == target){
                    res[0] = i;
                    res[1] = j;
                    return res;
                }
            }
        }
        return res;

    }
};

下方是哈希表解题:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int n = nums.size();
        //使用target - nums[i]
        //哈希表,前者入哈希,后者查哈希
        unordered_map<int,int> hash;
        for(int i = 0 ; i < n ;i ++){
            if(hash.find(target - nums[i]) != hash.end()){
                return {hash[target - nums[i]] , i};
            }
            hash[nums[i]] = i;
        }
        return {};

    }
};

141 Linked List Cycle

在这里插入图片描述
在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        int pos = -1;
        //哈希表存储什么?
        unordered_map<ListNode*,int> hash;
        ListNode *p = head;
        //一定要全部遍历吗?
        int i = 0;
        if(p == nullptr || p->next == nullptr){
            return false;
        }
        //怎么就能判定 p指向了之前的结点
        while(p){
            if(hash.find(p) != hash.end()){
                pos =  hash[p];
                return true;
            }
            hash[p] = i;
            i++;
            p = p->next;
        }
        return false;
    }
};

要求空间复杂度为O(1)使用快慢指针。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用C语言哈希表法实现two sums的完整代码: ```c #include <stdio.h> #include <stdlib.h> #define HASH_SIZE 1009 typedef struct ListNode { int val; int index; struct ListNode *next; } ListNode; ListNode *hashTable[HASH_SIZE]; void insert(int key, int index) { int h = abs(key) % HASH_SIZE; ListNode *node = hashTable[h]; while (node != NULL) { if (node->val == key) { return; } node = node->next; } ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); newNode->val = key; newNode->index = index; newNode->next = hashTable[h]; hashTable[h] = newNode; } int *twoSum(int *nums, int numsSize, int target) { for (int i = 0; i < HASH_SIZE; i++) { hashTable[i] = NULL; } for (int i = 0; i < numsSize; i++) { insert(nums[i], i); } int *result = (int *)malloc(2 * sizeof(int)); for (int i = 0; i < numsSize; i++) { int complement = target - nums[i]; int h = abs(complement) % HASH_SIZE; ListNode *node = hashTable[h]; while (node != NULL) { if (node->val == complement && node->index != i) { result[0] = i; result[1] = node->index; return result; } node = node->next; } } return NULL; } int main() { int nums[] = {2, 7, 11, 15}; int target = 9; int *result = twoSum(nums, sizeof(nums) / sizeof(int), target); printf("[%d, %d]\n", result[0], result[1]); free(result); return 0; } ``` 代码中使用了链表来实现哈希表哈希表大小为1009(一个质数),插入和查找的时间复杂度均为$O(1)$。在函数`twoSum`中,首先将所有数插入哈希表,然后遍历数组,对于每个数,计算它的补数,查找补数是否存在于哈希表中。如果存在,返回它们的下标即可。在函数结束时,需要释放动态分配的结果数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值