由买买提看人间百态

topics

全部话题 - 话题: strlen
1 2 3 4 5 6 下页 末页 (共6页)
i**p
发帖数: 902
1
来自主题: Programming版 - VC++ does not support strlen()
http://www.acceleratedcpp.com/details/msbugs.html
Chapter 12
Similar to the problems in Chapters 6 and 8, where VC++ 6.0 fails to include
the character classification functions (isalpha, isalnum, isspace etc.) as
part of the std namespace, it also fails to include the strlen function in
std. The workaround is analogous: Omit the qualification of std::strlen.
For example, in the Str constructor that takes a const char*:
#ifdef _MSC_VER
std::copy(cp, cp + strlen(cp), std::back_inserter(data));
#el
p****e
发帖数: 32
2
来自主题: Programming版 - strlen怎么实现的
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)strlen.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include
#include
size_t
strlen(str)
const char *str;
{
register const char *s;
for (s = str; *s; ++s);
return(s - str);
}
f*****y
发帖数: 444
3
checked the strlen source code, it doesn't check the NULL pointer, why?
s*******y
发帖数: 558
4
来自主题: Programming版 - strlen怎么实现的
请问大家知道strlen这个函数怎么实现的
如果传入的const char* 不是以\0结尾的话, 怎么计算出length呢?
谢谢
n********y
发帖数: 66
5
欢迎拍砖,就用了一个 256 字节的table来检查一个数字是否出现过
#include
#include
#include
#include
char* nextpointer(char* str, int strlen)
{
char *p = str;
char *pend = str + strlen;
char count[256];
memset(count, 0, sizeof(count));
while (p < pend)
{
++count[*p];
if (count[*p] > 1)
{
return p;
}
++p;
}
return p;
}
void longestsingle(char* str, int strlen)
{
char *pstart = str;
char *pend... 阅读全帖
i***h
发帖数: 12655
6
用C++ STL, 还好了
下面的代码少了最后一步, 也没有sanity check
但也不难
当然效率不是最好的
#include
#include
#include
#include
using namespace std;
bool
compstr(char* a, char* b)
{
return strcmp(a,b)<0;
}
void
suffixArray(char *a, char *b)
{
char *m = (a>b)? a-1 : b-1;
char* suffix[strlen(a)+strlen(b)];
for(int i = 0; i suffix[i] = a+i;
}
for(int i=0; i suffix[strlen(a)+i] = b+i;
}
sort(suffix, suffix+strlen(a)+strlen(b), comp... 阅读全帖
g*********s
发帖数: 1782
7
来自主题: Programming版 - which str_cmp code is better?
so this is good?
#ifndef _HAVE_STRING_ARCH_strcmp
# if __GNUC_PREREQ (3, 2)
# define strcmp(s1, s2) \
__extension__
\
({ size_t __s1_len, __s2_len;
\
(__builtin_constant_p (s1) && __builtin_constant_p (s2)
\
&& (__s1_len = strlen (s1), __s2_len = strlen (s2),
\
(!__string2_1bptr_p (s1) || __s1_len >= 4)
\
&& (!__string2_1bptr_p (s2) || __s2_len >= 4)) ... 阅读全帖
w********s
发帖数: 1570
8
#include "stdafx.h"
#include
#include
#include
#include
typedef std::map> MyMap;
MyMap m;
int repeat2(char* str)
{
for (int i = 0; i < strlen(str); ++i)
{
char c = str[i];
if (m.find(c) == m.end())
{
std::vector v;
v.push_back(i);
m[c] = v;
}
else
{
m[c].push_back(i);
}
}
int* count = new int[strlen(str)];
mems... 阅读全帖
w********s
发帖数: 1570
9
#include "stdafx.h"
#include
#include
#include
#include
typedef std::map> MyMap;
MyMap m;
int repeat2(char* str)
{
for (int i = 0; i < strlen(str); ++i)
{
char c = str[i];
if (m.find(c) == m.end())
{
std::vector v;
v.push_back(i);
m[c] = v;
}
else
{
m[c].push_back(i);
}
}
int* count = new int[strlen(str)];
mems... 阅读全帖
p****n
发帖数: 148
10
来自主题: JobHunting版 - 刚做了一道题挺有意思
C版

/**************************/
int match (const char *s1, const char *s2, const int len) {
int r = 0;
for (int i = 0; i < len; i++)
if (*(s1+i) == *(s2+i))
r++;
return r;
}
size_t edit (const char *s, const char *u, int *begin, int *end) {
size_t maxmatch = 0;
for (int i = 0, j = strlen(u)-1; i < strlen(s); (j > 0)?j--:i++) {
int len = min(strlen(u) - j, strlen(s) - i);
int m = match(s+i, u+j, len);
if (m > maxmatch) {
... 阅读全帖
l*********8
发帖数: 4642
11
来自主题: JobHunting版 - leetcode上wild match
I modified the program.
Now the major function should be simple enough (although the whole
program is not that short).
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if(!s || !p )
return false;
const char * nextStar = strchr(p, '*');
if ( !nextStar)
return strCmp(s, p);
if (!strCmpN(s, p, nextStar-p) )
return false;
do {
s += nextStar-p;
p = nextStar + 1;
n... 阅读全帖

发帖数: 1
12
来自主题: JobHunting版 - 放c code求老师傅指教
update:
谢谢板上各位老师傅,现在我知道是我的问题了,“without using a second string
”我没有理解好,感谢大家赐教,我继续努力!
刚刚解决身份问题,男,找工作和leetcode都有一段时间了,最近碰到这家公司的一个
面试
http://stackoverflow.com/jobs/115265/software-engineer-networking-schweitzer-engineering?searchTerm=SEL&offset=3&sort=s
问的问题和glassdoor一样,所以我也准备了一下那个unions的答案和例子,以前没用
这个:
Interview Questions
1) What is in the software requirements?
2) What is mutex and semaphore?
3) When to use unions?
4) What are the pros and cons of using assembly in embedded systems?
5) Programming t... 阅读全帖
t*********l
发帖数: 30
13
那个ptr+strlen(b) 是不是应改成 ptr+strlen(a) ?
a+strlen(b) 应改为b+strlen(b), 因为前面你用
strstr(b,a)搜索b 中有没有 a. 另外如果改为
if (*(ptr+strlen(a)) == '\0'), 哈哈, 只有
两个函数调用, 好:)))
s*****s
发帖数: 157
14
来自主题: JobHunting版 - 继续攒人品 报几家面经
ip address那个, 用strtok好像容易些, 下面的code能否完成:
int foo(char *IP)
{
if(strlen(IP) > 16)
return false;
if(strlen(IP) < 8)
return false;
for (int i = 0 ; i < strlen(IP); i++)
{
if (!(IP[i] >= '0' && IP[i] <= '9' || IP[i] == '.'))
return false;
}
char * range = strtok(IP, ".");
short c = 0;
while ((range != NULL) && (c < 4))
{
if((strlen(range) <= 3) && (atoi(range) >= 0 &&(atoi(range) <= 255))
++c;
else
break;

range... 阅读全帖
w*******s
发帖数: 96
15
Two implementation. one use sort and one use hash.
//pre: str is sorted.
void PermutationWithDuplicate(char *str, int position)
{
if (position == strlen(str)) {
printf("%s", str);
return;
}

char lastChar = '\0';
for (int i = position; i {
//skip those character which is duplicated. Since the string is
sorted, it's easy.
if (lastChar == str[i] ) continue;

lastChar = str[i];
swap(str[po... 阅读全帖
c**********e
发帖数: 2007
16
这个Strategy design pattern的例子为什么人为得弄得这么复杂?
#include
#include
#include
using namespace std;
class Strategy;
class TestBed
{
public:
enum StrategyType
{
Dummy, Left, Right, Center
};
TestBed()
{
strategy_ = NULL;
}
void setStrategy(int type, int width);
void doIt();
private:
Strategy *strategy_;
};
class Strategy
{
public:
Strategy(int width): width_(width){}
void format()
{
char line[80], wo... 阅读全帖
l*********8
发帖数: 4642
17
来自主题: JobHunting版 - leetcode上wild match
贴一下我的程序,通过了leetcode judge.
感觉还是有些繁琐.
class Solution {
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!s || !p)
return false;
if(p[0] == '\0' && s[0] != '\0')
return false;
const char * star = NULL;
const char * nextStar = NULL;
if (p[0] == '*') {
star = p;
p++;
}
while (1) {
n... 阅读全帖
w****x
发帖数: 2483
18
来自主题: JobHunting版 - 做了一下scramble string
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 !... 阅读全帖
w****x
发帖数: 2483
19
来自主题: JobHunting版 - google scramble string O(n) 解法
贴一个递归和DP的:
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
... 阅读全帖
w****x
发帖数: 2483
20
来自主题: JobHunting版 - LeetCode Scramble String 疑问
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 !... 阅读全帖
w****x
发帖数: 2483
21
发现这道题属于没见过肯定想不出来,知道怎么解也很难写对。昨天重新写了一下,觉
得学到蛮多的,分享一下。
以前写过一个很挫的DP版本:
int GetEditDist(const char* str1, const char* str2)
{
assert(str1 && str2);
int nLen1 = strlen(str1);
int nLen2 = strlen(str2);
int* rec = new int[nLen2];
bool bFound = false;
for (int i = 0; i < nLen2; i++)
{
if (str2[i] == str1[0])
bFound = true;
rec[i] = bFound ? i : i+1;
}
bFound = (str2[0] == str1[0]); //(str2[0] == str1[0]) not false
for (int i = 1; i < nLe... 阅读全帖
f*******w
发帖数: 1243
22
来自主题: JobHunting版 - 问大牛们一个Leetcode上的题
Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same
length. Find all starting indices of substring(s) in S that is a
concatenation of each word in L exactly once and without any intervening
characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
我的code如下
之前用unordered_set, 如果L里面有重复元素就跑不过
现在改用multiset, 直接出错……
求助求助……
class Solution {
public:
vector findSubstring(string ... 阅读全帖
f*******w
发帖数: 1243
23
来自主题: JobHunting版 - 问大牛们一个Leetcode上的题
现在能过小case了,大的跑不过去……
class Solution {
public:
vector findSubstring(string S, vector &L) {
unordered_map strset, tmpset;
int numstr = L.size(), strlen = L[0].size();
vector ret;
for(int i = 0; i < numstr; ++i) {
auto it = strset.find(L[i]);
if(it != strset.end()) ++it->second;
else strset.insert(make_pair(L[i], 1));
}
tmpset = strset;
for(int i = 0; i < (int)S.size() - num... 阅读全帖
f*******w
发帖数: 1243
24
来自主题: JobHunting版 - 问大牛们一个Leetcode上的题
string sub = "";
for(int k = 0; k < strlen; ++k)
sub += S[i + j*strlen + k];
改成 string sub = S.substr(i+j*strlen, strlen) 就过large judge了,不过要
1600ms……显然不是什么好解法- -
x********i
发帖数: 92
25
来自主题: JobHunting版 - G家电面题
感觉对y的处理比较复杂. 我纯新手...写了一个代码, 望斧正
然后对于大小写的处理, 我就是创建一个新数组, 然后把所有的都转换成小写字母.
我运行了题目里给出的所有字符串, 结果都是正确的, 然后test了一个空字符串, 返回
为0. 求问还需要做什么样的test啊? 感觉很多代码写出来自己都不知道对不对, 因为
没有完善的test方案, 求指点...新手跪谢了
#define VOWELY 0
#define CONSOY 1
int vowelProduct(const char* newstr){
int length = strlen(newstr);
const char* consolant = "bcdfghjklmnpqrstvwxz";
const char* vowels = "aeiou";
int i;
int yFlag;
int score=0;
char* current;
char* yTest;
int point[26] = {0};
int tempScore=0;... 阅读全帖
a********n
发帖数: 1287
26
用suffix array
char* LongestCommonSubStr( char* a, char* b )
{
if( !a || !b )
{
return NULL;
}
int sizea = strlen( a );
int sizeb = strlen( b );
char** suffix = new char*[sizea + sizeb];
int suffixIdx = 0;
for( int i = 0; i < sizea; i++ )
{
suffix[suffixIdx++] = a + i;
}
for( int i = 0; i < sizeb; i++ )
{
suffix[suffixIdx++] = b + i;
}
std::sort( suffix, suffix + sizea + sizeb, Comp() );
int maxLen = 0;
char* ... 阅读全帖
k**********i
发帖数: 36
27
►►►Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch... 阅读全帖
D*****r
发帖数: 6791
28
来自主题: Joke版 - Evolution of a programmer
http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
High School/Jr.High
10 PRINT "HELLO WORLD"
20 END
First year in College
program Hello(input, output)
begin
writeln('Hello World')
end.
Senior year in College
(defun hello
(print
(cons 'Hello (list 'World))))
New professional
#include
void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}
... 阅读全帖
d*******y
发帖数: 75
29
void permutation(char* part1, char* part2)
{
int i, j = 0, k = 0;
int size = 0;
if ( strlen(part2) == 1 ) {
printf("%s%s\n", part1, part2);
} else {
char tmppart1[4] = {0};
char tmppart2[4] = {0};
for ( i = 0; i < strlen(part2); i++ )
{
j = 0;
k = 0;
strcpy(tmppart1, part1);
tmppart1[3-strlen(part2)] = part2[i];
while( j < strlen(part2) )
{
p**********g
发帖数: 9558
30
来自主题: Programming版 - 算法问题求教:字符串比较
假设字符串没有重复的字符
compare[a...b] = {0, 0, ...}
if(strlen(input1)!=strlen(input2)) return false;
for(p=input1;p compare[*p]=1;
for(p=input2;p if(!compare[*p])
return false;
return true
s*******e
发帖数: 93
31
来自主题: JobHunting版 - 这题谁知道答案?
我也写了一个code. 有测试ihasleetcode的所有test case答案都一样。
如果没有想错的话应该是O(n)
因为begin和end两个值都最多扫过每一个字母一次
假设都是ascii字符
typedef struct range
{
int begin;
int end;
} Range;
Range shortestSubstring(const char* str, int strLen, const char* characters,
int charCount)
{
int* needToFind=new int[256];
int* hasFound=new int[256];

for(int i=0;i<256;i++)
{
needToFind[i]=0;
hasFound[i]=0;
}

for(int i=0;i {
int index=(int)characters[i];
... 阅读全帖
l*****g
发帖数: 685
32
来自主题: JobHunting版 - aababccbc remove abc
用C++写了一个版本:
void RecursivelyRemoveSubString(char *str, const char *sub)
{
if (!str || !sub || *sub == 0 || *str == 0 || strlen(str) < strlen(sub))
return;
char *end = str;
char *cur = str;
int subLen = strlen(sub);
char subEnd = sub[subLen - 1];

while (*cur != 0)
{
*end = *cur;
if (*end == subEnd)
{
char *reCur = end;
int subIndex = subLen - 1;
bool match = true;
bool reCurEnd = false;... 阅读全帖
m****t
发帖数: 555
33
我的程序运行结果是正确的。
这个c程序输出就是 pylhssrtnaatareyeopsfefasmeietawodny
#include
#include
#include
int main()
{
char *A="paypalisthefastersaferwaytosendmoney";
char B[4][10]={};
int ROW = 4;
int COL;
int N;
COL = strlen(A)/ROW;
if (strlen(A)%ROW) {
COL++;
}
N= strlen(A);
int pos=0;
int c, xb=0;
int x, y;
for (c=0; c<= ROW+COL-2; c++) {
x = xb;
y = c-x;
while (x >= 0 && y <= COL-1 && pos B[x][y] = A[pos];
pos++;
x--;
y++;
}
if (xb < ROW... 阅读全帖
p********s
发帖数: 37
34
来自主题: JobHunting版 - 关于leetcode的Scramble String问题
刚试了下,用预处理+暴力搜索可以忽悠过去
预处理:对于s1,s2的所有位置p1,p2和长度l有子串(p1,p1+l)和(p2,p2+l),如果两个
子串包含不同的字符集则搜索时不予考虑
搜索:对于s1,s2,以及可能的字串长度l,递归搜索以下两种情况:
把s1和s2拆成l和strlen(s1)-l两段,分别递归搜索
把s1拆成l和strlen(s1)-l两段,把s2拆成strlen(s2-l)和l两段,分别递归搜索
w****x
发帖数: 2483
35
来自主题: JobHunting版 - facebook的面试题
上个O(n)空间的
bool isInterleave(string s1, string s2, string s3) {

const char* p1 = s1.c_str();
const char* p2 = s2.c_str();
const char* p3 = s3.c_str();

int nLen1 = strlen(p1);
int nLen2 = strlen(p2);
int nLen3 = strlen(p3);

if (nLen3 != nLen1 + nLen2)
return false;
if (nLen3 == 0) return true;

bool* rec = new bool[nLen2+1];

for (int i = nLen1; i >= 0; i--)
{
... 阅读全帖
w********g
发帖数: 106
36
知识题:
C++和Java的区别
JVM怎么工作
其余的忘记了,也都是基本概念。
编程题:
大数加法
char* add(const char*a, const char *b){ }
原以为一面就会挂,结果面试官发善心让我过了。二面的面试官可能有事,过了半小时
才打来电话。面试官是国人大哥,英语没口音,但是他念我的名字按照中国人顺序念。
因为电话迟了半小时,所以整个面试只有半小时,所以只有一道编程题。题目很简单,
但我做的很差,因为可恨我很久不用C编程了,把strlen、strcpy都忘记了。我写C++的
string.length(),结果他说只能写C的。我说我忘了,其实我记的,但是一着急就写错
了,NND一个参数的基础函数我都能写错。
我写了一句
char *result = (char*)malloc( max( strlen(a), strlen(b) ) +2 );
我边写他就边找bug,我一行还没写完他就指出我的错误。看来以后要养成一遍成的习
惯。由此我也知道这次挂了,因为对方显然要求一遍就过。
这位面试官思路很活跃很快,从来不等我把话说完或者把某一块代码写完。另外比如我... 阅读全帖
a***e
发帖数: 413
37
来自主题: JobHunting版 - Implement strStr() ?
找到一个解释比wiki清楚的source
http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.h
下面是根据那个原理写的KMP,这下觉得清楚多了。和正在学习的同学共享一下。。。
。。。顺便多谢各位。。。。
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int pos = kmpSearch(haystack, needle);
if (pos == -1) return nullptr;
else return haystack + pos;
}
static void kmpPreprocess(const char *p, vector &b)
{
int i = 0, j = -1;
b[i] = j;
const int m = strlen(p);
... 阅读全帖
a***e
发帖数: 413
38
来自主题: JobHunting版 - 能不能讨论一下kmp
曾经问过,7月份的时候花了好多时间搞清楚,现在又忘了,就记得个大概。而且以前
看着很清楚的网页现在一看没有啦。。。。。。。觉得还是看图最清楚
http://www.mitbbs.com/article_t/JobHunting/32742535.html
找到一个解释比wiki清楚的source
http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.h
下面是根据那个原理写的KMP,这下觉得清楚多了。和正在学习的同学共享一下。。。
。。。顺便多谢各位。。。。
class Solution {
public:
char *strStr(char *haystack, char *needle) {
int pos = kmpSearch(haystack, needle);
if (pos == -1) return nullptr;
else return haystack + pos;
}
static void km... 阅读全帖
b******g
发帖数: 3616
39
来自主题: JobHunting版 - 能不能讨论一下kmp
也看过不少KMP的讲解,觉得确实是matrix67讲得最清楚。CLRS讲得太生涩。之前刷lc
的strStr()这题是用土办法2 points做的,今天也跟风写了个KMP版的strStr()过了下
lc.
BTW: 今天才发现lc这题的interface改了,以前是返回指针,现在变成返回起始index
了。
vector KMPpreprocessing(char *needle) {
int m = strlen(needle);
// assume j = match[i]: needle[i-j:i] == needle[0:j]
vector match(m,-1);
int j = -1;
for(int i=1; i while(j>=0 && needle[i]!=needle[j+1]) j = match[j];
if(needle[i]==needle[j+1]) j++;
... 阅读全帖
r******s
发帖数: 925
40
来自主题: Programming版 - C++ question (reference and pointer)
The code in below doesn't work. What's the best way to fix it without
changing define of A, B, C?
what's the output?
#include
#include
using namespace std;
typedef char TT[10];
int B (TT &b)
{
cout << b << ", Len: " << strlen(b)< }
int A (TT a)
{
cout << a << ", Len a : " << strlen(a) << endl;
B(a);
}

int C (char c[10])
{
cout << c << ", Len c : " << strlen(c) << endl;
}
int main()
{
TT d;
cin >> d;
A(c);
C(d);
return 0;
}
a****m
发帖数: 693
41
#include
#include
#include
using namespace std;
// this function removes trailing spaces from x
void trim (char *str, int size )
{
int i;

/* remove the null terminator */
for ( i = size; i >0; i=i-1 )
{
if ( str[i] == '\n' || str[i]=='\t' || str[i]==' ' )
{
str[i-1] = '\0';
/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to her... 阅读全帖
w****t
发帖数: 33
42
来自主题: JobHunting版 - CS 面试题总结(5)
Debug the following code - explain the problems you find. Optionally, provid
e an
improved version.
#include
#include
char *gText = 0;
unsigned int gSize = 0; // the length of gText, in bytes
int append(const char* s )
{
if( s && s[0] )
{
if( (! gText) || (gSize = 0) )
{
gText = (char *)s;
gSize = strlen(s);
}
else
{
unsigned int len = gSize;
unsigned int s_len = strlen( s );
gSize += s_len;
char* temp = new char[ gSize ];
memcpy(temp, gText, len);
memcpy(temp + len, s, s_le
c*********n
发帖数: 1057
43
来自主题: JobHunting版 - amazon onsite 面经
2的话加个判断就好了吧
permute(char *str,int start)
int i;
if(start==strlen(str)-1)
printf("%s\n",str);
for(i=start;i if(start != i && str[i]==str[start])
continue;//eliminate duplicates
swap(str[i],str[start]);
permute(str,start+1);
swap(str[i],str[start]);
}
}
k*k
发帖数: 49
44
来自主题: JobHunting版 - amazon onsite 面经
#include
void swap(char* arr, int f, int t){
char tmp = arr[f];
arr[f] = arr[t];
arr[t] = tmp;
}
void permute(char *str,int start){
int i;
if(start==strlen(str)-1)
printf("%s\n",str);

for(i=start;i if(start != i && str[i]==str[start])
continue;//eliminate duplicates
swap(str, i, start);
permute(str,start+1);
swap(str, i, start);
}
}
int main(){
char str[] = "1233";
permute(str, 0);
}
$ ./pm | wc -l
22
but sh
c****p
发帖数: 32
45
来自主题: JobHunting版 - 一个coding题目
花了20分钟验证,20分钟coding+debug...这么慢行不行啊...
int findMinEditDistance(const char* pszStr1, const char* pszStr2)
{
size_t N = strlen(pszStr1);
size_t M = strlen(pszStr2);
int** map = new int*[N];
for(int i=0;i {
map[i] = new int[M];
}

// map[0][0]
map[0][0] = pszStr1[0] == pszStr2[0] ? 0 : 1;
// map[1...N-1][0]
for(int i=1;i {
map[i][0] = pszStr1[i] == pszStr2[0] ? i : map[i - 1][0]+1;
}
// map[0][1...M-1]
f
c*********u
发帖数: 361
46
来自主题: JobHunting版 - Google 2nd interview questions
Mine:
bool findPattern(const string& text, const string& pattern)
{
int n = strlen(pattern);
int count = 0;
for (int i = 0; i < strlen(text); i++) {
if (text[i] == pattern[count]) {
count++;
if (count == n)
return true;
}
}
return false;
}
y****n
发帖数: 579
47
来自主题: JobHunting版 - 回馈本版,贴GOOGLE电话面经
int RemoveConsecutiveDuplicates(char* s){
if(s==NULL)return 0;
int len = strlen(s);
if(len==1)return 0;
int index = 1;int tail = 0;
while(index if(s[index]==s[tail]){
index++;
}else{
tail++;
s[tail] = s[index];
index++;
}
}
s[tail+1]='\0';
return len-strlen(s);
}
i**********e
发帖数: 1145
48
来自主题: JobHunting版 - 面经-facebook, amazon,telenav, quantcast
你的代码好像有 bug,没有 '\0' 终止 string output.
我尝试写一下,思路跟两个数相加,不可利用 + operator 怎么做。
唯一一个问题就是 out = "0110",前面的 0 是无可避免,除非数组往前挪一位。
char *bstradd(char a[], char b[]) {
int n1 = strlen(a);
int n2 = strlen(b);
int len = max(n1, n2) + 1;
char *out = new char[len + 1];
out[len] = '\0';
int carry = 0;
for (int i = 0; i < len; i++) {
int dig1 = (i < n1) ? a[n1 - i - 1] - '0' : 0;
int dig2 = (i < n2) ? b[n2 - i - 1] - '0' : 0;
int sum = dig1 ^ dig2 ^ carry;
carry = (dig1 & dig2) | (di... 阅读全帖
h***o
发帖数: 1494
49
来自主题: JobHunting版 - 问个题?
int TransferString(char* str1, char* str2)
{
if(!str1||!str2)
return -1;
int len1=strlen(str1);
int len2=strlen(str2);
int** t=new int[len1+1][len2+1];
for(int i=0;i t[i][0]=i;
for(int j=0;j t[0][j]=j;
for(j=1;j for(i=1;i {
if(str1[i]==str2[j])
d[i][j]=d[i-1][j-1];
else
{
d[i][j]=d[i-1][j]>d[i][j-1]?d[i][j-1]:d[i-1][j];
d[i][j]=d[i][j... 阅读全帖
d**e
发帖数: 6098
50
贴出来看看?
这个跟strstr类似吧?
O(nm)
n = strlen(string)
m = strlen(pattern)
1 2 3 4 5 6 下页 末页 (共6页)