1.TwoSum
记录被LeetCode虐的日子
第一种方法:使用枚举
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
{
int *result = (int*)malloc(2 * sizeof (int)); //申请内存
int i = 0;
int j = 0;
if(sizeof(nums) < 1) //输入参数有误判断
{
return NULL;
}
for(i = 0;i <= numsSize-1; i++)
{
for(j = 0;j <= numsSize-1;j++)
{
if((nums[i] + nums[j] == target) && (j != i) )
{
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}
注意:
1 代码开始,先检查边界值。如果为边界值,直接返回相应结果;如果是指针则检查是否为NULL.是数字的情况,要考虑特殊的数值,如零
2 提示错误:
load of null pointer of type 'const int'
在调用函数返回时,返回值如果是一个常量,则没问题。
返回值若为指针,则需注意会出现这个错误。如果返回的指针地址指向函数内的局部变量,在函数退出时,该变量的存储空间会被销毁,此时去访问该地址就会出现这个错误。
解决办法有以下三种:
1)返回的指针使用malloc分配空间
2)将该变量使用static修饰 static修饰的内部变量作用域不变 但是声明周期延长到程序结束 即该变量在函数退出后仍然存在
3)使用全局变量
建议使用malloc分配空间返回
3 指针数组初始化方法
指针数组中的每个元素都是一个指针,如下的array数组中的每个元素都是一个字符串指针,指向一个一维字符串数组。
char **array;
array = (char **)malloc(sizeof(char *) * 100) ; //array包含100个指针元素 为这100个指针变量分配空间
for(int i = 0; i < 100; i++)
array[i] = (char *)malloc(sizeof(char) * 50); // 为array中的每个指针变量进行初始化 上面的表达式只是为指针变量分配了空间 并没有为它们赋值 此语句为每个指针分配了长度为50个字符的空间 并将该空间的初始地址赋值给array中的指针
以下为第二次做这道题
解法一
解法一就是把数组中所有的值算出来,找到等于
target的值,然后返回相应的位置即可。时间复杂度为O(N^2)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target)
{
int *result = (int*)malloc(2 * sizeof (int)); //申请内存
int i = 0;
int j = 0;
if(sizeof(nums) < 1) //输入参数有误判断
{
return NULL;
}
for(i = 0;i <= numsSize-1; i++)
{
for(j = 0;j <= numsSize-1;j++)
{
if((nums[i] + nums[j] == target) && (j != i) )
{
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}
解法二 使用Hash Map
Hash Map来建立数字和坐标之间的映射关系,
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> datamap; //哈希表 用来建立数字和坐标之间的映射关系
vector<int> res;
for (int i = 0; i < nums.size(); i++)
{
datamap[nums[i]] = i;
}
for (int i = 0; i < nums.size(); i++)
{
int t = target - nums[i]; //t 就是希望在哈希表中找到的元素
if (datamap.count(t) > 0)
{
//说明在哈希表中找到了t
res.push_back(i);
res.push_back(datamap[t]);
break;
}
}
return res;
}
};
上面代码存在一个BUG,当提交到leetcode时,testcase为{3,2,4},target = 6时可以测出这个BUG。修改后的代码为
class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> datamap; //哈希表 用来建立数字和坐标之间的映射关系
vector<int> res;
for (int i = 0; i < nums.size(); i++)
{
datamap[nums[i]] = i;
}
for (int i = 0; i < nums.size(); i++)
{
int t = target - nums[i]; //t 就是希望在哈希表中找到的元素
if (datamap.count(t) > 0 && datamap[t] != i)
{
//说明在哈希表中找到了t
res.push_back(i);
res.push_back(datamap[t]);
break;
}
}
return res;
}
};
1.TwoSum的更多相关文章
- JavaScript的two-sum问题解法
一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...
- twoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- LeetCode——TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- Leetcode 1——twosum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode — two-sum
package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...
随机推荐
- linux释放页面缓存drop_caches
关于drop_caches文件:系统默认为0 在Documentation/sysctl/vm.txt中有如下描述: drop_caches Writing to this will cause th ...
- 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)
针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1) 文档内容 适用于: Oracle Database – Enterprise Edi ...
- gcc对c++标准的支持
GCC 4.8.1完全支持c++11核心部分,对应的glibc为2.17 gcc 4.9支持c++11正则表达式,卧槽...4.8.5会报terminate called after throwing ...
- es数据迁移脚本(python)
#!/usr/bin/python # -*- coding: UTF-8 -*- # 文件名:indiceCreate.py import sys import base64 import time ...
- python简说(二)list
一.list # 1.list 列表 数组a = ['A', 'B', 'C', 'D']# 0 1 2# 2.空list# a = []# a = list()# 3.下标 角标 索引# print ...
- Linux和windows之间使用scp无密码传输文件,脚本自动化
1.环境 windows2008 R2 和rhel 6.5 2.需求 通过在windows上指定计划任务,自动完成从Linux系统上备份文件到windows 3.工具 win: SSH Secure ...
- iOS Xcode Error 集锦
一),'libxml/tree.h' file not found Solution: 1. 导入libxml2.dylib 包 2.设置Header Search Paths 为 /usr/inc ...
- topcoder srm 475 div1
problem1 link 暴力枚举$r$只兔子的初始位置,然后模拟即可. problem2 link 假设刚生下来的兔子是1岁,那么能够生小兔子的兔子的年龄是至少3岁. 那么所有的兔子按照年龄可以分 ...
- 关于二进制——lowbit运算
lowbit(n)意思即为找出n在二进制表示下最后一位1即其后面的0所组成的数值,别的东西算法书上有,这里提出一个重要的公式 lowbit(n)=n&(~n+1)=n&(-n),这个有 ...
- bzoj4008: [HNOI2015]亚瑟王 dp
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4008 思路 神仙啊 \(f[i][j]表示第i个点有j次机会(不管成功与否)\) \(f ...