由买买提看人间百态

topics

全部话题 - 话题: nindex
(共0页)
r**h
发帖数: 1288
1
来自主题: JobHunting版 - 火帖里边的一道M的题Subarray sum
题目不要求subarray是连续的吧
DFS,就是根据每个节点(取或者不取)两种情况向后搜索并记录当前的解。由于题目
里面提到数组中所有元素都是正数,因此如果当前的和已经大于sum那么就可以直接剪枝
void findSubSetSum(int *pArr, int *pSub, int nSize, int nIndex, int curSum,
int nSum){
//剪枝
if(curSum+pArr[nIndex] > nSum)
return;
//找到解
if(curSum+pArr[nIndex] == nSum){
pSub[nIndex] = 1;
printResult(pArr, pSub, nIndex);
pSub[nIndex] = 0;
return;
}
//继续向后搜索,分两种情况
if(nIndex < nSize-1){
pSub[nIndex] = 1;
findSubSetSum(pArr, pSub, nSize, nIndex+1, curSum+pArr[nIndex... 阅读全帖
H**********5
发帖数: 2012
2
txt文档三行数据,想读到结构体中
yingduSB, 61 , 65, 43, 65, 54, 43, 65
sanjie.haojian, 91, 90, 80, 100, 89, 99, 88
sange.haoer, 98, 92, 80, 100, 89, 99, 88
while (NULL!=fgets(line,60,inputFile))
{
sscanf(line,"%20[^,],%[0-9^,],%[0-9^,],%[1-9^,],%[1-9^,],%[1-9^,
][^/n]",&stu[nIndex].name,
&stu[nIndex].quiz1,
&stu[nIndex].quiz2,
&stu[nIndex].quiz3,
&stu[nIndex].quiz4,
&stu[nIndex].midi);
//&... 阅读全帖
w****x
发帖数: 2483
3
来自主题: 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
4
来自主题: JobHunting版 - 那个skiplist的题谁来给谢谢
//How to find a value in a skip list
//about skip-list http://en.wikipedia.org/wiki/File:Skip_list.svg
struct SKIP_NODE
{
int nVal;
vector links;
};
//sort like binary search, scope down to decrease the searching range
SKIP_NODE* Find(SKIP_NODE* pHead, int x)
{
assert(pHead);
SKIP_NODE* pCur = pHead;
int nIndex = pHead->links.size() - 1;
while (nIndex >= 0)
{
if (pCur->nVal == x)
return pCur;
if (pCur->links[nIndex] == NULL ||
... 阅读全帖
w****x
发帖数: 2483
5
来自主题: JobHunting版 - 出两道题目大家做做

struct EVENT
{
int nStart;
int nEnd;
bool bFlg;
EVENT(int a, int b) : nStart(a), nEnd(b) { bFlg = false; }
};
struct ENDPOINT
{
int nIndex;
bool bStart;
int nVal;
};
bool lessThan(const ENDPOINT& ed1, const ENDPOINT& ed2)
{
return ed1.nVal < ed2.nVal;
}
void setFlg(EVENT events[], int n)
{
if (events == NULL || n <= 0)
return;
ENDPOINT* ends = new ENDPOINT[n*2];
for (int i = 0; i < n; i++)
{
ends[2*i].bStart = true;
ends[2*... 阅读全帖
w****x
发帖数: 2483
6
来自主题: JobHunting版 - 拓扑排序的题怎么做?

/*
Given an array list of lots of strings, they are arranged by unknown
alphabetical
order, print all possible alphabetical orders (Sort)
*/
void buildMap(const char* str[], int n, int nBeg, bool bMap[26][26])
{
if (str == NULL || n <= 1) // <= 1 not 0
return;
int nPrev = 0;
while (str[nPrev][nBeg] == 0 && nPrev < n) // missing nPrev < n
nPrev++;
int nIter = nPrev + 1;
while (nIter < n)
{
if (str[nPrev][nBeg] != str[nIter][nBeg])
{
... 阅读全帖
b******g
发帖数: 77
7
来自主题: JobHunting版 - GOOGLE 第二轮电面
N * LogN 解法
class StrCompare
{
public:
bool operator()( const string& str1, const string& str2 ) const
{
return str1.size() > str2.size();
}
};
class PairFinder
{
public:
void init()
{
m_nProductSize = 0;
}

size_t getProduct() const { return m_nProductSize; }
pair getPair() const { return make_pair( m_csFirst, m_
csSecond ); }

void compute( vector& vctStr )
{
init();
if( vctStr.size() < 2 ) re... 阅读全帖
b******g
发帖数: 77
8
一个martrix,每行每列都是sorted,找到前kth 个
Analysis:
Applying a quick-select algorithm will achieve an O( n * n ) time
complexity. Can we do better? Yes. Here is a algorithm:
1. Like quick-select algorithm, this algorithm applies divide-and-
conquer.
2. Initialize those two boundary lines to (n * [0] and n * [n]) to
include all array items in boundary.
3. Process subarray between boundary lines (lo, hi ) in each
recursive call:
a. select a random item in su... 阅读全帖
p*****e
发帖数: 53
9
来自主题: Programming版 - 帮忙看看这几段程序有问题吗?
实在看不出哪里不对,只能来这里求助高手帮忙看看了
非常感谢!!!
1.
int IsSecretPassword(char *ptrstring)
{
char temp[1024];
if(ptrstring)
{
strcpy(temp, ptrstring);
for (int i =0; i {
temp[i] = toupper(temp[i]);
}
if (strcmp(temp, "TEST")==0)
return 1;
else
return 0;
}
return 0;
}
2.
int* MakeArray(int nsize)
{
int *ptr = 0;
if(nsize <=0)
return NULL;
ptr = (int*)malloc(sizeof(int... 阅读全帖
c***r
发帖数: 4631
10
来自主题: Programming版 - 帮忙看看这几段程序有问题吗?
俺系业余爱好的,上来玩玩。
1,
int IsSecretPassword(char *ptrstring)
{
if( null == ptrstring ) return 0;
int n = strlen(ptrstring);

if( n > 0)
{
char * temp = malloc(n+1);
int r;
strcpy(temp, ptrstring);
for (int i =0; i {
temp[i] = toupper(temp[i]);
}
if (strcmp(temp, "TEST")==0)
{
r=1;
}
else
{
r = 0;
}
free(temp);
return r;
}
return 0;
}
2,
int* MakeArray(u... 阅读全帖
b**********1
发帖数: 215
11
来自主题: Programming版 - C++ 模板的技术问题
template class
CRollBufferFast
{
public:
CRollBufferFast()
{ m_pData = new TYPE[WINDOW_ELEMENTS + HISTORY_ELEMENTS];
Flush(); }
.....
__forceinline TYPE & operator[](const int nIndex) const
{
return m_pCurrent[nIndex];
}
protected:
TYPE * m_pData;
TYPE * m_pCurrent;
};
CRollBufferFast m_rbPrediction;
m_rbPrediction[0] = nA;
m_rbPrediction[-2] = m_rbPrediction[-1] - m_rb... 阅读全帖
m*******l
发帖数: 12782
12
匈牙利是n吧
nIndex这样的
(共0页)