要求:

  给定一个整数(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. asp.net core 自定视图主题 实现IViewLocationExpander接口

    新建ThemeViewLocationExpander.cs 实现IViewLocationExpander接口 /// <summary> /// 自定视图主题 实现IViewLocat ...

  2. react 中文文档案例四 (登陆登出按钮)

    import React from 'react'; import ReactDOM from 'react-dom'; class LoginControl extends React.Compon ...

  3. 前端优化系列之一:dns预获取 dns-prefetch 提升页面载入速度

    问题:怎么做到dns域解析?   用于优化网站页面的图片   问题:怎么提升网站性能? dns域解析,是提升网站的一个办法.   DNS Prefetch,即DNS预获取,是前端优化的一部分. 一般来 ...

  4. ueditor chrome bug

    一.概述: 关于UEditor在谷歌浏览Chrome打开选择指上传图片,发现[点击选择图片]时无法立即弹出选择框,而是等4-7秒钟才显示出来的BUG,试了N多方法,改层级都用了,也无效.在网上找到了一 ...

  5. Codeforces Round #520 (Div. 2) B math(素数因子的应用)

    题意: 给出一个n ; 有两个操作: 1,mul A   ,   n=n*A   : 2,sqrt()  ,  n=sqrt(n)  开更出来必须是整数 : 求出经过这些操作后得出的最小  n , 和 ...

  6. thinkphp引入模板view

    3.1 模板放在哪儿? 放在模块的view目录下并且每个控制器的模板,要在与控制器同名的目录下. 以 index.php/Home/User/add则对应的模板在 /Home/view/User/ad ...

  7. 安装tomcat时遇到的问题

    1.刚开始在eclipse配置的tomcat是免安装的,后来提示 所以后来配置了一个安装版的. 2.后来运行server发现报错:8080,8005,端口被占用,然后关闭xammp上的server,然 ...

  8. VS 开发中觉得不错的几款插件

    1.C# Outline 2013  {}折叠 2.Indent Guides:为缩进添加竖直线 3.ResXManager:多语言资源文件编辑 4.C# Formatter:代码清理 5.Highl ...

  9. Silverlight FullScreen 全屏

    <UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...

  10. Android中当前Activity跳转到当前Activity页面

    步骤:先关闭自己,在跳转 case R.id.btn_copy:// 复制 Toast.makeText(mContext, "正在复制", Toast.LENGTH_SHORT) ...