[leetcode]Isomorphic Strings 解题报告 C语言

【题目】

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.

【题目分析】

这道题就很像我们所学的映射,需要一一对应,一个字符只能映射到一个字符,二个不同的字符不能映射到同一个字符上,也就是说,对于已经映射了的某个字符,只需要判断映像对不对,而还没有映射的就需要判断会不会重叠(即两个不同的字符映射到一个字符上)。

【具体代码如下】

bool isIsomorphic(char* s, char* t) {
    int hash[255]={0};
    int index;
    int i;
    int j;
    for(i=0;s[i]!=‘\0‘;i++)
    {
        index=s[i];
        if(hash[index]==0)
        {
          for(j=0;j<255;j++)
          {
            if(hash[j]==t[i])
            return false;
          }
            hash[index]=t[i];
        }
        if(hash[index]!=0)
        {
            if(hash[index]!=t[i])
            return false;
        }
    }
    if(t[i]!=‘\0‘)return false;
    return true;

}

【个人总结】

第8行的赋值是可以的哦,char虽然是字符类型,但是在技术实现上确实整型,因为char类型实际存储的是整数而不是字符,为了处理字符,计算机使用的是一种数字编码,一般是ASCII码。这也是为什么hash表的大小是255的原因。

将s中的字符作为下标,t中的字符作为hash数组中的存值,实现映射。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-28 20:36:27

[leetcode]Isomorphic Strings 解题报告 C语言的相关文章

LeetCode: Multiply Strings 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

[leetcode]Ugly Number 解题报告 C语言

[题目] Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] 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 '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

[leetcode]Count Primes 解题报告 C语言

[题目] Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. [题目分析] 这道题常用的判断一个数是否为质数是行不通的,根据hint,采用Sieve of Eratosthenes算法实现,具体关于该算法详见https://en.w

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【LeetCode】205. 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 character