开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了。

想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力。python则是最流行的语言。

做题用的是 xcode的 leecode插件 非常的方便。顺序从简单到难。开始。

[1] 两数之和
*
* https://leetcode-cn.com/problems/two-sum/description/
*
* algorithms
* Easy (44.20%)
* Total Accepted:    232.9K
* Total Submissions: 526.9K
* Testcase Example:  '[2,7,11,15]\n9'
*
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
*
* 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
*
* 示例:
*
* 给定 nums = [2, 7, 11, 15], target = 9
*
* 因为 nums[0] + nums[1] = 2 + 7 = 9
* 所以返回 [0, 1]
 
最开始的想法就是先弄个最简单的思路。
在数组中,通过两个参数i和j,进行循环,然后判断相加等不等于 目标值target。
int* twoSum(int* nums, int numsSize, int target) {
int i,j,count=;//count为统计的次数
int *a = (int*)malloc(*sizeof(int));//为a分配内存
//循环遍历数组
for(i=;i<numsSize;i++){
for(j=i+;j<numsSize;j++){
if(nums[i]+nums[j]==target){
a[]=i;
a[]=j;
count=;//统计次数设为1
}
}
if(count==) break; //若找到,则退出。
}
return a;
}

这里有两个点要注意:

1.要对返回的数组进行动态的分配内存,不然会报错。

2.设置一个count,记录是否找到成功的值,找到一次后因为题目要求不能重复利用,其实就可以退出了。

这道题用c语言做可以通过,但是时间复杂度达到了O(n²) 有另一种办法就是用哈希表,在别的一些高级脚本语言中可以用字典的方式,只需要一次遍历就可以。

但是c语言中需要自己写哈希表的东西。这里顺便复习下数据结构的哈希表

参考:https://blog.csdn.net/hang404/article/details/84764531

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
struct node {
long val;
int index;
struct node* next;
};
void insert(struct node** hashTable, long val, int index, int n) {
int t = abs(val) % n;
struct node* temp = hashTable[t];
// head-add
struct node* add = (struct node*)malloc(sizeof(struct node));
add->val = val;
add->index = index;
add->next = temp->next;
temp->next = add;
}
int search(struct node** hashTable, long target, int n) {
int index = abs(target) % n;
struct node* temp = hashTable[index]->next;
while(temp) {
if(temp->val == target) {
return temp->index;
}
temp = temp->next;
}
return -;
}
void freeHashTable(struct node** hashTable, int n) {
int i = ;
struct node *temp = NULL, *delete = NULL;
for(i = ; i < n; i++) {
temp = hashTable[i];
delete = temp;
while(temp) {
delete = temp;
temp = temp->next;
free(delete);
}
}
free(hashTable);
}
int* twoSum(int* nums, int numsSize, int target) {
int i = , j = ;
int n = numsSize, index = ;
int* result = NULL;
struct node** hashTable = (struct node**)malloc(n * sizeof(struct node*));
// init head node
for(i = ; i < n; i++)
hashTable[i] = (struct node*)calloc(, sizeof(struct node));
for(i = ; i < n; i++) {
index = search(hashTable, target - nums[i], n);
if(- == index)
insert(hashTable, nums[i], i, n);
else {
result = (int*)malloc(sizeof(int) * );
result[] = index;
result[] = i;
freeHashTable(hashTable, n);
return result;
}
}
freeHashTable(hashTable, n);
return result;
}

关于哈希表的概念和c语言中哈希表的设计:

https://blog.csdn.net/qq_34888036/article/details/80880487

------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=1 lang=python3
#
# [1] 两数之和
#
# https://leetcode-cn.com/problems/two-sum/description/
#
# algorithms
# Easy (44.78%)
# Total Accepted: 257.1K
# Total Submissions: 574K
# Testcase Example: '[2,7,11,15]\n9'
#
# 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
#
# 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
#
# 示例:
#
# 给定 nums = [2, 7, 11, 15], target = 9
#
# 因为 nums[0] + nums[1] = 2 + 7 = 9
# 所以返回 [0, 1]
#
#
#
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums) #获取nums的长度
d = {} #创建空字典
for x in range(n):
a = target - nums[x] #构成target和需要的差值
if nums[x] in d:
return d[nums[x]],x #返回两个位置
else:
d[a]=x #将当前位置所需的差值和自身位置存入字典中

这段代码我还理解了很多遍。

比如从2开始,字典对2的存储是: 7:0

就是当前位置需要的差值,和自身的位置。

然后寻找到下一个 a此时等于2,如果字典中没有这个7的话,那字典中存储的就应该是 2 :1 也就是它需要2可以合成target,自身的位置是1。

如果有这个7的话,那么就返回 需要差值是7的那个数的位置,和当前位置也就是x。返回的就是 d[7]和1 也就是 0和1

大意就是 从这段数组中,边存便检查,字典中存放target与每个数的差值和自身的位置,在后面如果查找到当前的数和字典中的差值相等,就返回

字典中对应的位置和自身的位置。没查到的话,就把当前数所需差值和当前的位置存放到字典中。

Leecode刷题之旅-C语言/python-1.两数之和的更多相关文章

  1. Leecode刷题之旅-C语言/python-88合并两个有序数组

    /* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...

  2. Leecode刷题之旅-C语言/python-21.合并两个有序链表

    /* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...

  3. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  4. Leecode刷题之旅-C语言/python-118杨辉三角

    /* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...

  5. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  6. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  7. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  8. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  9. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

随机推荐

  1. SVNKit学习——使用High-Level API管理Working Copy示例(六)

    本篇内容是基于SVNKit High-Level API实现的针对Working copy的操作,操作内容与SVN图形化界面.命令行类似. High-Level API类图: 核心思想: 所有操作由各 ...

  2. BIEE入门(三)业务模型层

    正如它的名字所示(Business Model and Mapping Layer),业务逻辑层需要把物理层的数据源以一种业务用户的视角来重新组织物理层的各个数据源(所谓的Mapping),同时在业务 ...

  3. SQL Server ->> Move characters in string N position(s) forward/backward based on ASCII table(根据ASCII表的排列顺序将字符串内的数值往前或者后移N个位)

    去年无聊的时候想到想玩一下根据ASCII表的排列顺序将字符串内的数值往前或者后移N个位,顺便看一下是T-SQL性能好还是用C#写CLR函数处理得快.结果是在50万行以下其实两者差距很小,当然这是在我的 ...

  4. MySQL中AddDate函数的疑惑

    无论使用哪一种RDBMS,都需要使用到其中的一些日期转换函数,在使用MySQL的AddDate函数时,遇到了点小问题,稍作记录. root@localhost:mysql3376.sock [(non ...

  5. nginx中的location匹配规则

    概述: 1. location在nginx配置文件中的作用是根据用户请求的URI来执行不同的应用. 2.URI的定义:标识.定位任何资源的字符串 协议://域名/目录a/目录b/文件c http:// ...

  6. March 4 2017 Week 10 Saturday

    There is more to life than increasing its speed. 生活不仅仅是匆匆赶路. I always think I have walked very slowl ...

  7. 在linux代码中打印函数调用的堆栈的方法

    之前一直有这样的需求,当时问到,也没搜到方法,现在竟然既问到了,也搜到了,哎,世事真是不能强求啊! 在Linux内核调试中,经常用到的打印函数调用堆栈的方法非常简单,只需在需要查看堆栈的函数中加入: ...

  8. LeetCodeOJ刷题之15-16【3Sum(三数和问题)】

    本文为两个题:[三数和(3Sum)]与[最近三数和(3Sum Closest)]问题 第一部分分析3Sum问题,第二部分分析3Sum Closest问题,由于两个问题的思路很像,所以这里放到一起分析. ...

  9. bash: ./adb: No such file or directory

    运行adb出现这种错误: bash: ./adb: No such file or directory   但adb确实存在. 可能1.你用的是64位的Linux,没装32位运行时库,安装 $ sud ...

  10. c#类的练习

    类部分练习题 - dijiaxing1234的博客 - CSDN博客  https://blog.csdn.net/dijiaxing1234/article/details/81230811 真好啊