leetcode力扣(1.两数之和,2.两数相加,3.无重复字符的最长字串)

1.两数之和
题目描述:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

链接:https://leetcode-cn.com/problems/two-sum

哈希表法
将 nums 中的元素值当下标,nums的下标当值存储在 hash 数组中 : hash[ nums[i] ] = i;
1 . 首先初始化 hash[2000], 初始值设为 -1 ,
2.遍历数组, 查看 target - nums[i] 为下标 的 hash 数组元素值 (hash[ target - nums[i] ]) 是否为 - 1;
3.若为 -1 ,将 下标 i 存放在 hash数组的 nums[i] 位置上, hash[ nums[i] ] = i;
4.若不为 -1 ,即存在 相加为 target 的元素,两个元素的下标为 hash[ target - nums[i]] , i;

注意: 测试用例中存在负数,在散列时会访问越界,故使用求余法,将负数散列到数组尾部 : hash[(nums[i] + MAX_SIZE) % MAX_SIZE] = i; 查找时也要如此;
就是负数放到后面

在这里插入图片描述

 #define maxsize 2048
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int hash[maxsize];
memset(hash,-1,sizeof(hash));
int *result=(int *)malloc(sizeof(int)*2);
for(int i=0;i<numsSize;i++)
{
    if(hash[(target-nums[i]+maxsize)%maxsize]!=-1)
    {
        result[1]=i;
        result[0]=hash[(target-nums[i]+maxsize)%maxsize];//处理负数
        *returnSize=2;
        return result;
    }
    hash[(nums[i]+maxsize)%maxsize]=i;
}
free(hash);
*returnSize=0;
return result;
}

2.两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头

链接:https://leetcode-cn.com/problems/add-two-numbers

注意进位

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int x,y,num,flag=0;
    struct ListNode *p1=l1,*p2=l2;
    struct ListNode *cur=(struct ListNode*)malloc(sizeof(struct ListNode));
    cur->next=NULL;
    struct ListNode *res=cur;
    while(p1!=NULL||p2!=NULL)
    {
        x=(p1==NULL)?0:p1->val;
        y=(p2==NULL)?0:p2->val;
        num=x+y+flag;
        flag=num/10;
        cur->next=(struct ListNode*)malloc(sizeof(struct ListNode));
        cur=cur->next;
        cur->val=num%10;
        cur->next=NULL;
        if(p1!=NULL){p1=p1->next;}
        if(p2!=NULL){p2=p2->next;}
    }
    if(flag>0)
    {
        cur->next=(struct ListNode*)malloc(sizeof(struct ListNode));
        cur=cur->next;
        cur->next=NULL;
        cur->val=1;
    }
    return res->next;
}

3无重复字符的最长字串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度

逐段查找

class Solution
{
public:
    int lengthOfLongestSubstring(string s)
    {
        //s[start,end) 前面包含 后面不包含
        int start(0), end(0), length(0), result(0);
        int sSize = int(s.size());
        while (end < sSize)
        {
            char tmpChar = s[end];
            for (int index = start; index < end; index++)
            {
                if (tmpChar == s[index])
                {
                    start = index + 1;//将开头移动到上一次出现之后的位置。
                    length = end - start;
                    break;
                }
            }

            end++;
            length++;
            result = max(result, length);
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值