要求:

  给定一个整数(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. 数据结构28:广义表及M元多项式

    广义表,又称为列表.记作: LS = (a1,a2,…,an) ;( LS 为广义表的名称, an 表示广义表中的数据). 广义表可以看作是线性表的推广.两者区别是:线性表中的数据元素只能表示单个数据 ...

  2. Qt 学习之路 2(3):Hello, world!

     豆子  2012年8月22日  Qt 学习之路 2  107条评论 想要学习 Qt 开发,首先要搭建 Qt 开发环境.好在现在搭建 Qt 开发环境还是比较简单的.我们可以到 Qt 官方网站找到最新版 ...

  3. 【Ant】How to print all the system properties in Ant build file

    在Ant里可以使用echoproperties task来达到目的 <target name="run"> <echoproperties /> </ ...

  4. maven 打包 包含xml

    <build> <finalName>dc-exam</finalName> <!-- 包含xml文件 --> <resources> &l ...

  5. Oracle table-lock的5种模式

    Oracle中的锁定可以分为几类: 1.DML lock(data lock), 2.DDL lock(dictionary lock) 3.internal lock/latch. DML lock ...

  6. day26 网络通讯的整个流程

    一.网络通信原理 1.  互联网的本质就是一系列的网络协议 2.  互联网协议按照功能不同分为osi七层或tcp/ip五层或tcp/ip四层 各层的功能简述: [1]物理层:主要定义物理设备标准,如网 ...

  7. storm定时器

    package com.example.mail; import org.apache.storm.Config; import org.apache.storm.LocalCluster; impo ...

  8. Ubuntu 14.04下MySQL服务器和客户端的安装

    下面进行简单的配置 安装完成后通过修改/etc/mysql/my.cnf(此文件为mysql的配置文件).将文件中的binding-address=127.0.0.1注释掉.其它配置根据需要更改. H ...

  9. 解决哈希(HASH)冲突的主要方法

    https://blog.csdn.net/xtzmm1215/article/details/47177701   虽然我们不希望发生冲突,但实际上发生冲突的可能性仍是存在的.当关键字值域远大于哈希 ...

  10. Web前端常见问题

    一.理论知识 1.1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服务器端响应http请求,浏览器 ...