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 ...
随机推荐
- 【转】深入浅出异步I/O模型
转自:http://pengpeng.iteye.com/blog/868643 从上篇文章的介绍我们知道linux内核根据TCP/IP网络模型,给我们隐藏了传输层以下的网络传输细节,我们的网络应用程 ...
- 【M27】要求或者禁止对象产生于heap之中
1.要求对象只能产生于heap之中,该怎么办? 栈上的对象肯定调用构造方法和析构方法(离开作用域的时候),因此,要求对象只能产生于heap之中,也就是禁止栈上产生对象,解决办法有两种:将所有的构造方法 ...
- Android 获取控件相对于屏幕位置
// View宽,高 public int[] getLocation(View v) { int[] loc = new int[4]; int[] location = new int[2]; v ...
- hdu 5272 Dylans loves numbers 水题
Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...
- 从来没有天才 靠自己创造未来——Leo鉴书(29)
之前在网上跟朋友们聊起天才这个话题,我认来从来没什么所谓天才,有朋友认为有的,只是我们定义不同,要不你看看苏轼? 持天才论者持两个观点:有些人天生擅长干某些事儿,也许是基因作怪:有些人的能力是上帝或者 ...
- 【JavaScript】Javascript中的函数声明和函数表达式
Javascript有很多有趣的用法,在Google Code Search里能找到不少,举一个例子: <script> ~function() { alert("hello, ...
- 【JavaScript】直接拿来用!最火的前端开源项目(一)
摘要:对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,本文整理GitHub上最火的前端开源项目列表,这里按分类的方式列出前九个. 对于开发者而言 ...
- MyBatis学习练习
转自:http://ccchhhlll1988-163-com.iteye.com/blog/1415621 基本目的:利用Mybatis完成对一个表简单的select.insert.update.d ...
- How to Copy and Paste in the Ubuntu Gnome Terminal
How to Copy: Select the content in terminal use your mouse , and then use Ctrl + Shift + C to copy t ...
- Oracle中删除重复的经典方案
DELETE FROM Personnel WHERE ROWID < (SELECT MAX(P1.ROWID) FROM Personnel AS P1 WHERE P1.dup_id = ...