題目
原題鏈接
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
思路
有點愚 使用了4張hash表......效率不咋地
代碼
SWIFT解決方案:
TODO:之后添加
C 語言解決方案:
int lengthOfLongestSubstring(char* s) {
/// 構(gòu)建 hash 表來實現(xiàn)
int len = strlen(s);
/// 構(gòu)建四張hash表
int hash[4] = {0};
int maxLen = 0;
for(int j =0;j<len;j++){
char *ss = s;
int llen = 0;
for (int i = j;i < len; ++i)
{
int tableIndex = ss[i] / 32 ; // 應(yīng)該是0 1 2 3
int posIndex = ss[i] % 32 ; // 一張hash表中的位置0-31
if((hash[tableIndex] >> (posIndex) & 0x1) == 0){
// 說明不存在
hash[tableIndex] |= 1 << (posIndex);
llen++; //計數(shù)+1
}else{
break;//跳出里邊的循環(huán)
}
}
if(llen > maxLen){
maxLen = llen;
}
hash[0] = 0 ;//hash 表清零
hash[1] = 0 ;//hash 表清零
hash[2] = 0 ;//hash 表清零
hash[3] = 0 ;//hash 表清零
}
return maxLen;
}