由买买提看人间百态

topics

全部话题 - 话题: nlen
1 (共1页)
w****x
发帖数: 2483
1
来自主题: JobHunting版 - 问个amazon面试题
int ClacTarget(int nLen, int nIndex)
{
assert(nLen > 0 && nIndex > 0 && nLen > nIndex);
if (nIndex < nLen/2)
return 2 * nIndex;
return (nIndex - nLen/2) * 2 + 1;
}
char* InplaceConvert(char* str)
{
if (NULL == str)
return NULL;
int nLen = strlen(str);
if (0 == nLen || nLen%2 != 0)
return str;
int nCount = nLen - 2;
for (int i = 1; i < nLen && nCount > 0; i += 2)
{
int nCur = ClacTarget(nLen, i);
char cTmp = str[nCur];
... 阅读全帖
w****x
发帖数: 2483
2
来自主题: JobHunting版 - 做了一下merge BST
//Merge two BST
//O(n) solution
//1. Change BST into linked list
//2. Merge 2 linked lists
//3. Change linked list into a balanced BST
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
////////////////////change BST to linked list///////////////////////////////
void _inner_chng_link(NODE* pNode, NODE*& ph, NODE*& pt)
{
if (NULL == pNode) return;
NODE* pLft1 = pNode;
NODE* pLft2 = pNode;
_inner_chng_link(pNode->pLft... 阅读全帖
w****x
发帖数: 2483
3
来自主题: JobHunting版 - 攒人品,Twitter 电面题目

写一个
//Merge two BST
//O(n) solution
//1. Change BST into linked list
//2. Merge 2 linked lists
//3. Change linked list into a balanced BST
struct NODE
{
int nVal;
NODE* pLft;
NODE* pRgt;
NODE(int n) : nVal(n), pLft(NULL), pRgt(NULL) {}
};
////////////////////change BST to linked list///////////////////////////////
void _inner_chng_link(NODE* pNode, NODE*& ph, NODE*& pt)
{
if (NULL == pNode) return;
NODE* pLft1 = pNode;
NODE* pLft2 = pNode;
_inner_chng_link(pNode-... 阅读全帖
w****x
发帖数: 2483
4
来自主题: JobHunting版 - 昨天的F家店面

..
为什么要逼我用stl...
int read4096(FILE* pf, char* pBuf);
char buf[4096];
int nLen = 0;
char* readline(FILE* pf)
{
std::string strRet;
bool bFound = false;
while(true);
{
int i = 0;
for (; i < nLen; i++)
{
if (buf[i] == 0 || buf[i] == '\n')
break;
strRet.append(buf[i]);
}
if (i == nLen)
nLen = 0;
else
{
nLen -= i+1;
memcpy(buf, buf+i+1, nLen);
... 阅读全帖
w****x
发帖数: 2483
5
来自主题: JobHunting版 - 被thank you的fb电面面经
int _inner_ways(const char* pStr)
{
if (*pStr == 0)
return 1;
if (*pStr <= '0' || *pStr > '9')
return 0;
int n = _inner_ways(pStr+1);
if ((*pStr == '1' && *(pStr + 1) >= '0' && *(pStr + 1) <= '9')
|| (*pStr == '2' && *(pStr + 1) >= '0' && *(pStr + 1) <= '6'))
n += _inner_ways(pStr+2);
return n;
}
int GetWays(const char* pStr)
{
if (NULL == pStr || *pStr == 0)
return 0;
return _inner_ways(pStr);
}
int GetWaysDP(const char* pStr)
... 阅读全帖
w****x
发帖数: 2483
6
Print neatly
一下忘了怎么做了,花了半小时,处理下标很痛苦啊。
忽然发现没想的那么复杂。
//you have a list of words, you want to print them
//on a paper with width n. when printing, each
//word must be separated by one blank except the blanks after the last word.
Find the fewest
//possible blanks except the last line.
//f(i, j) = [1 + f(i+1, j+len(i)+1) ] || [nLen - j-len(i) + f(i+1,0)]
int GetLeastBlanks(int a[], int n, int nLen)
{
if (NULL == a || n <= 0 || nLen <= 0)
return -1;
int rec[100][100] = { 0 };
for (... 阅读全帖
x*********w
发帖数: 533
7
来自主题: JobHunting版 - 问道算法题
const char* minimumPadding(const char* szStr, char res[])
{
if (NULL == szStr || *szStr == 0 || NULL == res)
return 0;
int nLen = strlen(szStr);
int rec[100][100] = { 0 };
for (int i = 0; i < nLen-1; i++)
{
rec[i][i+1] = szStr[i] == szStr[i+1] ? 0 : 1;
}
for (int i = 2; i < nLen; i++)
{
for (int j = 0; j < nLen-i; j++)
{
rec[j][j+i] = 1 + min(rec[j+1][j+i], rec[j][j+i-1]);
if (szStr[j] == szStr[j+i])
... 阅读全帖
w****x
发帖数: 2483
8
来自主题: JobHunting版 - 昨天的F家店面
发现一个bug:
int read4096(FILE* pf, char* pBuf);
char buf[4096];
int nLen = 0;
void readline(char* szMem, FILE* pf)
{
assert(szMem);
bool bRet = false;
char* pWrite = szMem;
while(true);
{
int i = 0;
for (; i < nLen; i++)
if (buf[i] == 0 || buf[i] == '\n')
{
bRet = true;
break;
}
int nMove = i+1;
if (!bRet)
nMove = i;
memcpy(pWrite, buf, nMove);
... 阅读全帖
w****x
发帖数: 2483
9
来自主题: JobHunting版 - 昨天的F家店面
int read4096(FILE* pf, char* pBuf);
char buf[4096];
int nLen = 0;
char* readline(char* szMem, FILE* pf)
{
assert(szMem);
bool bRet = false;
char* pWrite = szMem;
while(true);
{
int i = 0;
for (; i < nLen; i++)
if (buf[i] == 0 || buf[i] == '\n')
{
bRet = true;
break;
}
int nMove = i+1;
if (!bRet)
nMove = i;
memcpy(pWrite, buf, nMove);
... 阅读全帖
w****x
发帖数: 2483
10

没意思
单链表的quick sort就可以了
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev-... 阅读全帖
x*********w
发帖数: 533
11
quick sort版本
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev->pNext;
... 阅读全帖
p*****3
发帖数: 488
12
来自主题: JobHunting版 - 请问如何sort一个linked list?
给个我以前写的吧,快速排序版本的
struct NODE
{
    int nVal;
    NODE* pNext;
 
    NODE(int n) : nVal(n), pNext(NULL) {}
};
 
void sort(NODE* pNode, int nLen)
{
    if (NULL == pNode || nLen <= 0)
        return;
 
    int nCount = 0;
    int nPivot = pNode->nVal;
    NODE* pPrev = pNode;
    NODE* ... 阅读全帖
w****x
发帖数: 2483
13
前天刚做过
//permutation of string with duplicate characters
//different from the subset problem, can't solve duplicated chars
//situation by sorting the array
void _inner_print(char strOrg[], char strCur[], int nLen)
{
assert(strOrg && strCur && nLen >= 0);
if (0 == nLen)
{
cout< return;
}
//record if a character is calculated as a header
//to eliminate duplicated characters
bool bRec[256];
memset(bRec, 0, sizeof(bRec));
for (int i = ... 阅读全帖
w****x
发帖数: 2483
14
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
你看这样行不行:
void _inner_print(char strOrg[], char strCur[], int nLen)
{
assert(strOrg && strCur && nLen >= 0);
if (0 == nLen)
{
cout< return;
}
//record if a character is calculated as a header
//to eliminate duplicated characters
bool bRec[256];
memset(bRec, 0, sizeof(bRec));
for (int i = 0; i < nLen; i++)
{
if (!bRec[strCur[i]])
{
swap(strCur[0], strCur[i]);
_inner_print(strOrg, strCur+1,... 阅读全帖
w****x
发帖数: 2483
15
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
你看这样行不行:
void _inner_print(char strOrg[], char strCur[], int nLen)
{
assert(strOrg && strCur && nLen >= 0);
if (0 == nLen)
{
cout< return;
}
//record if a character is calculated as a header
//to eliminate duplicated characters
bool bRec[256];
memset(bRec, 0, sizeof(bRec));
for (int i = 0; i < nLen; i++)
{
if (!bRec[strCur[i]])
{
swap(strCur[0], strCur[i]);
_inner_print(strOrg, strCur+1,... 阅读全帖
w****x
发帖数: 2483
16
来自主题: JobHunting版 - 昨天的F家店面
妈的, 要是15分钟我也超时了
int read4096(FILE* pf);
char buf[4096];
int nLen = 0;
void readline(char* szMem, FILE* pf)
{
assert(szMem);
bool bRet = false;
char* pWrite = szMem;
while(true);
{
int i = 0;
for (; i < nLen; i++)
if (buf[i] == 0 || buf[i] == '\n')
{
bRet = true;
break;
}
int nMove = i+1;
if (!bRet)
nMove = i;
memcpy(pWrite, bu... 阅读全帖
w****x
发帖数: 2483
17
来自主题: JobHunting版 - 请教两个题
max rectangle做了个O(n^3)的解法, histogram那个我是不指望了
const int M = 5;
int GetMaxRect(bool A[M][M])
{
int rec[M][M];
for (int j = 0; j < M; j++)
{
for (int i = 0; i < M; i++)
{
if (A[i][j])
rec[i][j] = 0;
else
rec[i][j] = i == 0 ? 1 : 1 + rec[i-1][j];
}
}
int nMax = 0;
for (int i = M-1; i >= 0; i--)
{
for (int j = i; j >= 0; j--)
{
int nLen = 0;
int nBeg = -... 阅读全帖
b**********5
发帖数: 7881
18
来自主题: JobHunting版 - 三哥题刷的不赖啊
kmp 没几行字吧。。。
int strstr(String haystack, String needle) {
int hLen = hayStack.length();
int nLen = needle.length();
int i = 0; int j = 0;
int[] next = new int[nLen];
preProcess(needle, next);
while (i < hLen && j < nLen) {
if (haystack.charAt(i) == needle.charAt(j)) { i++; j++;}
else {
j = next[j];
}
}
if (j == nLen) return i-j;
else return -1;
}
void preProcess(String needle, int[] next) {
next[0] = -1;
int k = -1... 阅读全帖
w****x
发帖数: 2483
19
来自主题: JobHunting版 - 大家看看我写的这个itoa有没有bug
const char* myitoa(int num, char str[])
{
assert(a);
char* pIterBeg = str;
if (num < 0)
{
num = -num;
*pIterBeg++ = '-';
}
//use long not short to avoid INT_MIN overflow
unsigned int numshort = num;
char* pIter = pIterBeg;
while (true)
{
*pIter++ = numshort%10 + '0';
numshort = numshort/10;
if (numshort == 0) break;
}
*pIter = 0;
int nLen = strlen(pIterBeg);
assert(nLen > 0);
char* pIterEnd = ... 阅读全帖
w****x
发帖数: 2483
20
来自主题: JobHunting版 - 刚才重新回顾了一下那题
struct NODE
{
vector vecGUID;
NODE* nodes[256];

NODE() { memset(nodes, 0, sizeof(nodes)); }
};
void _inner_get_guid(NODE* pNode, const char* str, int k, vector& vec)
{
if (NULL == pNode)
return;
if (k <= 0)
{
vec = pNode->vecGUID;
return;
}
_inner_get_guid(pNode->nodes[*str], str+1, k-1, vec);
}
vector getGUID(const char* str, NODE* pRoot, int k)
{
vector vecRet;
if (NULL == pRoot || NULL == str || *str == 0 ... 阅读全帖
w****x
发帖数: 2483
21
来自主题: JobHunting版 - 罗马转数字,数字转罗马问题

//Turn Roman number to decimal
//I(1), V(5), X(10), L(50), C(100), D(500), M(1000)
int GetNum(char c)
{
if (c == 'I' || c == 'i')
return 1;
else if (c == 'V' || c == 'v')
return 5;
else if (c == 'X' || c == 'x')
return 10;
else if (c == 'L' || c == 'l')
return 50;
else if (c == 'C' || c == 'c')
return 100;
else if (c == 'D' || c == 'd')
return 500;
else if (c == 'M' || c == 'm')
return 1000;

return 0;
}
... 阅读全帖
w****x
发帖数: 2483
22

//leetcode OJ --> get decode ways
int GetDecodeWays(const char* str)
{
assert(str);
int nLen = strlen(str);
if (nLen <= 0) return 0;
int a = 1;// 1 not 0
int b = (str[0] >= '1' && str[0] <= '9') ? 1 : 0;
for (int i = 1; i < nLen; i++)
{
int c = 0;
if ((str[i-1] == '1' && str[i] >= '0' && str[i] <= '9')
|| (str[i-1] == '2' && str[i] >= '0' && str[i] <= '6'))
c += a;
if (str[i] >= '1' && str[i] <= '9')
c += b;... 阅读全帖
w****x
发帖数: 2483
23
来自主题: JobHunting版 - 发一个刚面的startup面经
直接贴Google doc了
Given a word, print out all the combinations of words from the letters that
make it. For example:
bad: abd adb bda dab dba
void _inner_print(char str[], int len, int pos)
{
if (pos == len)
{
cout< return;
}
for (int i = pos; i < len; i++)
{
swap(str[pos], str[i]);
_inner_print(str, len, pos+1);
swap(str[pos], str[i]);
}
}
Input: bad
step 1: 3, i = 0 - bad
step 2.: 3, i = 1 - abd
step 2.: 3, i = 2 - dabchar
void... 阅读全帖
j*****y
发帖数: 1071
24
来自主题: JobHunting版 - 发一个刚面的startup面经
thanks。 你的第一题的code感觉有点问题, swap 以后,应该 swap 回来
for (int i = pos; i < len; i++)
{
swap(str[pos], str[i]);
_inner_print(str, len, pos+1);
swap(str[pos], str[i]);
swap(str[pos], str[i]);
}

Given a word, print out all the combinations of words from the letters that
make it. For example:
bad: abd adb bda dab dba
void _inner_print(char str[], int len, int pos)
{
if (pos == len)
{
cout< return;
}
for (int i = pos; i < len; ... 阅读全帖
s********z
发帖数: 5411
25
来自主题: Programming版 - #ifdef _MSC_VER 是什么意思啊?
我在把下面这段code改成qt,是不是需要删掉 “ifdef _MSC_VER”阿?
这里为什么要用”#ifdef _MSC_VER“ “#endif”阿? 只有是visual studio
compiler情
况下,这个code才能用?
多谢
xx.h
#ifdef _MSC_VER
int ReadString(CString& strMsg, char chStop);
int ReadString(CString& strMsg);
int WriteString(CString strCmd);
#endif
xx.cpp
#ifdef _MSC_VER
int CCamlinkSerial::ReadString(CString& strMsg, char chStop)
{
if (!m_bOpened) { return 0; }
char chCur = chStop + 1; // ensures that ucCur and ucStop are
different to begi... 阅读全帖
w****x
发帖数: 2483
26
来自主题: JobHunting版 - 谷歌面经
struct NODE
{
string str;
NODE* pLft;
NODE* pRgt;
NODE(const char* szStr = "") : str(szStr), pLft(NULL), pRgt(NULL) {}
};
void serialize(NODE* pNode, char*& p)
{
if (p == NULL) return;
if (pNode == NULL)
{
*((int*)p) = 0;
p += sizeof(int);
return;
}

int nLen = pNode->str.length();
*((int*)p) = 1;
p += sizeof(int);
strcpy(p, pNode->str.c_str());
p += nLen+1;

serialize(pNode->pLft, p);
serialize(pNode->pRgt... 阅读全帖
k*******3
发帖数: 1909
27
来自主题: Programming版 - return Triangular_iterator( _beg_pos );意思
下面程序copy来自C++ essential 4.7节,
Triangular_iterator begin() const
{ return Triangular_iterator( _beg_pos ); }
请问
return Triangular_iterator( _beg_pos ); 这句话的语法是怎么样的?
Triangular_iterator是自定义的一个class吧,class后面直接加(_beg_pos)什么意思
呢?
如果是返回一个Triangular_iterator的object, 不需要定义成
Triangular_iterator obj(_beg_pos);
return obj;吗?
谢谢!
================================Full Class definition==================
class Triangular_iterator
{
public:
Triangular_iterator( int index ) : _index( index-1 ){} //*... 阅读全帖
w***o
发帖数: 109
28
来自主题: JobHunting版 - 罗马转数字,数字转罗马问题
倒着来想法很新颖,但好像有问题,比如 IVX = (V - I) + X, not (X - V - I)

const char* pIter = szNum + nLen - 1;
int nRet = 0;
int nPrev = 0;
while (pIter >= szNum)
{
int nCur = GetNum(*pIter);
if (nCur >= nPrev)
nRet += nCur;
else nRet -= nCur;
nPrev = nCur;
pIter--;
}
x*********n
发帖数: 28013
29
来自主题: EmergingNetworking版 - 为什么删不掉?
DC4500-colocation#sh bootflash:
-#- ED ----type---- --crc--- -seek-- nlen -length- ---------date/time-------
-- name
1 .. image 07861B02 119B02C 39 17936300 Feb 14 2011 10:38:40 -06:
00 cat4500-entservicesk9-mz.122-53.SG2.bin
2 .D image 33259397 21416E4 38 16410168 Mar 15 2011 18:20:05 -05:
00 cat4500-entservicesk9-mz.122-44.SG.bin
DC4500-colocation#dir bootflash:
Directory of bootflash:/
1 -rwx 17936300 Feb 14 2011 10:38:40 -06:00 cat4500-entservicesk9-
mz.122-53... 阅读全帖
w*******y
发帖数: 60932
30
Sony HDRCX110 High Definition Handycam Camcorder $358 with Free Shipping w
code NLENS
Also in blue (sku sonhdrcx110L) and red (sku sonhdrcx110R) are same price.
this is the cheapest i saw by almost $40.
Link:
http://www.electronics-expo.com/make-a-store/item/SONHDRCX110 /Sony/HDRCX110/1.html
peep the wiki below. Much appreciated
1 (共1页)