问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3818 访问。

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入:1->2->4, 1->3->4

输出:1->1->2->3->4->4


Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Input: 1->2->4, 1->3->4

Output: 1->1->2->3->4->4


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3818 访问。

public class Program {

    public static void Main(string[] args) {
var A = "this apple is sweet";
var B = "this apple is sour"; var res = UncommonFromSentences(A, B);
ShowArray(res); Console.ReadKey();
} private static void ShowArray(IList<string> array) {
foreach(var domain in array) {
Console.Write($"{domain} ");
}
Console.WriteLine();
} private static string[] UncommonFromSentences(string A, string B) {
string[] wordA = A.Split(' ');
string[] wordB = B.Split(' ');
var dicA = new Dictionary<string, int>();
var dicB = new Dictionary<string, int>();
var res = new List<string>();
foreach(var word in wordA) {
if(dicA.ContainsKey(word)) {
dicA[word]++;
} else {
dicA[word] = 1;
}
}
foreach(var word in wordB) {
if(dicB.ContainsKey(word)) {
dicB[word]++;
} else {
dicB[word] = 1;
}
}
foreach(var kvp in dicA) {
if(kvp.Value == 1 && !dicB.ContainsKey(kvp.Key)) {
res.Add(kvp.Key);
}
}
foreach(var kvp in dicB) {
if(kvp.Value == 1 && !dicA.ContainsKey(kvp.Key)) {
res.Add(kvp.Key);
}
}
return res.ToArray();
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3818 访问。

sweet sour

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#21-合并两个有序链表(Merge Two Sorted Lists)的更多相关文章

  1. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  2. LeetCode 21:合并两个有序链表 Merge Two Sorted Lists

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. Merge two sorted linked lists and return it as a new ...

  3. [Swift]LeetCode21. 合并两个有序链表 | Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. Leetcode题库——21.合并两个有序链表

    @author: ZZQ @software: PyCharm @file: mergeTwoLists.py @time: 2018/9/16 20:49 要求:将两个有序链表合并为一个新的有序链表 ...

  5. LeetCode 23. 合并K个排序链表(Merge Two Sorted Lists)

    23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k S ...

  6. Java实现 LeetCode 21 合并两个有序链表

    21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...

  7. <每日 1 OJ> -LeetCode 21. 合并两个有序链表

    题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1->1-> ...

  8. 力扣Leetcode 21. 合并两个有序链表

    合并两个有序链表 将两个升序链表合并为一个新的升序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1-> ...

  9. [LeetCode题解]21. 合并两个有序链表 | 递归

    解题思路 使用递归实现: 定义函数功能:合并两个有序链表,并返回链表的头 结束条件:两个链表其中一个为空,返回另一个链表 递推公式: l1.val < l2.val:l1.next = Merg ...

随机推荐

  1. Ethical Hacking - GAINING ACCESS(15)

    CLIENT SIDE ATTACKS Social Engineering Gather info about the user(s). Build a strategy based on the ...

  2. 设计模式:Adapter模式

    目的:复用代码和兼容以前的代码 思想:提供一个中间层,做兼容 方法:“继承”的方式,“委托”的方式 继承关系图: 委托方式 继承方式 例子: //原来的打印 class Print { public: ...

  3. 05 . ELK Stack+Redis日志收集平台

    环境清单 IP hostname 软件 配置要求 网络 备注 192.168.43.176 ES/数据存储 elasticsearch-7.2 内存2GB/硬盘40GB Nat,内网 192.168. ...

  4. mybatis sqlsession与sqlsquery、transaction、connection

    sqlsession和connection 一个sqlsession一般对应一个connection,并且mybatis默认每次获取session都会开启一个事务,且不自动提交事务.如果更新操作完成后 ...

  5. 01 . Git常用命令及方法和分支管理

    原理 # Workspace:工作区 # Index / Stage:暂存区 # Repository:仓库区(或本地仓库) # Remote:远程仓库 本地分支关联远程 git branch --s ...

  6. Spring Security 实战干货:理解AuthenticationManager

    1. 前言 我们上一篇介绍了UsernamePasswordAuthenticationFilter的工作流程,留下了一个小小的伏笔,作为一个Servlet Filter应该存在一个doFilter实 ...

  7. 曹工说Spring Boot源码(30)-- ConfigurationClassPostProcessor 实在太硬核了,为了了解它,我可能debug了快一天

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  8. DVWA SQL 注入关卡初探

    1. 判断回显 给id参数赋不同的值,发现有不同的返回信息 2. 判断参数类型 在参数后加 ' ,查看报错信息 数字型参数左右无引号,字符型参数左右有引号 4. 引号闭合与布尔类型判断 由于是字符型参 ...

  9. UDP 绑定信息

    """ 建立->绑定本地ip地址和端口号->接收数据->转码输出->关闭客户端 """ from socket im ...

  10. Python basestring() 函数

    描述 basestring() 方法是 str 和 unicode 的超类(父类),也是抽象类,每组词 www.cgewang.com 因此不能被调用和实例化,但可以被用来判断一个对象是否为 str ...