leetcode系列---Two Sum C#code
/// <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的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [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 ...
随机推荐
- NAT网络下tcp_tw_recycle参数引起的故障
记录一次阿里云服务器故障排查思路 公司网络是nat 环境 问题: 同一个服务有两台服务器 172.19.19.252 172.19.19.187 两台服务器 要连node5 发现172.19.19.2 ...
- Android中Project、Module的区别
Project 可以包含多含 Module. Project相当于eclipse里面的工作区间,module相当于其project.module可以作为狭义上的模块,可以多个app共用的module. ...
- JavaSE----02.Java语言基础
02.Java语言基础 1.关键字 Java关键字是电脑语言里事先定义的,有特别意义的标识符,有时又叫保留字,还有特别意义的变量.Java的关键字对Java的编译器有特殊的意义,他们用来表示一 ...
- 【Java基础】Java中的语法糖
目录 Java中的语法糖 switch对String和枚举类的支持 对泛型的支持 包装类型的自动装箱和拆箱 变长方法参数 枚举 内部类 条件编译 断言 数值字面量 for-each try-with- ...
- @DateTimeFormat注解
@DateTimeFormat在spring-context依赖下,所在包如下 当form表单中出现时间字段需要跟pojo对象中的成员变量进行数据绑定时,springmvc框架中的时间数据无法自动绑定 ...
- Spring Boot 2.X(七):Spring Cache 使用
Spring Cache 简介 在 Spring 3.1 中引入了多 Cache 的支持,在 spring-context 包中定义了org.springframework.cache.Cache 和 ...
- CSS3新单位vw、vh、vmin、vmax使用详解
像 px.em 这样的长度单位大家肯定都很熟悉,前者为绝对单位,后者为相对单位.CSS3 又引入了新单位:vw.vh.vmin.vmax.下面对它们做个详细介绍. 新单位也成为视窗单位,视窗(View ...
- 推荐一款可以丰富博文GIF免费录制工具——GifCam
在网上写博文,看别人添加gif图片很好奇,于是尝试制作并传递了一次,确实挺有意思的,于是分享一下. gifcam是一个绿色的,可以录制GIF动图的小软件,十分适合用来录制电脑的各种操作. 在这里附上工 ...
- hihocoder 数论二·Eular质数筛法
数论二·Eular质数筛法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上次我学会了如何检测一个数是否是质数.于是我又有了一个新的问题,我如何去快速得 ...
- Real Beauty真正的美丽
[1]Beauty is only skin-deep. Physical beauty can only be held fleetingly. Real beauty is much deeper ...