[Leetcode学习-C语言]Two Sum

news/2025/2/12 2:20:41/

题目连接:LeetCode - The World's Leading Online Programming Learning Platform

leetcode写C真的很多坑......

用 hash 拉链法 构造hash函数,才能写这么题小题。

typedef struct Node {  // 得自己做个hash 拉链法struct Node *next;int val;int sign;}Node, * pNode;void insert(int val, int sign, int hash, pNode nodeList[]) {pNode newNode = (pNode) malloc(1 * sizeof(Node));newNode->val = val;newNode->sign = sign;newNode->next=NULL; // leetCode的检测问题需要置空//printf("  a");if(nodeList[hash]) {pNode node = nodeList[hash];while(node->next) {node = node->next;}node->next = newNode;} else {nodeList[hash] = newNode;}}
int* twoSum(int* nums, int numsSize, int target, int* returnSize){*returnSize = 2; // leetCode需要指定返回多少数字int *result = (int*)malloc(sizeof(int)*2); // leetCode避免内存被清空int point1, point2, find = 0;pNode nodeList[10000] = {0}; // 这里能写成动态分配内存就好了// for(int i = 0; i < numsSize; i ++) printf("%d ", nums[i]);for(int i = 0; i < numsSize; i ++) {int val = target - nums[i];int hash = abs(val % numsSize);  // 做取余//printf("%d  ", hash);if(nodeList[hash]) {pNode nodeI = nodeList[hash];//printf("  %d|%d  ", nodeI->val, val);while(nodeI) {if((nodeI->val) == val) {point1 = nodeI->sign;point2 = i;find = 1;break;}nodeI = nodeI->next;}}if(find) break;insert(nums[i], i, abs(nums[i] % numsSize), nodeList);}result[0] = point1;result[1] = point2;//printf("\n%d %d", point1, point2);return result;
}


http://www.ppmy.cn/news/1157891.html

相关文章

信息解码(Message Decoding, ACM/ICPC World Finals 1991, UVa 213)rust解法

考虑下面的01串序列&#xff1a; 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1101, 1110, 00000, … 首先是长度为1的串&#xff0c;然后是长度为2的串&#xff0c;依此类推。如果看成二进制&#xff0c;相同长度的后一个串等于前一个串加1。注意上述…

解决Armbian插有线才能连接无线网

问题 Armbian插一下有线才能自动连接无线网。 RK3228电视盒子&#xff0c;刷了armbian&#xff0c;机器有无线网卡和有线网卡。 在插上有线的情况下开机&#xff0c;无线也可以访问。拔掉有线&#xff0c;无线正常。 在没插有线的情况下&#xff0c;无线不能自动连接&#xff…

高效工具类软件使用

高效工具类软件使用 目录概述需求&#xff1a; 设计思路实现思路分析1.Leanote2.Obsidian 的使用 参考资料和推荐阅读 Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a better result,wait for…

ArcGIS笔记8_测量得到的距离单位不是米?一经度一纬度换算为多少米?

本文目录 前言Step 1 遇到测量结果以度为单位的情况Step 2 简单的笨办法转换为以米为单位Step 3 拓展&#xff1a;一经度一纬度换算为多少米 前言 有时我们会遇到这种情况&#xff0c;想在ArcGIS中使用测量工具测量一下某一段距离&#xff0c;但显示的测量结果却是某某度&…

ffmpeg的重采样计算

最近在看ffmpeg的重采样计算逻辑&#xff0c;有一句话没大看懂 dst_nb_samples av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) src_nb_samples, dst_rate, src_rate, AV_ROUND_UP); &#xff0c;各种请教之后&#xff0c;记录如下。 重采样后的总样本数 为什么要涵盖重采…

探索数字时代的核心:服务器如何塑造未来并助你成就大业

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

第二届中国未来交通产业发展峰会在深举办 聚焦智能网联、低空经济

智慧交通、低空飞行、自动驾驶……交通产业“未来已来”。10月12日&#xff0c;2023第二届中国未来交通产业发展峰会在深圳成功举办。本次峰会是国内聚焦高级别智能网联、低空产业、智慧物流、新能源和交通装备发展的高水平行业盛会&#xff0c;为行业搭建上下游沟通交流的广阔…

二叉树的三种遍历方式的本质

二叉树的定义就不在这里多说了&#xff0c;下面这个图就是一个简单的二叉树&#xff1a; 二叉树的三种遍历方式&#xff1a; 前序遍历&#xff1a;头左右&#xff0c;也就是先头后左再右&#xff1a;1245367 public static void prePrint(BinaryTreeNode root) {if (root ! n…