【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], ...
随机推荐
- Xcode如何找到默认的生成路径?
我最近刚刚入门ObjectiveC,在研习<Objective C程序设计(第6版)>一书. 今天看到有关文件和归档的章节,但是我对XCode的生成文件路径并不了解,然后,在调试代码的时候 ...
- 第四天 rxcocoa
HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...
- expert C Programing notes
1.寻常算术转换 在运算中 如果其中一个操作数是long double 则另一个转为long double,其次 如果有一个为double 则另一个转为double,再次 float . unsign ...
- Java NIO与IO的区别和比较
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- 网络之AFNetsorking
AFNetsorking作为功能全面的网络第三方,既通俗好用又与时俱进-及时的更新使用了NSURLSession,不得不爱. AFNetsorking使用: 1,AFNetsorking GET请求 ...
- cvGet2D的用法
CvScalar s;s = cvGet2D(src, j,i);//获取src图像中坐标为(i,j)的像素点的值s.val[0] 代表src图像BGR中的B通道的值~int nXY = cvGet2 ...
- log4j 配置
给java项目添加log4j日志: 1.下载log4j jar包,放入lib目录, 导入项目中 2.创建log4j.properties 文件 目录 Src 3.在需要使用输出的类中使用 priva ...
- 39 网络相关函数(七)——live555源码阅读(四)网络
39 网络相关函数(七)——live555源码阅读(四)网络 39 网络相关函数(七)——live555源码阅读(四)网络 简介 14)readSocket从套接口读取数据 recv/recvfrom ...
- 9 DelayQueueEntry 延时队列节点类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- Linux 文件rwx权限问题 chmod 777 XXX 任何人拥有最高权限
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限.ls -l:得到-rw-r--r-- 1 apple users 2254 2006-05-20 13 ...