码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [Leetcode学习-C语言]Two Sum


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

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

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

    1. typedef struct Node { // 得自己做个hash 拉链法
    2. struct Node *next;
    3. int val;
    4. int sign;
    5. }Node, * pNode;
    6. void insert(int val, int sign, int hash, pNode nodeList[]) {
    7. pNode newNode = (pNode) malloc(1 * sizeof(Node));
    8. newNode->val = val;
    9. newNode->sign = sign;
    10. newNode->next=NULL; // leetCode的检测问题需要置空
    11. //printf(" a");
    12. if(nodeList[hash]) {
    13. pNode node = nodeList[hash];
    14. while(node->next) {
    15. node = node->next;
    16. }
    17. node->next = newNode;
    18. } else {
    19. nodeList[hash] = newNode;
    20. }
    21. }
    22. int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    23. *returnSize = 2; // leetCode需要指定返回多少数字
    24. int *result = (int*)malloc(sizeof(int)*2); // leetCode避免内存被清空
    25. int point1, point2, find = 0;
    26. pNode nodeList[10000] = {0}; // 这里能写成动态分配内存就好了
    27. // for(int i = 0; i < numsSize; i ++) printf("%d ", nums[i]);
    28. for(int i = 0; i < numsSize; i ++) {
    29. int val = target - nums[i];
    30. int hash = abs(val % numsSize); // 做取余
    31. //printf("%d ", hash);
    32. if(nodeList[hash]) {
    33. pNode nodeI = nodeList[hash];
    34. //printf(" %d|%d ", nodeI->val, val);
    35. while(nodeI) {
    36. if((nodeI->val) == val) {
    37. point1 = nodeI->sign;
    38. point2 = i;
    39. find = 1;
    40. break;
    41. }
    42. nodeI = nodeI->next;
    43. }
    44. }
    45. if(find) break;
    46. insert(nums[i], i, abs(nums[i] % numsSize), nodeList);
    47. }
    48. result[0] = point1;
    49. result[1] = point2;
    50. //printf("\n%d %d", point1, point2);
    51. return result;
    52. }

  • 相关阅读:
    电机应用开发-PID控制器参数整定
    java计算机毕业设计客服管理系统源码+mysql数据库+系统+lw文档+部署
    01简单的CMakeLists.txt示例
    Next.js 13 服务器组件和应用目录完整指南
    使用 RNN 模型从零实现 情感分类(详解)
    KBU1510-ASEMI整流桥KBU1510
    解决Webpack报错Unable to load ‘@webpack-cli/serve‘ command
    springboot使用logback日志框架超详细教程
    python怎么实现Excel表的折叠功能
    三年!
  • 原文地址:https://blog.csdn.net/qq_28033719/article/details/133844274
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号