要求:

  给定一个整数(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. git 创建、切换和提交新分支

    查看本地分支 git branch 创建新的分支 git branch <newBranch> 切换分支 git checkout <branchName> 创建并切换分支 g ...

  2. CF1076D Edge Deletion 最短路径树+bfs

    题目描述 You are given an undirected connected weighted graph consisting of n n n vertices and m m m edg ...

  3. springboot整合actuator,进行运维监控

    首先引入依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  4. Django 11 form表单(状态保持session、form表单及注册实现)

    Django 11 form表单(状态保持session.form表单及注册实现) 一.状态保持 session 状态保持 #1.http协议是无状态的:每次请求都是一次新的请求,不会记得之前通信的状 ...

  5. 数据结构4.3_字符串模式匹配——KMP算法详解

    next数组表示字符串前后缀匹配的最大长度.是KMP算法的精髓所在.可以起到决定模式字符串右移多少长度以达到跳跃式匹配的高效模式. 以下是对next数组的解释: 如何求next数组: 相关链接:按顺序 ...

  6. [例] 用MappedByteBuffer更新文件内容

    import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; impor ...

  7. zabbix监控nginx mysql 服务添加

    [root@test2 ~]# rpm -ivh nginx-1.8.0-1.el7.ngx.x86_64.rpm [root@test2 ~]# cd /etc/nginx/ [root@test2 ...

  8. Linux环境搭建禅道项目管理工具

    1.开源版安装包下载 [root@iZbp ~]# wget http://dl.cnezsoft.com/zentao/9.0.1/ZenTaoPMS.9.0.1.zbox_64.tar.gz 2. ...

  9. python Gensim库建立word2vec参数说明

    from gensim.models import word2vec model = word2vec.Word2Vec(sentences, size=80, window=10,workers=6 ...

  10. svn被锁 The project was not built due to "org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir

    解决办法  :  右键该项目  ,---->Team---->选"Team"-->"Refresh/Cleanup",并确认"Ref ...