/// <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. Maven 梳理 -scope属性

    在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺省值,适用于所有阶 ...

  2. Spring 梳理-传递模型数据到视图

    传递model,返回string,不指定返回key package com.jt; import com.myOrg.DeptUserDomain; @Controller @RequestMappi ...

  3. thymeleaf常用配置说明

    #spring.thymeleaf.cache = true #启用模板缓存. #spring.thymeleaf.check-template = true #在呈现模板之前检查模板是否存在. #s ...

  4. JS/JQuery 文档加载完成

    1. $(function(){...}); 文档树加载完执行的方法:(注意:不包括图片.css.js等文件): 一个页面中可以有多个$(function(){...}); 方法: 两种写法: //第 ...

  5. Salesforce学习之路-developer篇(三)利用Visualforce Page实现页面的动态刷新案例学习

    Visualforce是一个Web开发框架,允许开发人员构建可以在Lightning平台上本地托管的自定义用户界面.其框架包含:前端的界面设计,使用的类似于HTML的标记语言:以及后端的控制器,使用类 ...

  6. word2vec预训练词向量

    NLP中的Word2Vec讲解 word2vec是Google开源的一款用于词向量计算 的工具,可以很好的度量词与词之间的相似性: word2vec建模是指用CBoW模型或Skip-gram模型来计算 ...

  7. C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

    目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...

  8. 用 Sphinx 搭建博客时,如何自定义插件?

    之前有不少同学看过我的个人博客(http://python-online.cn),也根据我写的教程完成了自己个人站点的搭建. 点此:使用 Python 30分钟 教你快速搭建一个博客 为防有的同学不清 ...

  9. C--二分搜索

    //二分搜索 //时间复杂度小 log2 n int search(int key, int a[], int len) { ; ; ; int mid; while(left<right) { ...

  10. Python之string模块(详细讲述string常见的所有方法)

    相信不少学习python的程序员都接触过string模块 string模块主要包含关于字符串的处理函数 多说无益,初学python的小伙伴还不赶紧码起来 接下来将会讲到字符串的大小写.判断函数. 以及 ...