Leetcode5. 最长回文子串
原题链接
class Solution {
public:
string longestPalindrome(string s) {
int len = s.size();
string ans = "";
vector<vector<int>> dp(len, vector<int>(len,0));
for (int l = 0;l < len;l++)
{
for (int i = 0;i < len - l;i++)
{
int j = i + l;
if (l == 0)
dp[i][j] = true;
else if (l == 1)
dp[i][j] = (s[i] == s[j]);
else
dp[i][j] = dp[i+1][j-1]&&(s[i]==s[j]);
if (dp[i][j] && l + 1 > ans.size())
ans = s.substr(i,l + 1);
}
}
return ans;
}
};
二叉树后序遍历非递归(双栈法)
void PostOrderWithoutRecusive(TreeNode *root)
{
if (root == nullptr)
{
return;
}
stack<TreeNode *>sta1;
stack<TreeNode *>sta2;
TreeNode * tmp = root;
while (!sta1.empty() || tmp)
{
if (tmp)
{
sta1.push(tmp);
sta2.push(tmp);
tmp = tmp->right;
}
else
{
tmp = sta1.top();
sta1.pop();
tmp = tmp->left;
}
}
while (!sta2.empty())
{
cout<<sta2.top()<<" ";
sta2.pop();
}
}
快速幂
typedef long long ll;
ll myPow(ll x, ll n)
{
ll res = 1;
while (n)
{
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}