/// <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. 关于react-router最新版本的使用

    现在react-router已经更新到了5.1.1版本,在一些使用方法上较之前有了很多改变,现做初步列举,以后会陆续更新. 关于引入react-router和基本使用 旧版本中引入react-rout ...

  2. MySQL-Access denied for user 'username'@'localhost' (using password: YES) 解决

    使用navicat新建MySQL用户保存时提示 Access denied for user 'username'@'localhost' (using password: YES): 解决方法: 请 ...

  3. 阿里云服务器CentOS6.9安装Tomcat

    上篇讲了CentOS6.9安装jdk,这篇来讲Tomcat的安装,本来准备使用yum命令安装的,但是通过 yum search tomcat 发现只有tomcat6,所以就在官网下了一个tomcat8 ...

  4. linux查看系统的一些版本号指令

    1.查看系统 [root@iZbp1eoiap1e1jb6pvo390Z ~]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch ...

  5. Thread线程类

    设置线程名 查看线程名是很简单的,调用Thread.currentThread().getName()即可. public class MyThreadDemo { public static voi ...

  6. Android_基于监听的事件处理机制

    一.引言 在经过几天的学习之后, 首先熟悉了几大基本布局以及一些常用控件的使用方法,目前正在学习如何实现一个基本的登录注册界面及其功能,而实现功能就需要我们采用事件处理机制来进行调用事件处理方法.以下 ...

  7. shell命令大全笔记

    ## -print 将匹配的文件输出到标准输出## -exec 将匹配的文件执行该参数所给出的shell命令## -ok 将匹配的文件执行该参数所给出的shell命令,每次执行命令有提示 #----- ...

  8. Java基础学习(七) - 异常处理

    1.异常概念 异常指的是程序在执行过程中出现的非正常的情况,导致JVM的非正常停止.在Java中,异常是一个类,产生异常就是创建异常对象并抛出一个异常对象. 异常指的并不是语法错误,语法错误,编译不会 ...

  9. 【Java】获取当前操作系统桌面路径

    //当前用户桌面 File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory(); String desktopPat ...

  10. 大神都在用的yum源

    本文原创首发于公众号:编程三分钟 yum 命令的使用 yum命令天天都在用,都快用烂了,但是很多人不知道为什么只要联网,yum命令就能像老奶奶手中的魔法棒一样,随心所欲的下载到想到的包. 比如你想装个 ...