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 ...
随机推荐
- 读《深入理解Elasticsearch》点滴-过滤器
1.过滤器不影响文档得分 2.过滤的唯一目的是用特定筛选条件来缩小结果范围:而查询不仅缩小结果范围,还会影响文档的得分 3.过滤器运行更加高效(因为不用计算得分) 4.通常过滤器使用Bits接口,返回 ...
- P5369 [PKUSC2018]最大前缀和
状态压缩 P5369 题意:求所有排列下的最大前缀和之和 一步转化: 求最大前缀和的前缀由数集S组成的方案数, 统计答案时直接乘上sum(S)即可 考虑最大前缀和的性质: 设最大前缀和为sum[i] ...
- 并发编程之多线程(Java)
一.线程与进程区别 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以线程基本上是 ...
- grep、正则表达式
1.grep :文本搜索工具 -i:忽略大小写--color:匹配到的内容高亮显示-v:显示没有被模式匹配到的行-o:只显示被模式匹配到的字符串-n:显示匹配到行的行号-E:使用扩展正则表达式-A:后 ...
- 爬虫那点事,干就玩了之seleunim
目录 selenium 环境准备 代码环境 开始爬虫 操作js 截图 切换窗口 在当前窗口切换访问地址 管理cookie # 加入战队 微信公众号 # 加入战队 微信公众号 做技术我们最重要的是[做] ...
- 【linux】Tomcat 安装
登录linux后,切换目录到 /usr/local cd /user/local 在/usr/local目录新建文件夹servers用于存放tomcat文件 mkdir servers 在文件夹ser ...
- 【JavaScript】使用纯JS实现多张图片的懒加载(附源码)
一.效果图如下 上面的效果图,效果需求如下 1.还没加载图片的时候,默认显示加载图片背景图 2.刚开始进入页面,自动加载第一屏幕的图片 3.下拉界面,当一张图片容器完全显露出屏幕,即刻加载图片,替换背 ...
- Mysql数据类型TINYINT(1)与BOOLEAN踩坑记
熟悉Mysql的同学应该都知道,Mysql查询的boolean结果将输出为0或者1. 比如: ; 其输出结果为1. 查阅mysql官方文档仅找到如下描述: 11.10 Using Data Types ...
- Python多任务之协程
前言 协程的核心点在于协程的使用,即只需要了解怎么使用协程即可:但如果你想了解协程是怎么实现的,就需要了解依次了解可迭代,迭代器,生成器了: 如果你只想看协程的使用,那么只需要看第一部分内容就行了:如 ...
- redis系列之------字典
前言 字典, 又称符号表(symbol table).关联数组(associative array)或者映射(map), 是一种用于保存键值对(key-value pair)的抽象数据结构. 在字典中 ...