LeeCode 第1题
要求:
给定一个整数(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题的更多相关文章
- leecode第二十三题(合并K个排序链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第二十一题(合并两个有序链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- leecode第二十题(有效的括号)
class Solution { public: bool isValid(string s) { ,end=s.size()-; )//万万没想到,他把空字符串当成true了 return true ...
- leecode第八题(字符串转换整数 (atoi))
;//判断返回0是因为错误还是真的是0 class Solution { public: int myAtoi(string str) {//写的很丑 if (str=="") ; ...
- leecode第七题(整数反转)
题解给的思路: ; class Solution { public: int reverse(int x) { ;//如果这里还是int,会在判断前就被裁剪了,无法判断溢出 ; ) flag=; wh ...
- leecode第五题(最长回文子串)
class Solution { public: string longestPalindrome(string s) { int len = s.length(); || len == ) retu ...
- leecode第三题(无重复字符的最长子串)
class Solution { public: int lengthOfLongestSubstring(string s) { int len=s.size(); ||len==)//边界 ret ...
- leecode第十一题(盛最多水的容器)
class Solution { public: int maxArea(vector<int>& height) { int len=height.size();//错过,少了i ...
- leecode第四题(寻找两个有序数组的中位数)
题解: class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<i ...
- LeeCode第一次刷题(两数相加)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
随机推荐
- asp.net core 简化模型验证 modelState.IsValid不用每一个写
第一种:直接在执行action之前验证模型 实现 IActionFilter public class ModelStateFilter : IActionFilter { public void O ...
- window下安装git与git使用
有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...
- 【Leetcode】Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- freemarker ! 用法
${(user.name)!""} 请注意,是打了()的 也就是它会先判断user是不是为null 在判断user.name 是不是为null
- NowCoder数列(矩阵快速幂变式)
时间限制 3000 ms 内存限制 32768 KB 代码长度限制 100 KB 题目描述 NowCoder最近在研究一个数列: * F(0) = 7 * F(1) = 11 * F(n) = F(n ...
- hdu6299 Balanced Sequence 贪心
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b, ...
- linux学习五
一.系统服务管理 1.概念 服务(service) 本质就是进程,但是是运行在后台的,通常都会监听某个端口,等待其它程 序的请求,比如(mysql , sshd 防火墙等),因此我们又称为守护进程,是 ...
- day16 类之间的关系 特殊成员
类与类之间的关系1.依赖关系(一个对象当另一个对象的参数) 关系最浅, 特殊成员: 1. 类名() 会自动调用 __init__() class Foo: def__init__(self, nam ...
- Xsheel远程链接ECS,Xftp上传文件到ECS
下载Xshell 工具:用来远程实现远程访问. 下载Xftp工具:方便用来上传文件. 下载方式直接百度搜索,如下如: 下载完成之后安装. 接下来就是链接云服务器了. 直接百度经验按照步骤来就OK了,如 ...
- Subarray Sum K
Given an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should ...