题目链接

  题目要求:

  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的更多相关文章

  1. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

  2. LeetCode(205)Isomorphic Strings

    题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...

  3. 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 ...

  4. 【leetcode❤python】 205. Isomorphic Strings

    #-*- coding: UTF-8 -*- #转换法class Solution(object):    def isIsomorphic(self, s, t):        "&qu ...

  5. LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II

     1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...

  6. LeetCode之“散列表”:Single Number

    题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...

  7. LeetCode之“散列表”:Valid Sudoku

    题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...

  8. Leetcode 两数之和 (散列表)

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...

  9. [LeetCode] Isomorphic Strings

    Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...

随机推荐

  1. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  2. app控件唯一相对Xpath自动生成(增强版uiautomatorviewer)

    作者:cryanimal QQ:164166060 工具由来 前面的一篇博文较详细地介绍了uiautomatorviewer:扫描和分析Android应用程序的UI控件的工具. 熟悉控件获取的同学都知 ...

  3. Windows运行GitStats

    Windows运行GitStats(金庆的专栏)GitStats - git history statistics generatorhttp://gitstats.sourceforge.net/G ...

  4. SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别

                          SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别 RAM / ROM 存储器 ROM和RAM指的都是半导体存储器,R ...

  5. RxJava操作符(03-变换操作)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51649975 本文出自:[openXu的博客] 目录: Buffer FlatMap fla ...

  6. Java安全管理器——SecurityManager

    总的来说,Java安全应该包括两方面的内容,一是Java平台(即是Java运行环境)的安全性:二是Java语言开发的应用程序的安全性.由于我们不是Java本身语言的制定开发者,所以第一个安全性不需要我 ...

  7. Hadoop MapReduce工作原理

    在学习Hadoop,慢慢的从使用到原理,逐层的深入吧 第一部分:MapReduce工作原理   MapReduce 角色 •Client :作业提交发起者. •JobTracker: 初始化作业,分配 ...

  8. Android简易实战教程--第二十四话《画画板》

    今天完成一个画画板. 首先来个布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  9. 【NPR】非真实感渲染实验室

    写在前面 前几天在知乎看到一个问题--关于非实感图形学或者风格化渲染有哪些好的书或者paper,我刚好接触过一些就去里面回答了一下.答完以后突然想在Unity里搞一个这样的集锦,把一些简单的NPR论文 ...

  10. Runtime系列(一)-- 基础知识

    众所周知,Objective-C 是一种运行时语言.运行时怎么来体现的呢?比如一个对象的类型确定,或者对象的方法实现的绑定都是推迟到软件的运行时才能确定的.而运行时的诸多特性都是由Runtime 来实 ...