leetcode每日三题_day1

  1. 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

暴力一下

int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
    int *result = (int *)malloc(sizeof(int) * 2);
    for (int i = 0; i < numsSize - 1; i++)
    {
        for (int j = i + 1; j < numsSize; j++)
        {
            if (nums[i] + nums[j] == target)
            {
                result[0] = i;
                result[1] = j;
                *returnSize = 2;
                return result;
            }
        }
    }
    return result;
}

hash一下

typedef struct HashNode
{
	int key;
	int value;
	struct HashNode * next;
}HashNode;

typedef struct HashMap
{
	int size;
	HashNode * table;
}HashMap;

HashMap * CreateHashMap(int *nums,int numSize)
{
	int i;
	int index=0;
	HashMap *hashmap = (HashMap *)malloc(sizeof(HashMap));		
	hashmap->size = numSize;
	hashmap->table = (HashNode *)malloc(sizeof(HashNode)*hashmap->size);	


	for(i=0;i<hashmap->size;i++)
	{
		hashmap->table[i].value = INT_MIN;
		hashmap->table[i].next = NULL;
	}


	while(index<numSize)
	{
		int place = abs(nums[index])%(hashmap->size);
		if(hashmap->table[place].value == INT_MIN)
		{
			hashmap->table[place].value = nums[index];
			hashmap->table[place].key = index;
		}
		else
		{
			HashNode * node_1 = (HashNode *)malloc(sizeof(HashNode));
			HashNode * hashnode;
			node_1->value = nums[index];
			node_1->key = index;
			node_1->next = NULL;

			hashnode = &(hashmap->table[place]);
			while(hashnode->next != NULL)
			{
				hashnode = hashnode->next;
			}
			hashnode->next = node_1;
		}
		index++;
	}
	return hashmap;
}

int GetHashMap(HashMap * hashmap, int value)
{
	int place = abs(value)%(hashmap->size);
	HashNode * pointer = &(hashmap->table[place]);
	while(pointer != NULL)
	{
		if(pointer->value == value)
		{
			return pointer->key;
		}
		else
		{
			pointer = pointer->next;
		}
	}
	return -1;
}

void freeHashMap(HashMap * hashmap)
{
	int i=0;
	HashNode * Fpointer;
	while(i<hashmap->size)
	{
		Fpointer = hashmap->table[i].next;
		while(Fpointer != NULL)
		{
			hashmap->table[i].next = Fpointer->next;
			free(Fpointer);
			Fpointer = hashmap->table[i].next;
		}
		i++;
	}
	free(hashmap->table);
	free(hashmap);
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
	int i;
	int key=0;
	HashMap * hashmap = CreateHashMap(nums,numsSize);
	int * res = (int *)malloc(sizeof(int)*2);
	* returnSize = 0;
	for(i=0;i<numsSize;i++)
	{
		int value = target - nums[i]; 
		key = GetHashMap(hashmap,value);
		if(key !=-1 && key != i)
		{
			res[0] = i;
			res[1] = key;
            * returnSize=2;
			break;
		}
	}
	freeHashMap(hashmap);
	return res;
}

//测试代码


记录一下第一次使用hashmap

第一步建立哈希表的数据结构
hashnode 每一个节点的数据结构,包括key,value,next指针
hashmap整个结构属性,包括结构大小,多少个初始化node


第二步初始化hashmap
给HashNode,HashMap地址空间,HashMap大小为size
HashMap中所有节点value初始化
定义哈希函数,将数据处理
判断当前位置是否冲突,不冲突则放入;冲突时申请一个新的HashNode,以及一个操作HashNode,将数据信息赋予新的HashNode,通过操作HashNode将新的Node放到最后


第三步查找元素
通过和第二步一样的哈希函数对传入数据进行处理
申请一个操作HashNode来指导计算出来的地址
判断改地址里的值和传入值是否相同,相同则返回key,即原数组中索引;不相同判断是否还有next,有的话重复操作判断;没有的话返回该地址下没有


第四步释放HashMap空间
需要逐步释放每一个Node上的链表节点
从HashMap第一个Node开始,申请一个新的Node指向该Node,判断该Node下是否还有Node

  1. 两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
示例
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

这是我能想到的一个做法,很简单的一个思路,但时间方面有所欠缺

    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
        if(l1==NULL&&l2==NULL){
            return false;
        }
        else if(l1==NULL){
            return l2;
        }
        else if(l2==NULL){
            return  l1;
        }

        int addflag=0;
        struct ListNode* result = (struct ListNode*)malloc(sizeof(struct ListNode));
        result->next = NULL;
        struct ListNode* cur = result;

        while(l1!=NULL&&l2!=NULL){
            struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
            tmp->next = NULL;

            int sum=l1->val+l2->val+addflag;
            addflag = sum /10;
            sum = sum%10;
            cur->val = sum;
            cur->next = tmp;

            if(l1->next==NULL&&l2->next==NULL){
                if(addflag!=0){
                    cur = cur->next;
                    cur->next = tmp;
                    cur->val=addflag;
                    tmp->next=NULL;
                }
                else{
                    cur->next = NULL;
                }
                return result;
            }

            else if(l1->next==NULL){
                l1->val=0;
                l2=l2->next;
            }
            else if(l2->next==NULL){
                l2->val=0;
                l1=l1->next;
            }
            else{
                l1=l1->next;
                l2=l2->next;
            }
            cur = cur->next;
        }
        return;
    }

  1. 无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。

和2差不多原理,记录元素位置。

int lengthOfLongestSubstring(char * s){
    int index[128] = {0};
    int start=0,max=0,count=0;
    int i;
    for( i=0;s[i]!='\0';i++)
    {
        if(index[s[i]]>start)
        {
            count = i - start;
            printf("count = %d\n",count);
            start = index[s[i]];
            if(count>max)
            {
                max = count;
            }            

        }
        index[s[i]] = i+1;
    }
    count = i - start;
     if(count>max)
    {
        max = count;
    } 
    return max;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值