由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 大牛来做一下这道题
相关主题
leetcode-- scramble string有人帮我看看这个C++class的定义为什么是合法的吗?
请教一个C++问题C++ question
这小段code有什么问题吗?请教一个const和non const的C++问题
问个anagram的问题C/C++里数组作函数的参数的话应该怎么写?
砸了面试,发面题C++ 面试题
C++问题3问个C++ 编译器临时变量的问题
G题讨论从Simplify Path问面试编程语言选择?
(回忆了几道题)有人做过 select2perfrom 的test吗 ?Question:Given a array,find out if there exist a subarray such its sum is zero
相关话题的讨论汇总
话题: ret话题: string话题: return话题: repeated话题: sz1
进入JobHunting版参与讨论
1 (共1页)
a********r
发帖数: 218
1
给你两个string, 返回最早重复的那个字母的最小的index
要求优化时间复杂度(两个loop肯定不行)
abbbce
ddddab
return 0 (a, b repeated, a's index is 0 in first string)
=========================
a
b
return -1 (no repeated)
=============================
aaaaaxxxy
ccx
return 2 (x repeated, x's index is 2 in second string )
d****g
发帖数: 62
2
O(m+n) using hash table

【在 a********r 的大作中提到】
: 给你两个string, 返回最早重复的那个字母的最小的index
: 要求优化时间复杂度(两个loop肯定不行)
: abbbce
: ddddab
: return 0 (a, b repeated, a's index is 0 in first string)
: =========================
: a
: b
: return -1 (no repeated)
: =============================

a********r
发帖数: 218
3
The key is a, b, c, d, e ....? 但字母有重复,我试了
C++ map, unordered_map,都不允许有重复的key

【在 d****g 的大作中提到】
: O(m+n) using hash table
j*r
发帖数: 23
4
you only need to put them to has table once, i.e. when you see them the
first time.
d****g
发帖数: 62
5
enjoy...
int func(const string &s1, const string &s2)
{
const int sz1=s1.size(), sz2=s2.size();
if(sz1==0 || sz2==0)
return -1;
int m[26], ret=-1;
memset(m, -1, sizeof(m));
for(int i=0; i if(m[s1[i]-'a']<0)
m[s1[i]-'a']=i;
for(int i=0; ret!=0 && i if(m[s2[i]-'a']>=0 && (ret<0 || ret>min(i, m[s2[i]-'a'])))
ret=min(i, m[s2[i]-'a']);
return ret;
}

【在 a********r 的大作中提到】
: The key is a, b, c, d, e ....? 但字母有重复,我试了
: C++ map, unordered_map,都不允许有重复的key

1 (共1页)
进入JobHunting版参与讨论
相关主题
Question:Given a array,find out if there exist a subarray such its sum is zero砸了面试,发面题
问个C++语法的问题C++问题3
interleave string 的题目G题讨论
Unique Binary Search Trees的变形(回忆了几道题)有人做过 select2perfrom 的test吗 ?
leetcode-- scramble string有人帮我看看这个C++class的定义为什么是合法的吗?
请教一个C++问题C++ question
这小段code有什么问题吗?请教一个const和non const的C++问题
问个anagram的问题C/C++里数组作函数的参数的话应该怎么写?
相关话题的讨论汇总
话题: ret话题: string话题: return话题: repeated话题: sz1