要求:

  给定一个整数(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. Eclipse 创建Maven 项目

    https://www.cnblogs.com/noteless/p/5213075.html

  2. 10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)

    Level:   Easy 题目描述: Given a string s and a non-empty string p, find all the start indices of p's ana ...

  3. codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)

    题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质  2  字典序大于等于原数组  3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...

  4. Java 目录和文件的复制

    1.复制一个目录及其子目录.文件到另外一个目录 //复制一个目录及其子目录.文件到另外一个目录 public void copyFolder(File src, File dest) throws I ...

  5. vue2.0组件的生命周期

    beforeCreate(){ console.log(new Date().getTime()) let data = this.text; console.log('组件创立之前') consol ...

  6. my.ZC

    1.100级,裸身,满技能,属性模拟 数据:   大唐 方寸 化生 龙宫 普陀 地府 狮驼 魔王   气血 1200 1900 2600 1200 2600 2600 1900 1900   魔法 7 ...

  7. MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建

    : 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程  API    Pom.xml:不用引用任何东西  Provider    Pom.xml:要denpend ...

  8. VBS 操作Excel

    VBS操作Excel 打开excel Dim objExcel,objWorkbook,objSheet Set objExcel=CreateObject("excel.applicati ...

  9. ace+validate表单验证(两种方法)

    //修改密码(直接在validate中验证提交) $("#changePassword").on(ace.click_event, function() { var html = ...

  10. Spark生态系统

    在大数据非常流行的今天,每个行业都在谈论大数据,每个公司(互联网公司,传统企业,金融行业等)都在讨论大数据.高层管理者利用大数据来进行决策:数据科学家利用大数据来进行业务创新:程序员利用大数据来完成项 ...