题目链接

  题目要求:

  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. 1小时学会JQuery

    ---------------------------------------------------------------------------------------------------- ...

  2. ROS(indigo) 安装和使用更新版本的Gazebo----3,4,5,6,7 附:中国机器人大赛中型组仿真比赛说明

    ROS(indigo) 安装和使用更新版本的Gazebo,本文以7为例. Gazebo7支持更多新的功能,如果使用下面命令安装ROS(indigo): ~$ sudo apt-get install ...

  3. 一个环形公路,上面有N个站点,A1, ..., AN,其中Ai和Ai+1之间的距离为Di,AN和A1之间的距离为D0。 高效的求第i和第j个站点之间的距离,空间复杂度不超过O(N)。

    //点数 #define N 10 //点间距 int D[N]; //A1到每个Ai的距离 int A1ToX[N]; void preprocess() { srand(time(0)); //随 ...

  4. 为什么函数式编程可以没有while?

    以前想不通,今天在写代码时不知怎么的,偶然就发现了答案.. 比如说把某个字符串s中所有"00"及更长的'00'统统换为'0'.最后结果中不能包含'00'. 00001100--&g ...

  5. ASCII 大文字生成器

    display text in large ASCII art fonts 显示大ASCII艺术字体 这种东西在源码声明或者软件初始化控制台打印时候很有用. 例如打开: http://www.oran ...

  6. 21 viewPager--- hzScrollView ----llContainer

    结构: MainActivity.java package com.qf.day21_hsviewpagerfragment_demo5; import java.util.ArrayList; im ...

  7. not in 前面/后面存在null值时的处理

    表声明 order_header表中有ship_method列: ship_method_map表中ship_method为主键列. 需求 找出order_header表中所有ship_method不 ...

  8. 在ubuntu上部署一个samba服务器

    今天公司装了一天新电脑,准备把它装成服务器,于是为了方便开发的使用,我在上面部署了一个samba,用来实现window和linux的联系: 具体步骤,我详细的查看了百度,高手云集,以下就是总结了网友的 ...

  9. Mybatis源码分析之缓存

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  10. 【移动开发】自定义ProgressBar

    <ProgressBar android:layout_centerInParent="true" android:layout_width="30dp" ...