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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
随机推荐
- iOS沙盒文件目录
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么.documents,tmp,app,Library.(NSHomeDirectory() ...
- PHP删除目录下的空目录
function rm_empty_dir($path){ if(is_dir($path) && ($handle = opendir($path))!==false){ ...
- Python之将字符串转换为字节的两种方法
s = '你是谁' a = bytes(s,'utf-8') # ==> 得出的 a 的结果就是对应的字节 s.encode('utf-8') # ==> 该命令将字符串转换为字节形式
- 树莓派-开启spi
1. sudo raspi-config #进入树莓派配置页 2. #进入每5项,进入启用spi即可
- 老男孩python作业3-购物车程序优化
购物车优化要求:用户入口: 1.商品信息存在文件里 2.已购商品,余额记录.第一次启动程序时需要记录工资,第二次启动程序时谈出上次余额 3.允许用户根据商品编号购买商品 4.用户选择商品后,检测是否够 ...
- c#工具类之Int扩展类
public static class IntHelper { /// <summary> /// 转换为2进制字符串 /// </summary> /// <param ...
- python 文件与异常
####文件### r: -只能读 不能写 -读取文件不存在,是会报错 r+: - 可以执行读写操作; - 文件不存在, 报错: w: -只能写,不能读 -会清空文件内容 -文件不存在,会新建文件 w ...
- Jenkins自动化CI CD流水线之3--参数化构建
一. 背景 如果只是简单的构建,jenkins自己默认的插件可以做,但是如果我们想要在构建过程中有更多功能,比如说:选择性构建.传参.项目指定变量等等其他功能,基础的参数化构建可以实现一些简单功能,但 ...
- kafka删除主题数据和删除主题
1.删除主题 在server.properties中增加设置,默认未开启 delete.topic.enable=true 删除主题命令 /bin/kafka-topics --delete --to ...
- robotFramework 读取Excel文件
1.Robotframework读取Excel文件 第一步:先安装ExcelLibrary 可以直接通过命令安装:pip install robotframework-ExcelLibrary 安装完 ...