C#LeetCode刷题之#389-找不同(Find the Difference)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
输入:
s = "abcd"
t = "abcde"输出:
e
解释:'e' 是那个被添加的字母。
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Input:
s = "abcd"
t = "abcde"Output:
e
Explanation:'e' is the letter that was added.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。
public class Program {
public static void Main(string[] args) {
var s = "ecb";
var t = "beca";
var res = FindTheDifference(s, t);
Console.WriteLine(res);
s = "loveleetcode";
t = "loveleetxcode";
res = FindTheDifference2(s, t);
Console.WriteLine(res);
Console.ReadKey();
}
private static char FindTheDifference(string s, string t) {
var cs = s.ToArray();
Array.Sort(cs);
var ct = t.ToArray();
Array.Sort(ct);
var i = 0;
for(; i < cs.Length; i++) {
if(cs[i] != ct[i]) return ct[i];
}
return ct[i];
}
private static char FindTheDifference2(string s, string t) {
var dic = new Dictionary<char, int>();
foreach(var c in s) {
if(dic.ContainsKey(c)) {
dic[c]++;
} else {
dic[c] = 1;
}
}
foreach(var c in t) {
if(dic.ContainsKey(c)) {
dic[c]--;
if(dic[c] < 0) return c;
} else {
return c;
}
}
return ' ';
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4062 访问。
a
x
分析:
FindTheDifference 的时间复杂度基于排序所使用的排序算法,FindTheDifference2 的时间复杂度为: 。
C#LeetCode刷题之#389-找不同(Find the Difference)的更多相关文章
- C#LeetCode刷题之#744-寻找比目标字母大的最小字母(Find Smallest Letter Greater Than Target)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4001 访问. 给定一个只包含小写字母的有序数组letters 和 ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
随机推荐
- 读取文件夹内容解析为Tree结构
package com.mine.io; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import ...
- 定时器三----js定时器
方法一: var t; //初始化定时器 $(function(){ init_fun_timer1(); }); ...
- 紧急处理RAC环境有一个监听down 的情况
初步处理 1. grid 登录查看是监听是否down掉 srvctl status listener -n node1 或者oracle登录 lsnrctl status 查看 如果掉了 grid 用 ...
- ajax原生js封装
不带注释的 function ajax(json) { json.type = json.type ? json.type : 'get'; json.async = json.async == fa ...
- vue : 在vuex里写一个数组首尾元素互换的方法
不着急上代码,先想几个问题. vuex里怎么写方法? mutation里写vuex方法,组件中用commit调用. 数组首尾元素怎么互换? arr.splice(0, 0, arr[arr.lengt ...
- 题解 洛谷 P2086 【[NOI2012]魔幻棋盘】
先考虑只有一维的情况,要求支持区间加和求区间 \(\gcd\),根据 \(\gcd\) 的性质,发现: \[ \gcd(a_1,a_2,a_3,\ldots a_n)=\gcd(a_i,a_2-a_1 ...
- vue中v-for
在vue中我们只要操作数据,就可以渲染和更新数据,这背后的boss就是diff算法 vue和react的虚拟DOM的Diff算法大致相同,其核心是基于两个简单的假设: 1. 俩个相同组件产生类似DOM ...
- vue学习(五) 访问vue内部元素或者方法
//html <div id="app"> <input type="button" value="ok" v-bind: ...
- BUUCTF-Web Comment
dirsearch扫出/.git/目录 遂用航神写的Githacker脚本 https://github.com/wangyihang/githacker 出来的源码并不完整,使用git log ...
- vue -电子时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...