/// <summary>
/// 方法一:双循环
/// </summary>
/// <param name="array"></param>
/// <param name="target"></param>
/// <returns></returns>
public static List<int[]> twoSum(int[] array, int target)
{
//int[] result;
List<int[]> result = new List<int[]>();
for (int i = 0; i < array.Count(); i++)
{
for (int j = i + 1; j < array.Count(); j++)
{
if (array[i] + array[j] == target && i != j)
{
int[] aa = { i, j };
result.Add(aa);
}
}
}
return result;
}
/// <summary>
/// 方法二:单循环
/// </summary>
/// <param name="array"></param>
/// <param name="target"></param>
/// <returns></returns>
public static List<int[]> twoSum2(int[] array, int target)
{
List<int[]> result = new List<int[]>();
for (int i = 0; i < array.Count(); i++)
{
var x = array.Select((a, index) => new { a, index }).Where(a => a.a == (target - array[i]));
if (x.ToList().Count != 0 && i != x.ToList()[0].index)
{
int[] aa = { i, x.ToList()[0].index };
var y=result.Where(a => a[0] == x.ToList()[0].index && a[1] == i);
if (y.ToList().Count == 0)
result.Add(aa);
}
}
return result;
}

  控制台展示:

 static void Main(string[] args)
{
Console.WriteLine("请输入数组:"); string ss = Console.ReadLine();
int length = ss.Length;
int[] arry = new int[length];
for (int i = 0; i < length; i++)
{
string s = ss.Substring(i, 1);
arry[i] = Convert.ToInt32(s);
}
Console.WriteLine("请输入目标值:");
int target = Convert.ToInt32(Console.ReadLine());
List<int[]> result = twoSum2(arry, target);
foreach (int[] ar in result)
{
foreach (int i in ar)
{
Console.Write(i);
}
}
Console.ReadLine();
}

  

leetcode系列---Two Sum C#code的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LeetCode 1 Two Sum 解题报告

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

  7. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

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

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

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

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

随机推荐

  1. redis常用操作-键的生存时间

    System.out.println("设置 key001的过期时间为5秒:"+jedis.expire("key001", 5)); System.out.p ...

  2. Cisco路由器基本使用

    作者:小啊博 QQ:762641008 转载请声明URL:https://www.cnblogs.com/-bobo/ 一.路由器命令行使用 router>                    ...

  3. 魔鬼在细节,理解Java并发底层之AQS实现

    jdk的JUC包(java.util.concurrent)提供大量Java并发工具提供使用,基本由Doug Lea编写,很多地方值得学习和借鉴,是进阶升级必经之路 本文从JUC包中常用的对象锁.并发 ...

  4. ELK 学习笔记之 Logstash安装

    Logstash安装: https://www.elastic.co/downloads/logstash 下载解压: tar –zxvf logstash-5.6.1.tar.gz 在/usr/lo ...

  5. 1.Eclipse下载、常用配置、快捷键

    Eclipse官网下载:https://www.eclipse.org/downloads/packages/ 自动补全 位置:Eclipse——Window——Perferences——Java—— ...

  6. B-概率论-极大似然估计

    [TOC] 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:https://www.cnblogs.com/nickchen121/ ...

  7. 【TencentOS tiny】深度源码分析(1)——task

    任务的基本概念 从系统的角度看,任务是竞争系统资源的最小运行单元.TencentOS tiny是一个支持多任务的操作系统,任务可以使用或等待CPU.使用内存空间等系统资源,并独立于其它任务运行,理论上 ...

  8. UVA - 1160 X-Plosives

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. BF算法(蛮力匹配)

    输入主串a,模式b b在a中的位置 1.在串a和串b中设置比较的下标i=0,j=0: 2.重复下述操作,直到a或b的所有字符均比较完毕: 2.1如果a[i]等于b[i],继续比较a和b的下一对字符: ...

  10. 初学 Spring MVC(基于 Spring in Action)

    Spring MVC(Model-View-Controller) 当你看到本博文时,我猜你可能正面临着我已探索过的问题. 同其他博主一样,我先按照书上详细的介绍一下 Spring MVC,也是为了自 ...