字符串经典的算法题目求解
1、反转二叉树的经典递归实现
namespace Algo01 {
struct TreeNode
{
int nVal;
TreeNode* pLeft;
TreeNode* pRight;
TreeNode(int nValue) :nVal(nValue), pLeft(nullptr), pRight(nullptr) {
};
};
class CSolutionBinaryTreeInvert
{
public:
TreeNode* TreeInvert(TreeNode* root)
{
if (nullptr == root)
{
return nullptr;
}
TreeNode* tmpNode = root->pLeft;
root->pLeft = TreeInvert(root->pRight);
root->pRight = TreeInvert(tmpNode);
return root;
}
};
}
2、字符串的经典操作
//字符串匹配暴力求解(双指针)(m*n)
char* Strstr(const char* pSrc, const char* pTarget);
//判断字符出现是否唯一
bool IsUnique(std::string strInput);
//判断两个字符串元素是否相同(组成字符串的元素)
bool IsPermutation(std::string strOne, std::string strTwo);
//消息是否在报纸中
bool CanCompose(std::string strNewsPaper, std::string strMessage);
//两个字符串组成元数是否相等
bool Anagram(std::string strSrc, std::string strDes);
//两个字符串组成元数是否相等
namespace Algo03
{
//字符串匹配暴力求解(双指针)(m*n)
char* Strstr(const char* pSrc, const char* pTarget);
//判断字符出现是否唯一
bool IsUnique(std::string strInput);
//判断两个字符串元素是否相同(组成字符串的元素)
bool IsPermutation(std::string strOne, std::string strTwo);
//消息是否在报纸中
bool CanCompose(std::string strNewsPaper, std::string strMessage);
//两个字符串组成元数是否相等
bool Anagram(std::string strSrc, std::string strDes);
//两个字符串组成元数是否相等
bool AnagramTwo(std::string strSrc, std::string strDes);
}
char* Algo03::Strstr(const char* pSrc, const char* pTarget)
{
{
if (nullptr == pSrc || nullptr == pTarget)
{
return (char*)pSrc;
}
char * pSrcOne =(char*) pSrc;
while (pSrcOne != nullptr)
{
char* pBegin = (char*)pSrcOne;
char* pTar = (char*)pTarget;
while (pSrcOne != nullptr && pTar != nullptr && *pBegin == *pTar)
{
pSrcOne++;
pTar++;
}
if (pTar == nullptr)
{
return pBegin;
}
pSrcOne = pBegin++;
}
return nullptr;
}
}
bool Algo03::IsUnique(string strInput)
{
bitset<256> hashMap;
for (int i = 0; i < strInput.length(); i++)
{
if (hashMap[(int)strInput[i]])
{
return false;
}
hashMap[(int)strInput[i]] = 1;
}
return true;
}
bool Algo03::IsPermutation(std::string strSrc, std::string strDes)
{
if (strSrc.length() != strDes.length())
{
return false;
}
std::unordered_map<char, int> hashMapSrc;
std::unordered_map<char, int> hashMapDes;
for (int i = 0; i < strSrc.length(); i++)
{
hashMapSrc[strSrc[i]]++;
hashMapDes[strSrc[i]]++;
}
if (hashMapSrc.size() != hashMapDes.size())
{
return false;
}
for (auto tmp = hashMapSrc.begin(); tmp != hashMapSrc.end();tmp++)
{
if (tmp->second == hashMapDes[tmp->second])
{
return false;
}
}
return true;
}
bool Algo03::CanCompose(std::string strNewsPaper, std::string strMessage)
{
if (strNewsPaper.length() < strMessage.length())
{
return false;
}
unordered_map<char, int> hashMapNewsPaper;
for (int i = 0; i < strNewsPaper.length();i++)
{
hashMapNewsPaper[strNewsPaper[i]]++;
}
for (const auto tmp : strMessage)
{
if (hashMapNewsPaper.count(tmp) == 0)
{
return false;
}
if (--hashMapNewsPaper[tmp] < 0)
{
return false;
}
}
return true;
}
bool Algo03::Anagram(std::string strSrc, std::string strDes)
{
if (strSrc.empty() || strDes.empty())
{
return false;
}
if (strSrc.length() != strDes.length())
{
return false;
}
sort(strSrc.begin(), strSrc.end());
sort(strDes.begin(), strDes.end());
if (strSrc != strDes)
{
return false;
}
return true;
}
bool Algo03::AnagramTwo(std::string strSrc, std::string strDes)
{
if (strSrc.empty() || strDes.empty())
{
return false;
}
if (strSrc.length() != strDes.length())
{
return false;
}
int ArrayInt[256] = { 0 };
for (int i = 0; i < strSrc.size(); i++)
{
++ArrayInt[(int)strSrc[i]];
--ArrayInt[(int)strDes[i]];
}
for (int i = 0; i < strDes.size();i++)
{
if (ArrayInt[(int)strDes[i]] < 0)
{
return false;
}
}
return true;
}
只是写了一部分,明天继续!