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 ...
随机推荐
- Java Thread.yield详解
这是Java中的一种线程让步方法,让Java中的线程从执行状态变成就绪状态,然后处理器再从就绪队列中挑选线程进行执行(优先级大的,被挑选的概率较大),这种转换也不确定,让或者不让都是取决与处理器,线程 ...
- django模板-自定义标签、过滤器
自定义标签或者过滤器的步骤 ①将要创建自定义标签或过滤器的app加入settings文件的installed_apps中 ②在app中创建templatetags目录,类型为包即packages ③在 ...
- Python学习基础(三)——装饰器,列表生成器,斐波那契数列
装饰器——闭包 # 装饰器 闭包 ''' 如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包 闭包 = 函数块 + 定义时的函数环境 ''' def f(): x = 100 ...
- OO课程第四次总结
终于来到了最后一次的OO作业,以博客作业的形式来终结也是极好的,回顾一下过去十六周自己的经历,感慨颇深. 测试和正确性论证 简单来说,测试的目的是将程序的代码做到全覆盖,从而确保每个分支都运行一遍,进 ...
- bzoj 3875 骑士游戏 - spfa - 动态规划
Description [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,J ...
- Eclipse之maven插件link方式安装
maven是开发人员要具备的必不可少的技能之一.在使用eclipse进行开发时,我们需要安装maven插件,网上有很多教程,但是有些教程写的太过模糊.在此,我将自己的安装方法总结一下,尽量细致. 前提 ...
- C# 监控代码执行效率
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); stop ...
- yum仓库中源的配置与使用
yum 主要功能是更方便的添加/删除/更新RPM 包,自动解决包的倚赖性问题,便于管理大量系统的更新问题. yum 可以同时配置多个资源库(Repository),简洁的配置文件(/etc/yum.c ...
- (转)Spring Cloud(二)
(二期)23.微服务框架spring cloud(二) [课程23]熔断器-Hystrix.xmind0.1MB [课程23]微服务...zuul.xmind0.2MB 熔断器-Hystrix 雪崩效 ...
- activiti 5.13流程图连线名称不显示bug修复
使用modeler设计器,流程图连线名称是有显示的,但是运行结果却没显示.找到网上2遍文章,说是activiti框架中的一个bug,要修改 DefaultProcessDiagramGenerator ...