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):

 1 bool isIsomorphic(string s, string t) {
 2     unordered_map<char, char> hashMap;
 3     int sz = s.size();
 4     for(int i = 0; i < sz; i++)
 5     {
 6         if(hashMap.find(s[i]) != hashMap.end())
 7         {
 8             if(hashMap[s[i]] != t[i])
 9                 return false;
10         }
11         else
12         {
13             unordered_map<char, char>::iterator itr = hashMap.begin();
14             for(; itr != hashMap.end(); itr++)
15                 if(itr->second == t[i])
16                     return false;
17
18             hashMap[s[i]] = t[i];
19         }
20
21     }
22
23     return true;
24 }

  第二个程序(24ms):

 1 bool isIsomorphic(string s, string t) {
 2     unordered_map<char, char> hashMap;
 3     unordered_map<char, char> rHashMap;
 4     int sz = s.size();
 5     for(int i = 0; i < sz; i++)
 6     {
 7         if(hashMap.find(s[i]) != hashMap.end())
 8         {
 9             if(hashMap[s[i]] != t[i])
10                 return false;
11         }
12         else
13         {
14             if(rHashMap.find(t[i]) != rHashMap.end())
15                 return false;
16
17             hashMap[s[i]] = t[i];
18             rHashMap[t[i]] = s[i];
19         }
20     }
21
22     return true;
23 }

  看了网上别人的做法(少掉了哈希表查找的时间损耗),只需8ms

 1 bool isIsomorphic(string s, string t) {
 2     char map_s[128] = { 0 };
 3     char map_t[128] = { 0 };
 4     int len = s.size();
 5     for (int i = 0; i < len; ++i)
 6     {
 7         if (map_s[s[i]]!=map_t[t[i]]) return false;
 8         map_s[s[i]] = i+1;
 9         map_t[t[i]] = i+1;
10     }
11
12     return true;
13 }
时间: 2024-10-27 03:44:42

LeetCode之“散列表”:Isomorphic Strings的相关文章

LeetCode之“散列表”:Contains Duplicate &amp;&amp; Contains Duplicate II

 1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 代码如下:

LeetCode之“散列表”:Valid Sudoku

题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board

LeetCode之“散列表”:Single Number

题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 这道题属于比较简单的,程序如下: 1 class Solutio

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

散列表(Hash Map)

今天第一次做Leetcode用到了散列表,之前学的数据结构的内容都忘了,正好趁热打铁补一补. 摘自其他博客的一个整合. 一.哈希表简介 数据结构的物理存储结构只有两种:顺序存储结构和链式存储结构(像栈,队列,树,图等是从逻辑结构去抽象的,映射到内存中,也这两种物理组织形式),在数组中根据下标查找某个元素,一次定位就可以达到,哈希表利用了这种特性,哈希表的主干就是数组. 比如我们要新增或查找某个元素,我们通过把当前元素的关键字 通过某个函数映射到数组中的某个位置,通过数组下标一次定位就可完成操作.

闭散列表的查找、插入和删除操作的完整C代码

/*闭散列表的建立.查找.插入.删除*/ #include <stdio.h> #define NIL -1 //假设关键字为非负整数 #define DEL -2 typedef int KeyType; KeyType HashTable[13]; //便于验证算法,关键字个数假定为不超过13,哈希表长定为13 //关键字插入函数 void InsertHashTable(KeyType k) { for(int i=0; i<13; i++) if( NIL == HashTabl

哈希表/散列表

哈希表/散列表,是根据关键字(key)直接访问在内存存储位置的数据结构. 构造哈希表的常用方法: 直接地址法---取关键字的某个线性函数为散列地址,Hash(Key) = Key或Hash(key) = A*Key + B, A,B为常数. 除留余数法---取关键值被某个不大于散列表长m的数p除后的所得的余数为散列地址. Hash(key) = key % p. 若采用直接地址法(Hash(Key) = Key)存在一定的缺陷. 当Key值特别大时,而Key之前的数很少,就会造成空间浪费.大多时