【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
秒小题好爽的说。
思路:保存两个字符串字母对应的字母,出现不一致就false
我的代码:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> map1, map2;
for(int i = ; i < s.size(); ++i)
{
if(map1.find(s[i]) == map1.end() && map2.find(t[i]) == map2.end())
{
map1[s[i]] = t[i]; map2[t[i]] = s[i];
}
else if(map1[s[i]] != t[i] || map2[t[i]] != s[i])
return false;
else;
}
return true;
}
网上C版本代码 免去了查找 更快
bool isIsomorphic(char* s, char* t) {
char mapST[] = { };
char mapTS[] = { };
size_t len = strlen(s);
for (int i = ; i < len; ++i)
{
if (mapST[s[i]] == && mapTS[t[i]] == )
{
mapST[s[i]] = t[i];
mapTS[t[i]] = s[i];
}
else
{
if (mapST[s[i]] != t[i] || mapTS[t[i]] != s[i])
return false;
}
}
return true;
}
【leetcode】Isomorphic Strings(easy)的更多相关文章
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
随机推荐
- python的re正则表达式模块学习
python中re模块的用法 Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
- 【开源一个小工具】一键将网页内容推送到Kindle
最近工作上稍微闲点,这一周利用下班时间写了一个小工具,其实功能挺简单但也小折腾了会. 工具名称:Simple Send to Kindle Github地址:https://github.com/zh ...
- Ruby注意事项
在Ruby中只有false和nil是'假', 其余都是真(0也是真)
- getField方法
getField方法是ThinkPHP中用来获取字段值的方法,区别于select和find方法,通常仅用于获取个别字段的值.但是事实上并没有那么简单,该方法的用法总结如下: 获取某个字段值这个是get ...
- Linux命令行修改IP、网关、DNS、主机名 的方法
修改主机名:[改里面的 HOSTNAME 即可] vim /etc/sysconfig/network 网卡eth0 IP修改为 102.168.0.1 ifconfig eth0 102.16 ...
- 使用PHP的五个小技巧
PHP的一些小技巧,比较基础,总结一下,老鸟换个姿势飘过去就是. 1. str_replace str_replace是非常常常常常用的php函数,用于字符串替换,经常看到某些php新人为了替换一批字 ...
- svn更改默认服务启动目录
配置文件位于 /etc/sysconfig/svnserve 修改为自己的目录
- iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...
- Html中各种空格的显示
一.使用全角空格 全角空格被解释为汉字,所以不会被被解释为HTML分隔符,可以按照实际的空格数显示. 二.使用空格的替代符号 替代符号就是在需要显示空格的地方加入替代符号,这些符号会被浏览器解释为空格 ...
- Java BigDecimal详解
借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供 较为精确的快速近 ...