51.题目描述
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> multiply(const vector<int>& A)
{
//如果数组只有一个元素,则返回空数组
if(A.size()< 2)
returnvector<int>(0);
vector<int>B;
for(inti = 0; i < A.size(); i++)
{
intleft, right;
if(i== 0)
{
right= GetMultiplay(A, i+1, A.size()-1);
B.push_back(right);
continue;
}
if(i== A.size()-1)
{
left= GetMultiplay(A, 0, i-1);
B.push_back(left);
continue;
}
left= GetMultiplay(A, 0, i-1);
right= GetMultiplay(A, i+1, A.size()-1);
B.push_back(left*right);
}
returnB;
}
intGetMultiplay(const vector<int>& A, int start, int end)
{
inti = start;
intnMultiply = 1;
while(i<= end)
{
nMultiply*= A[i];
i++;
}
returnnMultiply;
}
};
int main()
{
vector<int>array(10);
vector<int>::iteratorit = array.begin();
for(;it != array.end(); it++)
{
cout<<"PLeaseinput num:";
cin>>*it;
}
Solutions;
vector<int>result = s.multiply(array);
vector<int>::iteratorite = result.begin();
for(;ite != result.end(); ite++)
cout<<*ite<<"";
cout<<endl;
return0;
}
52.题目描述
请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
#include <iostream>
using namespace std;
class Solution
{
public:
bool match(char* str, char* pattern)
{
//如果字符串为空,则返回false
if(str== NULL || pattern == NULL)
returnfalse;
returnmatchCore(str, pattern);
}
boolmatchCore(char *str, char *pattern)
{
if(*str=='\0'&& *pattern == '\0')
returntrue;
if(*str!= '\0' && *pattern == '\0')
returnfalse;
if(*(pattern+1)== '*')
{
//当第二个字符为*时,且第一个字符匹配时,存在三种情况,
if((*str== *pattern) || (*pattern == '.' && *str != '\0'))
{
returnmatchCore(str+1, pattern+2) || matchCore(str+1, pattern) || matchCore(str,pattern+2);
}
else
{
returnmatchCore(str, pattern+2);
}
}
//若没有*号,则加1后继续比较
if((*str== *pattern) || (*pattern == '.' && *str != '\0'))
returnmatchCore(str+1, pattern+1);
//不匹配返回false
returnfalse;
}
};
int main()
{
char*str = "aaa";
char*pattern = "ab*ac*a";
Solutions;
if(s.match(str,pattern))
cout<<"匹配"<<endl;
else
cout<<"不匹配"<<endl;
return0;
}
53.题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
#include <iostream>
using namespace std;
class Solution {
public:
bool isNumeric(char* string)
{
//如果字符串为空,则返回false
if(string== NULL)
returnfalse;
//首先判断正负号
if(*string== '+' || *string == '-')
string++;
if(*string== '\0')
returnfalse;
//扫描连续的数字
boolbNumeric = true;
ScanNum(&string);
if(*string!= '\0')
{
//如果是小数
if(*string== '.')
{
string++;
ScanNum(&string);
if(*string== 'e' || *string == 'E')
bNumeric= IsExponential(&string);
}
//如果是科学技术法
elseif(*string == 'e' || *string == 'E')
bNumeric= IsExponential(&string);
else
bNumeric= false;
}
returnbNumeric && *string == '\0';
}
voidScanNum(char **str)
{
while(**str!= '\0' && **str >= '0' && **str <= '9')
(*str)++;
}
boolIsExponential(char **string)
{
if(**string!= 'e' && **string != 'E')
returnfalse;
//跳过e或者E
(*string)++;
//e或者E后面的+或者-判断
if(**string== '+' || **string == '-')
(*string)++;
if(**string== '\0')
returnfalse;
ScanNum(string);
return(**string == '\0') ? true : false;
}
};
int main()
{
char*str[] = {"+100","5e2","-123","3.1416","-1E-16","12e","1a3.14","1.2.3","+-5","12e+4.3"};
Solutions;
for(inti = 0; i < 10; i++)
if(s.isNumeric(str[i]))
cout<<str[i]<<"表示数值"<<endl;
else
cout<<str[i]<<"不表示数值"<<endl;
return0;
}
54.题目描述
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
输出描述:
如果当前字符流没有存在出现一次的字符,返回#字符。
思路:在类中声明index用于记录字符出现的次序,当字符只出现一次是,哈希表中对应的值为该字符出现的次序。只要遍历哈希表,找出次序大于等于0的最小值即为第一次只出现一次的字符。具体实现见代码:
#include <iostream>
#include <limits>
using namespace std;
class Solution
{
private:
intindex; //记录了在字符串中出现的顺序
inthashTable[256];
public:
Solution():index(0)
{
//-1表示出现0次,0表示出现1次, 1表示重复出现
for(inti = 0; i < 256; i++)
hashTable[i]= -1;
}
//Insert one char from stringstream
void Insert(char ch)
{
if(hashTable[ch] == -1)
hashTable[ch]= index;
elseif(hashTable[ch] >= 0)
hashTable[ch]= -2;
index++;
}
//return the first appearence once char in current stringstream
char FirstAppearingOnce()
{
charch = '\0';
intminIndex = numeric_limits<int>::max();
for(inti = 0; i < 256; i++)
{
if(hashTable[i]>= 0 && hashTable[i] < minIndex)
{
ch= (char) i;
minIndex= hashTable[i];
}
}
returnch != '\0' ? ch : '#';
}
voidprintHashTable()
{
for(inti = 0; i < 256; i++)
cout<<hashTable[i]<<"";
}
};
int main()
{
char*str = "goog";
Solutions;
for(inti = 0; i < 6; i++)
s.Insert(str[i]);
//s.printHashTable();
cout<<s.FirstAppearingOnce()<<endl;
return0;
}
55.题目描述
一个链表中包含环,请找出该链表的环的入口结点。
#include <iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
if(pHead== NULL)
returnNULL;
//统计环中结点的数目
intnNumOfLoopNode = 0;
boolIsLoop = CountNumOfLoopNode(pHead, &nNumOfLoopNode);
if(IsLoop)
{
returnGetEntryNodeOfLoop(pHead, nNumOfLoopNode);
}
else
returnNULL;
}
boolCountNumOfLoopNode(ListNode* pHead, int *nNumOfLoopNode)
{
ListNode*lSlow = pHead;
ListNode*lFast = pHead->next;
boolbIsLoop = true;
while(lSlow!= lFast)
{
if(lSlow!= NULL && lFast != NULL)
{
lSlow= lSlow->next;
if(lFast->next!= NULL)
lFast= lFast->next->next;
else
{
bIsLoop= false;
break;
}
}
else
{
bIsLoop= false;
break;
}
}
if(bIsLoop)
{
intcount = 1;
while(lSlow->next!= lFast)
{
lSlow= lSlow->next;
count++;
}
*nNumOfLoopNode= count;
returnbIsLoop;
}
else
{
*nNumOfLoopNode= 0;
returnbIsLoop;
}
}
ListNode* GetEntryNodeOfLoop(ListNode *phead, int nNumOfLoopNode)
{
ListNode*lSlow = phead;
ListNode*lFast = phead;
for(inti = 0; i < nNumOfLoopNode; i++)
{
lFast= lFast->next;
}
while(lSlow!= lFast)
{
lFast= lFast->next;
lSlow= lSlow->next;
}
returnlSlow;
}
};
int main()
{
ListNodenode1(1);
ListNodenode2(2);
ListNodenode3(3);
ListNodenode4(4);
ListNodenode5(5);
ListNodenode6(6);
node1.next= &node2;
node2.next= &node3;
node3.next= &node4;
node4.next= &node5;
node5.next= &node6;
node6.next= &node6;
Solutions;
ListNode*pEntry = s.EntryNodeOfLoop(&node1);
if(pEntry!= NULL)
{
cout<<pEntry->val<<endl;
}
else
{
cout<<"NoLoop"<<endl;
}
return0;
}