LeetCode之“散列表”:Isomorphic Strings
题目要求:
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.
Note:
You may assume both s and t have the same length.
这题难度不大。
第一个程序(28ms):
bool isIsomorphic(string s, string t) {
unordered_map<char, char> hashMap;
int sz = s.size();
for(int i = ; i < sz; i++)
{
if(hashMap.find(s[i]) != hashMap.end())
{
if(hashMap[s[i]] != t[i])
return false;
}
else
{
unordered_map<char, char>::iterator itr = hashMap.begin();
for(; itr != hashMap.end(); itr++)
if(itr->second == t[i])
return false; hashMap[s[i]] = t[i];
} } return true;
}
第二个程序(24ms):
bool isIsomorphic(string s, string t) {
unordered_map<char, char> hashMap;
unordered_map<char, char> rHashMap;
int sz = s.size();
for(int i = ; i < sz; i++)
{
if(hashMap.find(s[i]) != hashMap.end())
{
if(hashMap[s[i]] != t[i])
return false;
}
else
{
if(rHashMap.find(t[i]) != rHashMap.end())
return false; hashMap[s[i]] = t[i];
rHashMap[t[i]] = s[i];
}
} return true;
}
看了网上别人的做法(少掉了哈希表查找的时间损耗),只需8ms:
bool isIsomorphic(string s, string t) {
char map_s[] = { };
char map_t[] = { };
int len = s.size();
for (int i = ; i < len; ++i)
{
if (map_s[s[i]]!=map_t[t[i]]) return false;
map_s[s[i]] = i+;
map_t[t[i]] = i+;
} return true;
}
LeetCode之“散列表”:Isomorphic Strings的更多相关文章
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- 【leetcode❤python】 205. Isomorphic Strings
#-*- coding: UTF-8 -*- #转换法class Solution(object): def isIsomorphic(self, s, t): "&qu ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- LeetCode之“散列表”:Valid Sudoku
题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...
- Leetcode 两数之和 (散列表)
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
随机推荐
- post插件
分享牛系列,分享牛专栏,分享牛.在项目开发中,http请求方式是最常见的了.怎么模拟http请求呢?方法有很多种,可以使用httpclient直接模拟请求,也可以使用火狐post插件方式,这个章节主要 ...
- Java异常处理-----java异常体系
再三思考后还是决定贴图,csdn的格式,我是真玩不转,对不起了各位,继续将就吧. 错误原因:内存溢出.需要的内存已经超出了java虚拟机管理的内存范围. 错误原因:找不到类文件. 错误(Error): ...
- 源码篇——AsyncTask机制
AsyncTask new AsyncTask<String,String,String>(){ // 运行在主线程中,做预备工作 onPreExecute(){ } // 运行在子线程中 ...
- 探究java接口中的变量与方法
关于变量 java接口里的变量都是默认 pubic static final的 为啥? public 接口得能被所有对象调用 static 这个变量是属于接口本身,而不是实现了接口的对象的 具体来说 ...
- 阻塞IO服务器模型之单线程服务器模型
单线程服务器模型是最简单的一个服务器模型,几乎我们所有程序员在刚开始接触网络编程(不管是B/S结构还是C/S结构)都是从这个简单的模型开始.这种模型只提供同时一个客户端访问,多个客户端访问必须要等到前 ...
- 有奖试读—Windows PowerShell实战指南(第2版)
为什么要学PowerShell? Windows用户都已习惯于使用图形化界面去完成工作,因为GUI总能轻易地实现很多功能,并且不需要记住很多命令.使得短时间学会一种工具成为可能. 但是不幸的是,GUI ...
- 关于查找iOS中App路径时所要注意的一个问题
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- Servlet之Session处理
HttpSession 对象中可用的几个重要的方法: 1 public Object getAttribute(String name) 该方法返回在该 session 会话中具有指定名称的对象 ...
- (NO.00004)iOS实现打砖块游戏(十六):导弹发射道具的实现(下)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上一篇我们完成了导弹道具相关的道具制作,本篇中我们来完成其实现 ...