题目为:

给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标.

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 练习的更多相关文章

  1. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  2. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  3. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  4. 1-Two Sum @LeetCode

    1-Two Sum 题目 思路 题目中得到的信息有: 都是整数,并且可正可负,也可一个值包含多个: 只有一个正确的结果. 方法一: 最直接的思路就是两重循环遍历,时间复杂度是O(n^2),这样肯定不行 ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  7. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  8. [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式

    Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...

  9. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

随机推荐

  1. Apache-系统-网络部分配置

    参考了很多东西,但有些理解可能会有错误的地方,希望看到错误的地方可以即使纠正. 参考的内容. http://netsecurity.51cto.com/art/200608/30428.htm htt ...

  2. iOS 7 Pushing the Limits - Good & Bad Namings in Cocoa

    Cocoa is a dynamically typed language, and you can easily get confused about what type you are worki ...

  3. C++ Interview - using new and delete to alloc and free memory

    1. dynamic create object and initialization int *pi = new int; // pi points to an uninitialized int ...

  4. hdu 5594 ZYB's Prime 最大流

    ZYB's Prime Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...

  5. Html Agility Pack基础类介绍及运用

    第一篇只对Html Agility Pack做了一个大概的介绍,在接下来的章节会比较深入的介绍Html Agility Pack. Html Agility Pack 源码中的类大概有28个左右,其实 ...

  6. 关于THIS_FILE

    VC++中本身就有内存泄漏检查的机制,可以在向导生成的支持MFC的工程中看到如下代码:  #ifdef _DEBUG  #define new DEBUG_NEW  #undef THIS_FILE  ...

  7. DB2 重新设定表自增字段的当前值

    转自:http://blog.csdn.net/jionghan3855/article/details/2709073 1.ALTER TABLE UKEY_INFO_TAB ALTER COLUM ...

  8. 548 - Tree (UVa OJ)

    Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...

  9. php常用代码(一)

    一:获取上个小时 方法1:date("H",strtotime("-1 hours"); 方法2:date('H',time()-60*60); 方法3:ech ...

  10. 关于在线查看相关开源源码的网站,包括Android源码

    无废话,纯干货! 各种源码自行搜索: http://grepcode.com/ Android源码:http://grepcode.com/project/repository.grepcode.co ...