leetcode题1Two sum 练习
给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
public int[] TwoSum(int[] nums, int target) {
int[] result=new int[];//创建一个返回数组; for (int j = ; j < nums.Length; j++)//{1,4,5,7,9,10};//{9,6,5,3,1,0}外层循环,传进的数组赋值
{
for (int k = j+; k < nums.Length; k++)//创建内层循环,内层循环主要是让temp[i]=target-nums[i];让temp[i]的值与数组nums[i]比自己大的元素比较看是否相等。
{ if (target - nums[j] == nums[k])
{ result[] = j;//相等就记录该位置。
result[] = k;//记录相等的位置。
} }
} return result;
}
附上该例源码:
namespace ConsoleApplication1
{
class addtwonum
{
public static void Main(string[] args)
{
int[] num = { , , , , , };//{9,6,5,3,1,0}
int[] result = getnum(num, );
for (int i = ; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
Console.ReadLine();
}
//
//{1,2,3,4,6} tagart =5
//a[0]+a[3] ,a[1]+a[2] [0,3],[1,2]
private static int[] getnum(int []nums,int tagart)
{
int[] result=new int[];
int[] temp=new int[nums.Length];
//for (int i = 0; i < nums.Length; i++)
// temp[i] = tagart - nums[i];
for (int j = ; j < nums.Length; j++)//{2, 4, 4, 7, 9, 10};//{6,4,4,-1,-2,-3}|{7,5,5,2,0,-1}
{
for (int k = j+; k < nums.Length; k++)
{
//result[0] = j;
//result[1] = k;
if (tagart - nums[j] == nums[k])
{
//j = nums.Length - 1;
//k = nums.Length;
result[] = j;
result[] = k;
} }
} return result;
} }
}
leetcode题1Two sum 练习的更多相关文章
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 1-Two Sum @LeetCode
1-Two Sum 题目 思路 题目中得到的信息有: 都是整数,并且可正可负,也可一个值包含多个: 只有一个正确的结果. 方法一: 最直接的思路就是两重循环遍历,时间复杂度是O(n^2),这样肯定不行 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式
Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- ASP.NET Web Forms的改进
虽然ASP.NET Web Forms不是vNext计划的一部分,但它并没有被忽视.作为Visual Studio 2013 Update 2的一部分,它重新开始支持新工具.EF集成和Roslyn. ...
- NAVICATE 修改存储过程提示PROCEDURE _Navicat_Temp_Stored_Proc already exists 解决方法
推测是navicate修改存储过程会自动名为_Navicat_Temp_Stored_Proc的存储过程 创建完毕后自动drop 由于某些原因或bug导致最终没有drop,于是修改存储过程失败. 所以 ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- 在编辑模式中一个ASP.NET应用详细视图显示集合属性 编辑模式和只读模式
https://documentation.devexpress.com/#Xaf/CustomDocument3230
- !"false"==fasle
首先要搞清楚优先级 !"false" --->false false == false;---->true !""--->true; !&qu ...
- javascript exec方法
题目 取出 "[大哭][尴尬][发怒][发怒][调皮][调皮][呲牙]" 串中[]里面的东西 思路 正则匹配,/\[(.+?)\]/ig; exec方法循环 exec() 方法的功 ...
- C#中String和string有什么区别
在C#中,string 是 System.String 的别名,所以基本上在使用时是没有差别的. 习惯上,我们把字符串当作对象时(有值的对象实体),我们用string.而我们把它当类时(需要字符串类中 ...
- Replace INTO与INSERT INTO的不同之处
Replace INTO和INSERT INTO的区别: REPLACE的运行与INSERT很相似.只有一点例外,假如表中的一个旧记录与一个用于PRIMARY KEY或一个UNIQUE索引的新记录具有 ...
- poll机制分析
更多文档:http://pan.baidu.com/s/1sjzzlDF linux poll/select用法及在字符驱动中的简单实现 1.poll和select 使用非阻塞I/O 的应用程序常常使 ...
- 算法总结—深度优先搜索DFS
深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...