要求:

  给定一个整数(int)数组(Array)和一个目标数值(Target),找出数组中两数之和等于目标值(target)的两个元素的下标位置,

假设:结果唯一,数组中元素不会重复。

本人思路:分别正序、倒序遍历数组求得结果。

代码如下:

public class Solution {
public int[] TwoSum(int[] nums, int target) { for(int i=;i<nums.Length;i++)
{
for(int j=nums.Length-;j>;j--)
{
if(nums[i]+nums[j]==target)
{
return new int[]{i,j};
}
} }
throw new Exception("没有答案");
}
}

  执行时长:这。。。    

  

  最优方法:  

public class Solution {
public int[] TwoSum(int[] nums, int target) { Dictionary<int, int> dic = new Dictionary<int, int>(); for (int i = ; i < nums.Length; i++)
{
int tag = target - nums[i];
if (dic.ContainsKey(tag))
{
return new int[] { dic[tag], i };
}
else
{
dic.Add(nums[i], i);
} } throw new Exception("没有答案");
}
}

  执行时长:

  

  个人总结:多思考题干,多探索解决方案。

 

  题目原文: Two Sum

  题目解析: Two Sum Solution

LeeCode 第1题的更多相关文章

  1. leecode第二十三题(合并K个排序链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. leecode第二十一题(合并两个有序链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  3. leecode第二十题(有效的括号)

    class Solution { public: bool isValid(string s) { ,end=s.size()-; )//万万没想到,他把空字符串当成true了 return true ...

  4. leecode第八题(字符串转换整数 (atoi))

    ;//判断返回0是因为错误还是真的是0 class Solution { public: int myAtoi(string str) {//写的很丑 if (str=="") ; ...

  5. leecode第七题(整数反转)

    题解给的思路: ; class Solution { public: int reverse(int x) { ;//如果这里还是int,会在判断前就被裁剪了,无法判断溢出 ; ) flag=; wh ...

  6. leecode第五题(最长回文子串)

    class Solution { public: string longestPalindrome(string s) { int len = s.length(); || len == ) retu ...

  7. leecode第三题(无重复字符的最长子串)

    class Solution { public: int lengthOfLongestSubstring(string s) { int len=s.size(); ||len==)//边界 ret ...

  8. leecode第十一题(盛最多水的容器)

    class Solution { public: int maxArea(vector<int>& height) { int len=height.size();//错过,少了i ...

  9. leecode第四题(寻找两个有序数组的中位数)

    题解: class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<i ...

  10. LeeCode第一次刷题(两数相加)

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...

随机推荐

  1. STL用法

    map.find(key) 获取map容器中指定键值x的元素,如果找到,返回此元素的迭代器,否则返回map::end()的迭代器(即查找到容器的末尾都没有找到此元素).

  2. springcloud系列四 搭建服务模块重点讲解

    首先这个服务地址:一定不要写错,是自己注册中心开启的地址 如果注意到这些了,可以简单的进行操作,也可以不需要mybatis与数据库连接,在controller里直接返回相应的数据可以了,不用这么幸苦的 ...

  3. POJ1052 Plato's Blocks

    题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...

  4. [Shell]Shell学习笔记之for

    关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿…1. for((i=1;i<=10;i++));do e ...

  5. window 系统 修改服务器远程登录端口

    window 系统 [ 默认3389远程端口 ] 快捷键:Ctrl+R  然后输入“regedit”,打开注册表 或者 单击左下角[开始]——[运行],然后在输入框输入 regedit,点击确定,打开 ...

  6. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  7. eclipse中找不到base64包的解决方法

    eclipse中找不到base64包的解决方法 2017年08月26日 11:05:26 yzp_leo 阅读数:634 标签: javaeclipsebase64更多 个人分类: 日记   ecli ...

  8. 「linux」后台启动nohup经常跟的>/dev/null 2>&1是什么意思

    https://www.cnblogs.com/wangsongbai/p/10215155.html 我们在写启动脚本的时候,会用nohup &,这个大家都知道,但是经常会有 >/de ...

  9. 2.2 Rust 数据类型

    2.2 数据类型 let guess: u32 = "42".parse().expect("Not a number!"); Rust has four pr ...

  10. Git命令行中文显示错误

    中文文件名乱码(git status.git log.git pull .git push) #不对0x80以上的字符进行quote,解决git status/commit时中文文件名乱码git co ...