记录被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的更多相关文章

  1. JavaScript的two-sum问题解法

    一个很常见的问题,找出一个数组中和为给定值的两个数的下标.为了简单一般会注明解只有一个之类的. 最容易想到的方法是循环遍历,这里就不说了. 在JS中比较优雅的方式是利用JS的对象作为hash的方式: ...

  2. twoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  3. [LeetCode] TwoSum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  5. [Lintcode two-sum]两数之和(python,双指针)

    题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...

  6. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

  7. LeetCode——TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  9. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. leetcode — two-sum

    package org.lep.leetcode.twosum; import java.util.Arrays; import java.util.HashMap; import java.util ...

随机推荐

  1. shell实现自动部署两台tomcat项目+备份

    就做个记录吧, 其实也没啥好说的. 主机 #!/bin/bash TODAY=$(date -d 'today' +%Y-%m-%d-%S) MIP="192.168.180.24" ...

  2. linux配置powerline(bash/vim)美化

    安装powerline需要pip 链接:https://pan.baidu.com/s/1Jc59VD35PYic2fTK5v8h1w 密码:otfp pip curl https://bootstr ...

  3. 热扩容LVM形式的/(根)分区(无损增大、缩小LVM分区)

    警告! 本文为虚拟机环境,生产环境请务必在操作前优先备份重要数据! 再有,请确保所需扩充的分区为非进程占用分区 实验背景:当时规划系统分区时/(根)目录分配过小 实验目的 : 无损增大/(根)分区容量 ...

  4. 前端 --- 2 css

    一. CSS的几种引入方式 1.行内样式 2.内部样式 写在网页的<head></head>标签对的<style></style>标签对中 3.外部样式 ...

  5. 从Git上导入Maven 项目到Eclipse

    Note: 经验之谈,操作过程中有不懂的地方可以留言问. Step: Open the Eclipse: --1.File>>Import>>Git:Project from ...

  6. 没有使用Material组件

    // 这个App没有使用Material组件, 如Scaffold. // 一般来说, app没有使用Scaffold的话,会有一个黑色的背景和一个默认为黑色的文本颜色. // 这个app,将背景色改 ...

  7. Linux 系统下 matplotlib 中文乱码解决办法

    亲测有效的方法之一: 1.下载中文字体simhei.ttf SimHei可以到http://fontzone.net/download/simhei下载 2.找到matplotlib相关的font文件 ...

  8. thinkphp如何省略index.php

    省略index.php叫做 伪静态化; 共有四个步骤: MariaDB[(none)]: 表示, 目前没有选择/使用 任何数据库. 如果use了数据库, 会提示: MariaDB[mysql]... ...

  9. EXCEL 基本函数

    案例2:修改非法日期 TODAY(),显示今天日期,数据格式是日期,如果是常规,就是数字. EXCEL 起始日期,1900/1/1是第一天 日期输入方式要正确 时间数据格式  1:00:00  = 1 ...

  10. 【问题解决:死锁】Lock wait timeout exceeded; try restarting transaction的问题

    执行数据删除操作时一直超时并弹出Lock wait timeout exceeded; try restarting transaction错误 解决办法 1.先查看数据库的事务隔离级别 select ...