Which are in?
Which are in?
Given two arrays of strings a1 and a2 return a sorted array in lexicographical order and without duplicates of the strings of a1 which are substrings of strings of a2.
Example: a1 = ["arp", "live", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns ["arp", "live", "strong"]
a1 = ["tarp", "mice", "bull"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns []
Note: Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.
using System;
using System.Linq;
using System.Collections.Generic; class WhichAreIn
{
public static string[] inArray(string[] array1, string[] array2)
{
// your code
List<string> list = new List<string>();
foreach (string strItem1 in array1)
{
foreach (string strItem2 in array2)
{
if (strItem2.Contains(strItem1))
{
if (list.Contains(strItem1) == false)
{
list.Add(strItem1);
}
}
}
}
list = list.OrderBy(x => x).ToList() ;
return list.ToArray();
}
}
其他人的解法:
需要注意distinct的用法,以及any的用法
using System;
using System.Linq; class WhichAreIn
{
public static string[] inArray(string[] array1, string[] array2)
{
return array1.Distinct()
.Where(s1 => array2.Any(s2 => s2.Contains(s1)))
.OrderBy(s => s)
.ToArray();
}
}
随机推荐
- [.Net MVC] 使用 log4net 日志框架
项目:后台管理平台 意义:项目开发中提出增加日志功能,对关键的操作.程序运行中的错误信息进行记录,这对程序部署后的调试有很大意义. 注:本文只是对网上搜集的信息进行了整合,以备今后查询. 关键字:.N ...
- 09_TomCat_基础知识
[TomCat目录结构] bin----------存放TomCat的操作命令.bat:window版本,sh:Linux版本. startup.bat: 后台在调用catalina.bat st ...
- Oracle PL/SQL 异常处理
Oracle数据库中的异常:没有异常的转移,因为没有受检异常和非受检异常得区分. 1.异常的产生: 2.异常的处理: declare --变量定义,初始化赋值. begin --变量的赋值,函数调用, ...
- OpenJudge 2803 碎纸机 / Poj 1416 Shredding Company
1.链接地址: http://poj.org/problem?id=1416 http://bailian.openjudge.cn/practice/2803 2.题目: 总时间限制: 1000ms ...
- windows phone 生产含logo的二维码
这几天了解二维码了解的比较多,不过就是没深入了解.google了一下生产含logo二维码的思路,就是把logo给画到生成的二维码上,还是因为二维码的纠错能力足够好啊,用Graphics对图片进行操作? ...
- 远程连接Ucenter数据库
网站和Ucenter不是同一服务器的连接方法~我折腾了好几天,终于找到了这方法!各位连接不上的不妨试试~什么事只有试过才知道行不行! define('UC_CONNECT', 'mysql'); de ...
- FireMonkey消息机制
interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, FMX.Forms, FMX.Plat ...
- 数据结构与算法C语言实现笔记(1)--表
声明:此一系列博客为阅读<数据结构与算法分析--C语言描述>(Mark Allen Weiss)笔记,部分内容参考自网络:转载请注明出处. 1.表 表是最简单的数据结构,是形如A1.A2. ...
- Vijos P1062 迎春舞会之交谊舞
题目链接:https://vijos.org/p/1062 题意:输入n(n <= 1500)个女生左边有多少个男生.每个女生都和她左边最近的男生跳舞. 输出每个女生到可以与之跳舞的男生之间有几 ...
- 【方言】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 几种 方言配置差异 <?xml v ...