由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 从Simplify Path问面试编程语言选择?
相关主题
G题,F题,leetcode题大牛来做一下这道题
C++ 面试题问一个精华区里的题目
贴一个OJ 的 longest valid parenthesisleetcode上wild match
G家电面,这回肯定挂了。附面经。做了一下scramble string
请教一道leetcode的online judge题被thank you的fb电面面经
请教一个C++问题Interview question from Yahoo
问个C++ 编译器临时变量的问题一个coding题目
一道leetcode变种,twitter常考,怎么做?菜鸟求救 请大家看看我的代码有没有问题
相关话题的讨论汇总
话题: string话题: ret话题: else话题: int话题: npos
进入JobHunting版参与讨论
1 (共1页)
a***e
发帖数: 413
1
leetcode上的,感觉如果选择的语言有split这个function,就会好写很多,比如C#和
Java。
C++就麻烦些。而且选择是algorithm 的find还是string的find也很不同。
面试时碰到这类题能说我这道题用C#或者Java,其他用C++行么?其实现在主要用C#,
但C++是总共用得最久的。Java感觉和C#很像,但实际工作中没用过,也不想为面试而
学。多谢
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
两个C++的
1. string simplifyPath(string path) {
vector a;
int n = path.size();
for (int i = 0; i {
string t;
size_t j = path.find('/', i); //用string的 find
if (j != string::npos)
t = path.substr(i, j - i);
else
{
t = path.substr(i, n - i);
}
if (t == "." || t == "")
{
if (j != string::npos)
{
i = j + 1;
continue;
}
else
break;

}
if (t == "..")
{
if (!a.empty())
a.pop_back();
else
{
if (j != string::npos)
{
i = j + 1;
continue;
}
else
break;
}
}
else
{
a.push_back(t);
t.clear();
}
if (j != string::npos)
i = j + 1;
else
break;
}
int m = a.size();
string ret;
if (m == 0)
ret = "/";
else
{
for (int i = 0; i {
ret += "/";
ret += a[i];
}
}
return ret;

}
2. string simplifyPath(string path) {
vector a;
for (auto i = path.begin(); i != path.end();) {
++i;
auto j = find(i, path.end(), '/');
auto dir = string(i, j);
if (!dir.empty() && dir != ".") {//
if (dir == "..") {
if (!a.empty())
a.pop_back();
} else
a.push_back(dir);
}
i = j;
}
int m = a.size();
string ret;
if (m == 0)
ret = "/";
else
{
for (int i = 0; i {
ret += "/";
ret += a[i];
}
}
return ret;

}
a***e
发帖数: 413
2
?
a***e
发帖数: 413
3
另外那道Length of Last Word
觉得没见过的话,现场15分钟要写出如下compact的code还是比较
int lengthOfLastWord(const char *s) {
int len = 0;
while (*s) {
if (*s++ != ' ')
{
len++;
}
else if (*s && *s != ' ') //觉得这个条件比较难懂,后来debug了才
知道怎么回事
len = 0;
}
return len;
}
我自己写的
int lengthOfLastWord(const char *s) {
int len=0;
while(*s!='\0')
{
if (*s==' '&&isalpha(*(s+1)))
{
len=0;
}
else if (isalpha(*s))
{
len++;
}
s++;
}

return len;
}
q********c
发帖数: 1774
4
这是我的code, 更简单些:
int lengthOfLastWord(const char *s) {
int len = strlen(s), wordLen = 0;
const char *p = s+len-1;

while(isspace(*p) && p>=s)p--;
while(isalpha(*p) && p>=s) {
p--;
wordLen++;
}
return wordLen;
}

【在 a***e 的大作中提到】
: 另外那道Length of Last Word
: 觉得没见过的话,现场15分钟要写出如下compact的code还是比较
: int lengthOfLastWord(const char *s) {
: int len = 0;
: while (*s) {
: if (*s++ != ' ')
: {
: len++;
: }
: else if (*s && *s != ' ') //觉得这个条件比较难懂,后来debug了才

h*******e
发帖数: 1377
5
恩, 知识点++啦, 应该反着来
a***e
发帖数: 413
6
嗯,根据从后面找这个思路又写了一遍,虽然不简洁,但是自己写的,很快写出来能通
过,还是感到了开心
int lengthOfLastWord(const char *s) {
int len=strlen(s);

int i=len-1;
int ret=0;
bool flagWord = false;

while(i>=0)
{
if (*(s+i)!=' ')
{
ret++;
flagWord=true;
}
else if (flagWord)
break;

i--;
}

return ret;
}

【在 q********c 的大作中提到】
: 这是我的code, 更简单些:
: int lengthOfLastWord(const char *s) {
: int len = strlen(s), wordLen = 0;
: const char *p = s+len-1;
:
: while(isspace(*p) && p>=s)p--;
: while(isalpha(*p) && p>=s) {
: p--;
: wordLen++;
: }

s**x
发帖数: 7506
7
选择自己最熟悉的语言,不存在的函数可以先假设存在,然后补写。c 也有tokenizer
不过用起来比较麻烦。要清楚对方问的要点。
a***e
发帖数: 413
8
多谢!看来交流也很重要。以前面试经常一听到题目就开写,生怕时间不够,写不完。
这次准备才发现好的思路能让代码短好多。想好了再写更快。
以后要好好改改。
1 (共1页)
进入JobHunting版参与讨论
相关主题
菜鸟求救 请大家看看我的代码有没有问题请教一道leetcode的online judge题
C++ Q51: string and c-string (C19)请教一个C++问题
A challenging interview question: revert a string问个C++ 编译器临时变量的问题
C++ Q83: 这个const_cast什么意思?一道leetcode变种,twitter常考,怎么做?
G题,F题,leetcode题大牛来做一下这道题
C++ 面试题问一个精华区里的题目
贴一个OJ 的 longest valid parenthesisleetcode上wild match
G家电面,这回肯定挂了。附面经。做了一下scramble string
相关话题的讨论汇总
话题: string话题: ret话题: else话题: int话题: npos