#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <list>
#include <algorithm>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <hash_map>
#include <bitset>
#include <string>
#include "myqueue.h"
#include "myvector.h"
#include "mystl.h"
#include "heap.h"
#include "mylist.h"
#include "dp.h"
#include "sort.h"
using namespace std;
/********* Tree ***********/
struct Node {
Node(int n) : value(n), left(NULL), right(NULL)
{}
int value;
Node *left;
Node *right;
};
Node* Build(int a[], int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
Node *root = new Node(a[mid]);
root->left = Build(a, start, mid - 1);
root->right = Build(a, mid + 1, end);
return root;
}
Node* BuildeATree(int *a, int len)
{
if (a == NULL)
return NULL;
return Build(a, 0, len - 1);
}
pair<int, int> core(Node *root)
{
pair<int, int> pairLeft;
pair<int, int> pairRight;
pair<int, int> ret(root->value, root->value);
if (root->left != NULL)
{
pairLeft = core(root->left);
if (pairLeft.first > ret.first)
ret.first = pairLeft.first;
if (pairLeft.second < ret.second)
ret.second = pairLeft.second;
}
if (root->right != NULL)
{
pairRight = core(root->right);
if (pairRight.first > ret.first)
ret.first = pairRight.first;
if (pairRight.second < ret.second)
ret.second = pairRight.second;
}
return ret;
}
int FindMaxDiff(Node *root)
{
pair<int, int> MaxMin;
MaxMin = core(root);
return abs(MaxMin.first - MaxMin.second);
}
void printTree(Node *root)
{
if (root == NULL)
return;
printTree(root->left);
cout << root->value << ' ';
printTree(root->right);
}
void PrintByLevel(Node *root)
{
if (root == NULL)
return;
queue<Node *> queue;
queue.push(root);
while (!queue.empty())
{
Node *pNode = queue.front();
queue.pop();
cout << pNode->value << ' ';
if (pNode->left)
queue.push(pNode->left);
if (pNode->right)
queue.push(pNode->right);
}
}
#define NUM 7
int money[NUM] = { 1, 2, 5, 10, 20, 50, 100 };
// 母函数解法
int NumOfCoins(int value)
{
int i, j, k, c1[1010], c2[1010];
for (i = 0; i <= value; ++i)
{
c1[i] = 1;
c2[i] = 0;
}
// 第一层循环是一共有 n 个小括号,而刚才已经算过一个了
// i 就是代表的母函数中第几个大括号中的表达式
for (i = 1; i < NUM; ++i)
{
for (j = 0; j <= value; ++j) //j 就是指的已经计算出的各项的系数
{
for (k = 0; k + j <= value; k += money[i]) //k 就是指将要计算的那个括号中的项
c2[k + j] += c1[j];
}
for (j = 0; j <= value; ++j) // 刷新一下数据,继续下一次计算,就是下一个括号里面的每一项
{
c1[j] = c2[j];
c2[j] = 0;
}
}
return c1[value];
}
int CalPermNum(int index, int num)
{
if (index < 0 || num <0)
return 0;
if (index == 0 || num == 1)
return 1;
// 一定index-1和index的混合,细细的体味一下这个意思
return CalPermNum(index - 1, num) + CalPermNum(index, num - money[index]);
}
// 牛顿迭代法开方
void mysqrt(double x)
{
double k = 1;
while (abs(x - k * k) > 0.0000000001)
{
k = (k + x / k) / 2;
}
printf("%.2f", k);
}
// Stein算法高效求最大公约数
// 最小公倍数=两整数的乘积÷最大公约数
int gcd(int x, int y)
{
if (x < y)
return gcd(y, x);
else if (y == 0)
return x;
else if ((x & 0x01) == 0)
{
if ((y & 0x01) == 0)
return (gcd(x >> 1, y >> 1) << 1); // gcd(ka,kb) = k * gcd(a,b)
else
return gcd(x >> 1, y);
}
else
{
if ((y & 0x01) == 0)
return gcd(x, y >> 1);
else
return gcd(y, x - y);
}
}
// 顺时针打印矩阵
void Print(int **m, int rows, int cols, int start)
{
int endX = cols - start - 1;
int endY = rows - start - 1;
for (int i = start; i <= endX; i++)
cout << m[start][i] << ' ';
if (endY > start)
for (int j = start + 1; j <= endY; j++)
cout << m[j][endX] << ' ';
if (endY > start && endX > start)
for (int i = endX - 1; i >= start; i--)
cout << m[endY][i] << ' ';
if (endY > start + 1 && endX > start)
for (int j = endY - 1; j > start; j--)
cout << m[j][start] << ' ';
}
void PrintMatrix(int **m, int rows, int cols)
{
if (m == NULL || rows <= 0 || cols <= 0)
return;
for (int start = 0; (2 * start < rows) && (2 * start < cols); start++)
{
Print(m, rows, cols, start);
}
}
// 八皇后问题
int cnt = 0;
void EightQueue(int a[], int len, int index)
{
if (index == len)
{
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if (abs(a[i] - a[j]) == abs(i - j))
return;
}
}
cnt++;
return;
}
for (int i = index; i < 8; i++)
{
Swap(&a[index], &a[i]);
EightQueue(a, len, index + 1);
Swap(&a[index], &a[i]);
}
}
///*******链表*******/
//struct ListNode {
// ListNode(int n = 0) : value(n)
// {}
// int value;
// ListNode *next;
//};
//
链表反转
//ListNode* Reverse(ListNode *pHead)
//{
// if (pHead == NULL || pHead->next == NULL)
// return pHead;
//
// ListNode *pLeft = pHead;
// ListNode *pMid = pHead->next;
// ListNode *pRight = pHead->next->next;
// pLeft->next = NULL;
// while (pRight != NULL)
// {
// pMid->next = pLeft;
//
// pLeft = pMid;
// pMid = pRight;
// pRight = pRight->next;
// }
// pMid->next = pLeft;
//
// return pMid;
//}
//
顺序打印链表
//void PrintList(ListNode *pHead)
//{
// while (pHead != NULL)
// {
// cout << pHead->value << ' ';
// pHead = pHead->next;
// }
// cout << endl;
//}
//
逆序打印链表
//void PrintReversely(ListNode *pHead)
//{
// if (pHead == NULL)
// return;
//
// ListNode *pNode = pHead;
// stack<ListNode *> s;
// while (pNode != NULL)
// {
// s.push(pNode);
// pNode = pNode->next;
// }
// while (!s.empty())
// {
// cout << s.top()->value << ' ';
// s.pop();
// }
// cout << endl;
//}
//
//ListNode* CreateList()
//{
// ListNode *pHead = new ListNode(0);
// pHead->next = NULL;
// for (int i = 10; i > 0; i--)
// {
// ListNode *pNode = new ListNode(i);
// pNode->next = pHead->next;
// pHead->next = pNode;
// }
// return pHead;
//}
// 旋转数组中的查找
int Search(int A[], int n, int num)
{
int left = 0;
int right = n - 1;
int mid = 0;
int pos = -1; //返回-1,表示查找失败
while (left <= right)
{
mid = (left + right) >> 2;
if (A[mid] == num)
{
pos = mid;
break;
}
// 前半部分是严格递增的,后半部分是一个更小的循环递增数组
if (A[left] <= A[mid])
{
if (A[left] <= num && num < A[mid])
right = mid - 1;
else
left = mid + 1;
}
//后半部分是严格递增的,前半部分是一个更小的循环递增数组
else
{
if (A[mid] < num && num <= A[right])
left = mid + 1;
else
right = mid - 1;
}
}
return pos;
}
// Add an element to the bottom of a stack:
template<typename T>
void AddToStackBottom(std::stack<T>& stack, T t)
{
if (stack.empty())
{
stack.push(t);
}
else
{
T top = stack.top();
stack.pop();
AddToStackBottom(stack, t);
stack.push(top);
}
}
// Reverse a stack recursively in three steps:
// 1. Pop the top element
// 2. Reverse the remaining stack
// 3. Add the top element to the bottom of the remaining stack
template<typename T>
void ReverseStack(std::stack<T>& stack)
{
if (!stack.empty())
{
T top = stack.top();
stack.pop();
ReverseStack(stack);
AddToStackBottom(stack, top);
}
}
void Probability(int *cnt, int n, int sum)
{
if (n == 0)
{
cnt[sum]++;
return;
}
for (int i = 1; i <= 6; i++)
{
Probability(cnt, n - 1, sum + i);
}
}
// n个骰子,各种情况的概率
void Probability(int n)
{
int *cnt = new int[6 * n + 1];
for (int i = 0; i < 6 * n + 1; i++)
cnt[i] = 0;
for (int i = 1; i <= 6; i++)
{
Probability(cnt, n - 1, i);
}
for (int i = 0; i < 6 * n + 1; i++)
cout << cnt[i] << ' ';
cout << endl;
delete[] cnt;
}
// 骰子游戏的优化版本
void Dice(int n)
{
int **d = new int*[2];
d[0] = new int[6 * n + 1];
d[1] = new int[6 * n + 1];
memset(d[0], 0, sizeof(int)* (6 * n + 1));
memset(d[1], 0, sizeof(int)* (6 * n + 1));
// 初始化第一个骰子
for (int i = 1; i <= 6; i++)
d[0][i] = 1;
// 从第二个骰子开始
int flag = 1;
for (int i = 2; i <= n; i++)
{
for (int j = i; j <= 6 * i; j++)
{
for (int back = 1; back <= 6 && j - back >= 1; back++)
{
d[flag][j] += d[1 - flag][j - back];
}
}
flag = 1 - flag;
memset(d[flag], 0, sizeof(int)* (6 * n + 1));
}
for (int i = 0; i < 6 * n + 1; i++)
cout << d[1 - flag][i] << ' ';
cout << endl;
}
// 不使用除号的整数除法
int Div(const int x, const int y)
{
int left_num = x;
int result = 0;
while (left_num >= y)
{
int multi = 1;
while (y * multi <= (left_num >> 1))
multi = multi << 1;
result += multi;
left_num -= y * multi;
}
return result;
}
// 十进制按二进制打印
void DecimalToBinary(long n)
{
long mask = 1;
int bits = sizeof(long) * 8;
while (bits--)
{
if ((n & (mask << bits)) != 0)
cout << 1;
else
cout << 0;
}
}
// 十进制按十六进制打印
void DecimalToHex(long n)
{
long mask = 0xf;
int bits = sizeof(long)* 8;
bits /= 4;
while (bits--)
{
int tmp = (n & (mask << (bits * 4))) >> (bits * 4);
if (tmp >= 0 && tmp <= 9)
cout << tmp;
else
cout << (char)(tmp - 10 + 'A');
}
}
void Allocate(vector<int> &buckets, int CurrentEggs, int IndexOfBuckt, int Base, int n, int m)
{
if (CurrentEggs > n)
return;
if (IndexOfBuckt == m )
{
if (CurrentEggs == n)
{
copy(buckets.begin(), buckets.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
return;
}
for (int i = Base; i <= n - CurrentEggs; i++)
{
buckets.push_back(i);
Allocate(buckets, CurrentEggs + i, IndexOfBuckt + 1, i, n, m);
buckets.pop_back();
}
}
// n个鸡蛋,m个篮子
void EggsAndBuckets(int n, int m)
{
if (n <= 0 || m <= 0 || n < m)
return;
vector<int> buckets;
for (int i = 1; i <= n; i++)
{
buckets.push_back(i);
Allocate(buckets, i, 1, i, n, m);
buckets.pop_back();
}
}
// b[i] = (a[0] * a[1] * a[2] ... * a[N-1]) / a[i],不使用其它变量
void ProductOfSubArray(int a[], int b[], int len)
{
b[0] = a[0];
for (int i = 1; i < len; i++)
{
b[i] = b[0];
b[0] = a[i] * b[i];
}
b[0] = a[len - 1];
for (int i = len - 2; i > 0; i--)
{
b[i] = b[i] * b[0];
b[0] = b[0] * a[i];
}
for (int i = 0; i < len; i++)
cout << a[i] << '\t';
cout << endl;
for (int i = 0; i < len; i++)
cout << b[i] << '\t';
cout << endl;
}
// 最长重复子串
int operator- (const string &s1, const string &s2)
{
int len = min(s1.length(), s2.length());
int cnt = 0;
for (int i = 0; i < len; i++)
{
if (s1[i] != s2[i])
break;
cnt++;
}
return abs(cnt);
}
void LongestSubString(const string &str)
{
vector<string> substrings;
int length = str.length();
// 求出给定字符串的所有后缀
for (int i = 0; i < length; i++)
substrings.push_back(str.substr(i, length - i));
// 后缀排序
sort(substrings.begin(), substrings.end());
int max = 0;
int index = 0;
for (int i = 0; i < substrings.size() - 1; i++)
{
// 利用重载的operator-计算相邻后缀的最长公共前缀
int diff = substrings[i] - substrings[i + 1];
if (diff > max)
{
index = i;
max = diff;
}
}
string res = substrings[index].substr(0, max);
cout << res << endl;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <math.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int maxDepth(TreeNode *root)
{
if (root == NULL)
return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return (left > right ? left + 1 : right + 1);
}
};
int main()
{
10;
system("pause");
return 0;
}
// 并查集
//class UnionFind {
//public:
// UnionFind(int n) : cnt(n)
// {
// id = new int[n];
// for (int i = 0; i < n; i++)
// id[i] = i;
//
// size = new int[n];
// for (int i = 0; i < n; i++)
// size[i] = 1;
// }
//
// ~UnionFind()
// {
// delete[] id;
// delete[] size;
// }
//
// // 返回连通组的个数
// int count()
// {
// return cnt;
// }
//
// // 查找一个节点属于哪个组
// int findSet(int a)
// {
// if (id[a] == a)
// return a;
// else
// return id[a] = findSet(id[a]); // 查找的同时提高节点的高度
// }
//
// bool isSameSet(int a, int b)
// {
// int x = findSet(a); // 查找a的组号
// int y = findSet(b); // 查找b的组号
//
// return x == y; // 判断组号是否相同
// }
//
// void Union(int a, int b)
// {
// int x = findSet(a);
// int y = findSet(b);
//
// if (x != y)
// {
// if (size[x] < size[y])
// {
// id[x] = y;
// size[y] += size[x];
// }
// else
// {
// id[y] = x;
// size[x] += size[y];
// }
//
// cnt--; // 合并后组的数量-1
// }
// }
//
//private:
// int *id;
// int *size;
// int cnt;
//};
//
//
//
//bool isInvalidInput = false;
//
//bool isNum(char ch)
//{
// return (ch >= '0' && ch <= '9');
//}
//
//int convert(char *pchar, bool isMinus)
//{
// long long res = 0;
// int flag = (isMinus == true ? -1 : 1);
// while (*pchar != '\0')
// {
// if (!isNum(*pchar))
// {
// res = 0;
// break;
// }
//
// res = res * 10 + flag * (*pchar - '0');
//
// if ((!isMinus && res > 0x7fffffff) || (isMinus && res < (signed int)0x80000000))
// {
// res = 0;
// break;
// }
//
// pchar++;
// }
//
// if (*pchar == '\0')
// isInvalidInput = false;
//
// return (int)res;
//}
//
//int StrToInt(char *str)
//{
// isInvalidInput = true;
//
// if (str == NULL || *str == '\0')
// return 0;
//
// char *pchar = str;
// bool isMinus = false;
// if (*pchar == '+')
// pchar++;
// else if (*pchar == '-')
// {
// isMinus = true;
// pchar++;
// }
//
// int num = 0;
// if (isNum(*pchar))
// {
// num = convert(pchar, isMinus);
// }
//
// return num;
//}
//
//
main.c
最新推荐文章于 2022-03-12 16:39:40 发布