面试面试经典 150 题 - 哈希
class Solution {
public :
bool canConstruct ( string ransomNote, string magazine) {
int hash[ 26 ] = { 0 } ;
for ( auto & ch : magazine) {
hash[ ch - 'a' ] ++ ;
}
for ( auto & ch : ransomNote) {
if ( -- hash[ ch - 'a' ] < 0 ) {
return false ;
}
}
return true ;
}
} ;
错误写法
class Solution {
public :
bool isIsomorphic ( string s, string t) {
if ( s. size ( ) != t. size ( ) ) return false ;
int hash_s[ 26 ] = { 0 } , hash_t[ 26 ] = { 0 } ;
for ( int i = 0 ; i < s. size ( ) ; ++ i) {
if ( ++ hash_s[ s[ i] - 'a' ] != ++ hash_t[ t[ i] - 'a' ] ) {
return false ;
}
}
return true ;
}
} ;
双向映射 - 两个大小为128 的array
class Solution {
public :
bool isIsomorphic ( string s, string t) {
if ( s. size ( ) != t. size ( ) ) return false ;
array< char , 128 > s2t = { 0 } ;
array< char , 128 > t2s = { 0 } ;
for ( int i = 0 ; i < s. size ( ) ; ++ i) {
char s_char = s[ i] ;
char t_char = t[ i] ;
if ( s2t[ s_char] == 0 && t2s[ t_char] == 0 ) {
s2t[ s_char] = t_char;
t2s[ t_char] = s_char;
} else if ( s2t[ s_char] != t_char || t2s[ t_char] != s_char) {
return false ;
}
}
return true ;
}
} ;
class Solution {
public :
bool wordPattern ( string pattern, string s) {
unordered_map< char , string> p2s;
unordered_map< string, char > s2p;
size_t start = 0 ;
for ( size_t i = 0 ; i < pattern. size ( ) ; ++ i) {
if ( start >= s. size ( ) ) return false ;
size_t j = start;
while ( j < s. size ( ) && s[ j] != ' ' ) {
++ j;
}
char & ch = pattern[ i] ;
string str = s. substr ( start, j - start) ;
start = j + 1 ;
if ( p2s. find ( ch) != p2s. end ( ) && p2s[ ch] != str) {
return false ;
}
if ( s2p. find ( str) != s2p. end ( ) && s2p[ str] != ch) {
return false ;
}
p2s[ ch] = str;
s2p[ str] = ch;
}
if ( start < s. size ( ) ) return false ;
return true ;
}
} ;
class Solution {
public :
bool isAnagram ( string s, string t) {
if ( s. size ( ) != t. size ( ) ) return false ;
array< int , 26 > hash;
for ( auto & ch : s) {
hash[ ch - 'a' ] ++ ;
}
for ( auto & ch : t) {
if ( -- hash[ ch - 'a' ] < 0 ) {
return false ;
}
}
return true ;
}
} ;
将排序后的字母作为哈希索引
将字母出现频次拼成字符串作为索引
1. 两数之和 - 哈希表存储 {val, index}
class Solution {
public :
vector< int > twoSum ( vector< int > & nums, int target) {
unordered_map< int , int > map;
int n = nums. size ( ) ;
for ( int i = 0 ; i < n; ++ i) {
if ( map. find ( target - nums[ i] ) != map. end ( ) ) {
return { i, map[ target - nums[ i] ] } ;
}
map[ nums[ i] ] = i;
}
return { - 1 , - 1 } ;
}
} ;
哈希表存储循环过程
class Solution {
public :
int transfer ( int n) {
int sum = 0 ;
while ( n) {
sum += ( n % 10 ) * ( n % 10 ) ;
n /= 10 ;
}
return sum;
}
bool isHappy ( int n) {
unordered_set< int > st;
while ( n != 1 ) {
if ( st. find ( n) != st. end ( ) ) {
return false ;
}
st. insert ( n) ;
n = transfer ( n) ;
}
return true ;
}
} ;
快慢指针
class Solution {
public :
int transfer ( int n) {
int sum = 0 ;
while ( n) {
sum += ( n % 10 ) * ( n % 10 ) ;
n /= 10 ;
}
return sum;
}
bool isHappy ( int n) {
int slow = n, fast = transfer ( n) ;
while ( fast != 1 && slow != fast) {
slow = transfer ( slow) ;
fast = transfer ( transfer ( fast) ) ;
}
return fast == 1 ;
}
} ;