leetcode:Two Sum

leetcode解题笔记:Two sum系列解题思路 Two Sum 3Sum 3Sum Closest Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, 阅读详情

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

看到题目,我想,这还不简单吗,两个for循环就搞定了呀!!!

刷刷写上代码,改正小错误后,leetcode告诉我:time Limit Exceeded。

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    int i, j;
    int *r = malloc(sizeof(int) * 2);

    for (i = 0; i < numsSize; i++) {
        int value = target - nums[i];
        for (j= i + 1; j < numsSize; j++) {
            if (nums[j] == value) {
                r[0] = i + 1;
                r[1] = j + 1;
                return r;
            }
        }
    }
    free(r);
    return NULL;
}


我不被接受啊,居然还需要优化!!!

我想想,貌似只能优化第二个循环了的查找了,写一个hash吧。

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */



struct solt {
    int value;
    int pos;
    struct solt *next;
};

struct solt ** create(int *nums, int numsSize) {
    int i = 0;
    struct solt ** head = calloc(numsSize, sizeof(struct solt *));
    for (i = 0; i < numsSize; i++) {
        int key = nums[i] % numsSize;
        key = key > 0 ? key : (-1 * key);
        struct solt *s = calloc(1, sizeof(struct solt));
        s->value = nums[i];
        s->pos = i;
        if (head[key] != NULL) {
            s->next = head[key];
        }
        head[key] = s;
    }
    return head;
}

int soltFind(struct solt ** head, int numsSize, int value) {
    int key = value % numsSize;
    key = key > 0 ? key : (-1 * key);
    struct solt * s = head[key];
    while (s != NULL) {
        if (s->value == value) {
            return s->pos;
        }
        s = s->next;
    }
    return -1;
}

void soltFree(struct solt **head, int numsSize) {
    int i = 0;
    for (i = 0; i < numsSize; i++) {
        struct solt *s = head[i];
        while (s) {
            struct solt * st = s->next;
            free(s);
            s = st;
        }
    }
    free(head);
}

int* twoSum(int* nums, int numsSize, int target) {
    int i, j;
    int *r = malloc(sizeof(int) * 2);
    struct solt ** head = create(nums, numsSize);
    for (i = 0; i < numsSize; i++) {
        int value = target - nums[i];
        int j = soltFind(head, numsSize, value);
        if (j >=0 && j != i) {
            soltFree(head, numsSize);
            r[0] = i + 1;
            r[1] = j + 1;
            return r;
        }
    }
    soltFree(head, numsSize);
    free(r);
    return NULL;
}



Leetcode: 1.Two Sum 两数之和 Two Sum 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 输入: nums = [2, 7, 11, 15], target = 9 输出: [0, 1] 因为 nums[0] + nums[1] = 2 + 7 = 9 ... 阅读详情

相关推荐

LeetCode::Two Sum C语言

题目 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target,

世界是兔子的 1万+

由free()函数引发的关于动态内存分配的思考

由free()函数引发的关于动态内存分配的思考 前几天力扣刷题时无意中看到的这道(伪)动态内存分配题激起了我对内存的好奇心,我之前做链表和二叉树的时候遇到过malloc()函数,但是当时只知道他是用来分配空间的,具体怎么分配还是一头雾水,出现的free()更是让我一时懵逼,不知道注释在说什么,于是有了下面这篇了解动态内存分配博客的诞生: 如图红框中的注释所示, Note: The returned array must be malloced, assume caller calls free(). 注意

橙橙橙汁i的博客 2361

2025反爬虫革命:京东商品接口的动态指纹认证与量子加密传输方案

在2025年,全球电商API接口面临的安全威胁呈现指数级增长:京东构建了基于硬件、软件、行为三重维度的设备指纹模型:京东构建了基于BB84+E91混合协议的量子密钥中继网络:

FB13713612741的博客 1648

leetcode 22 括号的生成 回溯法

/** * Note: The returned array must be malloced, assume caller calls free(). */ int pos; void getAns(char ** result, int left, int right, int *retNum, int n, char * map) { //printf("line:%d %...

chen_ethan的博客 486

联邦学习模型投毒研究进展

在联邦学习中,多个设备(如移动设备、IoT设备等)本地训练模型,然后将更新的模型参数上传到服务器进行聚合。

2401_82375734的博客 1265

C#实战:基于ONNX与SCRFD/ArcFace的高效人脸识别系统开发

本文详细介绍了如何在C#环境中,利用ONNX Runtime高效部署SCRFD人脸检测与ArcFace人脸识别模型,构建一个完整的、高性能的人脸识别系统。内容涵盖环境搭建、模型推理、人脸对齐、特征提取等核心模块,并提供了从Demo到生产系统的优化思路与实战代码,为.NET开发者提供了一套开箱即用的解决方案。

weixin_29061821的博客 488

leetcode 1299. 将每个元素替换为右侧最大元素 C语言 动态规划

题目 读题 求第i个数右侧的最大值,就需要求出在(i,n-1]之间的最大值,并把这个最大值赋给arr[i], 求解问题便变成了求左开右闭后缀最大值的问题 运用动态规划 设计状态:求第i项之后的最大值(包含第1项)就转移到求[第i+1项之后的最大值(包含第i+1项)和第i项之间]的最大值 写出状态方程:Rmix[i]=max(Rmix[i+1],a[i]); 设定初始状态:Rmix[n-1]=a[n-1]; rmix[n-1]=-1; 执行状态转移 返回最终解 ..

Crush_66的博客 667

Linux系统下奥比中光Astra Pro相机SDK部署与避坑指南

本文详细指导了在Linux系统下部署奥比中光Astra Pro深度相机SDK的全过程,涵盖系统准备、SDK安装、设备权限配置及常见问题排查。重点解析了USB设备规则配置这一关键步骤,并提供了Python与C++项目的集成方法,旨在帮助开发者快速完成相机配置,避免常见陷阱。

weixin_28455801的博客 298

leetcode 算法 第1题

** 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。 ** 首先第一眼,没有思路 然后看了解答 /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum/*两数之和*/(int* nums/*整数数组*/, int numsSize/*和值*/, int target/*整数目标值*/ , int* returnSize/*目标值数组下标*/){ /

m0_46663240的博客 1195

UniTouch:多模态触觉对齐技术如何重塑零样本跨模态应用

UniTouch模型通过创新的多模态触觉对齐技术,成功将触觉数据映射到通用的视觉-语言嵌入空间,解决了触觉模态的“孤岛困境”。该技术利用对比学习和传感器特定标记,实现了高质量的零样本学习能力,使模型无需额外训练即可完成触觉分类、描述等任务,并能驱动跨模态生成,为机器人、VR和智能制造等领域提供了强大的触觉理解新范式。

weixin_29044713的博客 409

智算中心建设全流程指南:从规划到落地

智算中心的建设是一个复杂而系统的过程,涉及选址、立项、办理相关手续、设计、土建、机电安装、调试、验收等多个环节。每个环节都需要充分考虑各种因素,并注意相关的事项,以确保智算中心的顺利建设和高效运行

数据中心全生命周期价值管理 3395

LeetCode刷题DAY1

力扣(LeetCode)刷题:1480. 一维数组的动态和

cheung的博客 5363

1、使用Spark SQL查询Databricks:全面指南

本博客全面介绍了如何使用Spark SQL在Databricks平台上进行高效的数据查询与分析。内容涵盖SQL基础操作、数据过滤、复杂查询技巧、简单与高级计算、数据聚合、日期处理等核心主题,适合数据分析师、开发人员及SQL学习者。通过逐步讲解和实用示例,帮助读者掌握从入门到高级的SQL技能,并提供学习路径建议和免费代码资源,助力实际数据分析工作。

redis7keeper的博客 90

《LeetCode》:Two Sum

《LeetCode》:Two Sum昨天刚说的准备去刷《程序员面试金典》上面的题,但是看了下那本书上面的题,感觉都没有什么意思,因此也就打算来刷了leetCode上面的题了。刷题的顺序选择了按照顺序来刷。今天开始了第一题:Two Sum 题目如下: 意思就是:在一个整型数组中,寻找两个数等于一个定值,返回这个数在数组中的下标,要注意的是:下标不是从0开始的。《剑指Offer》上也有类似

wojiushimogui的博客 811

6、深入解析Microsoft 365核心服务对比与迁移策略

本文深入解析了Microsoft 365的核心服务,包括身份和设备管理、现代设备配置体验、Exchange和SharePoint的迁移策略以及Microsoft Teams的协作功能。同时探讨了迁移过程中需要考虑的安全控制措施,为组织规划和实施云迁移项目提供了全面的指导和建议。

k8s6orchestrator的博客 105

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

从 Two Sum 开始: LeetCode100

Two Sum 其实是很多算法题的原型。很多后续题目都是它的变形,比如:Two Sum IIThree SumFour Sum所以理解这题,其实是在建立Hash 思维的基础。重新刷 Two Sum 的过程让我意识到:算法不是记住解法,而是建立一种思考模式。当看到一个数时,要习惯性问自己:我真正需要找的,是不是?当这种模式形成之后,很多题目就会变得自然很多。有时候回头看最基础的题,反而会有新的理解。这也是刷算法最有意思的地方。

qq_39899644的博客 534

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【Java版】LeetCode 力扣第 1 题:两数之和 (two sum)

题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 题目解析 使用查找表来解决该问题。 设置一个 map 容器 record 用来记录元素的值与索引,然后遍历数组 nums

代码时光 384

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode: 哈希——two-sum,3sum,4sum

1). two-sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add ...

156

Leetcode:Two Sum

题目: 最先思路: public class Solution { public int[] TwoSum(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) { for (int j = i + 1; j < nums

Chen_17 285

LeetCode:Two Sum

LeetCode解题记录

vivianXuejun的博客 372

LeetCode: Problem 1, Two Sum

LeetCode: 2 sum 题目链接:https://leetcode.com/problems/two-sum/ 作为一名学院小学渣,被dalao推荐来写leetcode,第一题就耗费了我好久时间,想尽量努力长期刷下去,就有了长期写博客的想法,希望自己能在这里坚持下去吧。 ...

swordpj的博客 198

LeetCode 1: Two Sum

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Giv

生而彷徨 1619

Leetcode: 167. Two Sum II - Input array is sorted

Leetcode: 167. Two Sum II - Input array is sorted 超时的代码: public class Sum22 { public int[] twoSum(int[] numbers, int target) { int result[] = new int[2]; ...

哎呀妈呀脑壳疼脑壳疼 97

【LeetCode】解题1:Two Sum

LeetCode解题 1:Two Sum Problem 1: Two Sum [Easy]Solution (Java)修改过程 Problem 1: Two Sum [Easy] Given an array of integers, return indices of the two numbers such that they add up to a specific target. Yo...

Fayedily的博客 249

LeetCode题解:Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the ta

Magi Su的技术回收站 8441
上一篇: 猴子摘香蕉一次可以摘1个或2个,总共50个,问有多少种摘法?
下一篇: node.js内存泄露问题记录
smallmelon
博客等级 码龄16年 22粉丝 50原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值