1# 刷题记录
自己刷题一些积累,没啥用,不要看
树相关
//数的结构体定义
typedef struct treeNode{
int val;
struct treeNode *right,*left;
treeNode():val(),right(NULL),left(NULL){}
}*tree;
//创建树
void createTree(tree &T) {
int val;
cin >> val;
if (-1 == val)
T = NULL;
else {
T = new treeNode;
T->val = val;
createTree(T->left);
createTree(T->right);
}
}
//层次遍历 并输出层高
void levelOrder(tree root) {
if (NULL == root)
return;
int curLevelCount = 1, nextLevelCount = 0,level=0;
queue<tree> q;
q.push(root);
treeNode* curPtr = NULL;
while (!q.empty()) {
curPtr = q.front();
q.pop();
cout << curPtr->val<<" ";
curLevelCount--;
if (NULL != curPtr->left) {
q.push(curPtr->left);
nextLevelCount++;
}
if (NULL != curPtr->right) {
q.push(curPtr->right);
nextLevelCount++;
}
if (0 == curLevelCount) {
level++;
curLevelCount = nextLevelCount;
nextLevelCount = 0;
}
}
cout << level;
}
//交换左右节点 节点没有实质改变
void changeRL(tree root) {
queue<tree> q;
q.push(root);
treeNode* cur = NULL;
while (!q.empty()) {
cur = q.front();
q.pop();
cout << cur->val<<" ";
if (cur->right != NULL)
q.push(cur->right);
if (cur->left != NULL)
q.push(cur->left);
}
}
//中序遍历,先序后序以此类推
void midOrder(tree T) {
if (T) {
cout << T->val<<" ";
midOrder(T->left);
midOrder(T->right);
}
}
int main() {
int n=7;
tree root;
createTree(root);
/*midOrder(root);*/
levelOrder(root);
cout << endl;
changeRL(root);
return 0;
}
非递归先序遍历
void treeNodeSumEqualValuePath(Tree T,int value) {
if (T == NULL) {
cout << "input invalid" << endl;
return;
}
int sum=0;
vector<Tree> s;
Tree cur = T;
while (cur || !s.empty()) {
while (cur) {
cout << cur->val;
s.push_back(cur);
cur = cur->lChild;
}
/*if (sum == value) {
auto it = s.begin();
for (; it != s.end(); it++)
cout << (*it)->val << " ";
}*/
cur = s.back();
s.pop_back();
cur = cur->rChild;
}
}
链表
//链表的插入 以插入学生成绩升序为例
typedef struct Student {
int score;
int stu_id;
struct Student* next;
Student(int sco, int id) :score(sco), stu_id(id),next(NULL){};
}*Class;
int main() {
int stuNum;
cin >> stuNum;
Student* c = new Student(0,0);
Student* ptr=NULL;
int score, stuID;
cin >> stuID >> score;
Student* stu = new Student(score, stuID);
c->next = stu;
ptr = c->next;
for (int i = 1; i < stuNum; i++) {
cin >> stuID >> score;
Student* stu = new Student(score, stuID);
if (score >= ptr->score) {
ptr->next = stu;
ptr = stu;
}
else {
Student* temp;
temp = c;
while (temp != ptr) {
if (score < temp->next->score) {
stu->next = temp->next;
temp->next = stu;
break;
}
temp = temp->next;
}
}
}
for (Student* p = c->next; p != NULL; p = p->next) {
cout << p->stu_id <<" "<< p->score<<endl;
}
return 0;
}
智能指针实现
//智能指针实现
template<typename SmartPointer, typename Type>
class PtrCount {
friend SmartPointer;
PtrCount(Type* _p):p(_p),use(1){}
~PtrCount()(delete p;)
Type* p;
size_t use;
};
template<typename Type>
class SmartPtr {
public:
SmartPtr(Type* p);
PtrCount(new PtrCount<SmartPtr>, Type > (p)) {};
SmartPtr(const SmartPtr& orig) :ptrCnt(orig.ptrCnt) {
++ptrCnt->use;
}
SmartPtr& operator=(const SmartPtr& rhs) {
++rhs.ptrCnt->use;
if (--ptrCnt->use == 0)
delete ptrCnt;
ptrCnt = rhs.ptrCnt;
return *this;
}
--SmartPtr() {
if (--ptrCnt->use == 0)
delete ptrCnt;
}
private:
PtrCount<SmartPtr, Type>* ptrCnt;
};
剑指offer
第二题 单例模式
//单例模式 懒汉式——第一次用到实例的时候才去实例化(不好,单线程适用)
class Singleton {
private:
Singleton() {}//私有构造函数
static Singleton* instance;//指针方面实例化
public:
static Singleton* getInstance() {
if (instance == NULL)
instance = new Singleton();
return instance;
}
};
//单例模式 懒汉式——第一次用到实例的时候才去实例化 加锁(多线程适用)
class Singleton2 {
private:
Singleton2() {}//私有构造函数
static Singleton2* instance2;//指针方面实例化
public:
static Singleton2* getInstance() {
if (instance2 == NULL) {
lock();//加锁
instance2 = new Singleton2();
unlock();//解锁
}
return instance2;
}
};
//单例模式 饿汉式——类定义时就有 加锁(多线程适用)
class SingletonH {
private:
SingletonH() {}
public:
static SingletonH* getInstance() {
static SingletonH instance;
return &instance;
}
};
第三题 任意一个重复的数字
int main() {
//重复的数字 o(n)解法
int n;
cin >>n;
int* arr =(int*)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++) {
arr[i] = 0;
}
for (int i = 0; i < n; i++) {
int number;
cin >> number;
if (0 == arr[number])
arr[number] = 1;
else {
cout << number;
break;
}
}
free(arr);
return 0;
//o(1)解法
int n;
cin >> n;
int* arr =(int*)malloc(sizeof(int)*n);
for (int i = 0; i < n; i++) {
int number;
cin >> number;
arr[i] = number;
}
for (int i = 0; i < n; i++) {
while (i != arr[i]) {
if (arr[i] == arr[arr[i]]) {
cout << arr[i];
break;
}
int temp;
temp = arr[arr[i]];
arr[arr[i]] = arr[i];
arr[i] = temp;
}
}
free(arr);
}
//不允许改变数组空间O(1)解法
#include<iostream>
using namespace std;
int countRange(int start, int end, int arr[],int length) {
int count = 0;
for (int i = 1; i <= length; i++) {
if ( arr[i]>=start&&arr[i]<=end)
count++;
}
return count;
}
int count(int left,int right,int arr[],int length) {
int mid = (left + right) / 2;
if (countRange(left, right, arr, length) == 0||arr==NULL)
return -1;//没有重复值
while (left!=right) {
if (countRange(left, mid, arr, length) > (mid - left+1)) {
right = mid;
}
else if (countRange(mid + 1, right, arr, length) > (right - mid)) {
left = mid+1;
}
mid = (left + right) / 2;
}
return left;
}
int main() {
int length;
cin >> length;
int* a = (int*)malloc(sizeof(int) * length);
for (int i = 1; i <=length; i++) {
int number;
cin >> number;
a[i] = number;
}
cout << count(1,length-1,&a[0],length);
}
第四题 二维数组查找数
#include<iostream>
using namespace std;
bool t_dArrayFind(int w_f, int* arr, int col, int row) {
int r=0, c=col-1;
if (arr == NULL)
return false;
while (1) {
int index = r * col + c;
if (c<0||r>(row-1))
break;
if (arr[index] == w_f)
return true;
else if (arr[index] > w_f) {
c--;
}
else {
r++;
}
}
return false;
}
int main() {
int* a;
int col, row;
cin >> col >> row;
a = (int*)malloc(sizeof(int)*(col*row));
for (int i = 0; i < col * row; i++)
cin >> a[i];
int want_Find;
cin >> want_Find;
cout << t_dArrayFind(want_Find, &a[0], col, row);
return 0;
}
第五题 替换空格
//比较笨的做
#include<iostream>
using namespace std;
char* changeSpace(char *s,int &length) {
for (int i = 0; i < length; i++) {
if (s[i] == ' ') {
length += 2;
for (int j = length - 1; j > i; j--) {
if((j-2)>=0)
s[j] = s[j - 2];
}
s[i] = '%';
s[i + 1] = '2';
s[i + 2] = '0';
i += 2;
}
}
return &s[0];
}
int main() {
char* s,*p;
s = new char[100];
cin.getline(s,100);
int length;
for (length = 0; length < 100; length++) {
if ('\0'==s[length])
break;
}
p = changeSpace(s, length);
for (int i = 0; i < length; i++)
cout << p[i];
return 0;
}
//聪明做法快慢指针
#include<iostream>
using namespace std;
char* repalceSpace(char* s, int numSpace, int& len) {
char* front, * back;
front = &s[len - 1];
back = &s[len + numSpace * 2 - 1];
while (front != &s[0]) {
if (' ' == *front) {
front--;
*back = '0';
back--;
*back = '2';
back--;
*back = '%';
back--;
}
else {
*back = *front;
back--;
front--;
}
}
len = len + numSpace * 2 - 1;
return &s[0];
}
int main() {
char* s,*p;
s = new char[100];
cin.getline(s, 100);
int len=0, numSpace = 0;
for (; len < 100; len++) {
if (' ' == s[len])
numSpace++;
if ('\0' == s[len])
break;
}
len++;
p = repalceSpace(s, numSpace, len);
for (int i = 0; i < len; i++) {
cout << p[i];
}
return 0;
}
第六题 链表反转(可以改变链表结构)
//三指针吧
#include<iostream>
using namespace std;
struct Node {
int val;
struct Node* next;
Node(int x) :val(x), next(NULL) {}
};
int main() {
Node* L;
int n;
cin >> n;
int val;
cin >> val;
L = new Node(val);
Node* front, * back,*p;
front =p= L;
//建立链表
for (int i = 1; i < n; i++) {
cin >> val;
Node* temp = new Node(val);
p->next = temp;
p = p->next;
}
//反转链表
Node* tmp;
tmp = new Node(0);
back = front->next;
while (true) {
if (front == L) {
tmp->next = front;
front = back;
back = front->next;
front->next = tmp->next;
tmp->next->next = NULL;
}
else {
tmp->next = front;
front = back;
back = front->next;
front->next = tmp->next;
if (back == NULL)
break;
}
}
L = front;
Node* it;
it = L;
while (it != NULL) {
cout << it->val << " ";
it = it->next;
}
//输出
return 0;
}
//更简单的 头插法
Node* reverseList(Node* L) {
if (L == NULL)
return NULL;
Node* head=new Node(0);
Node* p,*tail;
p = L;
tail = NULL;
while (p) {
Node* cur;
cur = p->next;
p->next = head->next;
head->next = p;
p =cur;
}
return head->next;
}
第七题 重建二叉树(前序中序建立树)
#include<iostream>
using namespace std;
typedef struct BinaryTreeNode {
int m_nValue;
BinaryTreeNode* m_pLeft, * m_pRight;
}*tree;
void createBinartTree(tree &T,int *preOrderStart,int *preOrderEnd,int *midOrderStart,int *midOrderEnd) {
T = new BinaryTreeNode;//递归终止条件
T->m_nValue = preOrderStart[0];
if (preOrderStart == preOrderEnd) {
if (midOrderStart == midOrderEnd && *preOrderStart == *midOrderStart) {
T->m_pLeft = T->m_pRight = NULL;
}
else
throw exception("invalid input");
}
else
{
int* root;
for (root = midOrderStart; root <= midOrderEnd; root++)
if (*root == *preOrderStart)
break;
//如果子树里找不到子树的根,说明有问题
if (root == midOrderEnd && *root != *preOrderStart)
throw exception("invalid input");
int leftLength = root - midOrderStart;//长度少算一位,因为指针坐标从0起
int* leftChild = preOrderStart + leftLength;
if (leftLength > 0)
createBinartTree(T->m_pLeft, preOrderStart + 1, leftChild, midOrderStart, root - 1);
else if(leftLength <= 0)
T->m_pLeft = NULL;
if (leftLength < (preOrderEnd - preOrderStart)) {
//说明有右子树
createBinartTree(T->m_pRight, leftChild + 1, preOrderEnd, root + 1, midOrderEnd);
}
else if (leftLength >= (preOrderEnd - preOrderStart))
T->m_pRight = NULL;
}
}
void prePrintTree(tree T) {
if (T != NULL) {
cout << T->m_nValue << " ";
prePrintTree(T->m_pLeft);
prePrintTree(T->m_pRight);
}
}
int main() {
int n;
int* preOrder, * midOrder;
cin >> n;
preOrder = (int*)malloc(sizeof(int) * n);
midOrder = (int*)malloc(sizeof(int) * n);
if (n == 0) {
cout << "tree is null";
}
else {
for (int i = 0; i < n; i++)
cin >> preOrder[i];
for (int i = 0; i < n; i++)
cin >> midOrder[i];
}
tree root;
createBinartTree(root, preOrder, preOrder+n-1,midOrder,midOrder+n-1);
prePrintTree(root);
return 0;
}
第八题 二叉树下一个节点
#include<iostream>
#include<vector>
using namespace std;
typedef struct Node {
int val;
Node* rChild, * lChild;
}*tree;
void createTree(tree &T) {
int val;
cin >> val;
if (val == -1) {
T = NULL;
}
else {
T = new Node;
T->val = val;
createTree(T->lChild);
createTree(T->rChild);
}
}
void midOrder(tree T,vector<int> &order) {
if (T != NULL) {
midOrder(T->lChild, order);
order.push_back(T->val);
midOrder(T->rChild, order);
}
}
int main() {
tree T;
createTree(T);
//方法1 笨方法
vector<int> order;
midOrder(T,order);
int f_val;
int IF_FIND=0;
cin >> f_val;
for (int i = 0; i < order.size(); i++) {
if (order[i] == f_val) {
IF_FIND = 1;
if((i + 1)<order.size())
cout << order[i + 1];
else {
cout << "下一个值不存在"<<endl;
}
}
}
if (0 == IF_FIND)
cout << "输入的值不存在" << endl;
return 0;
}
含有指向父节点指针的做法
#include<iostream>
#include<queue>
using namespace std;
typedef struct Node {
int val;
Node* lChild=NULL, * rChild=NULL, *father=NULL;
}*tree;
//创建含有指向父节点的树
void createTree(tree& T, tree& Tfather) {
int val;
cin >> val;
if (val == -1)
T = NULL;
else {
T = new Node;
T->val = val;
if (Tfather != NULL)
T->father = Tfather;
createTree(T->lChild,T);
createTree(T->rChild,T);
}
}
void findNextNode(tree T,int val) {
//层次遍历找到节点
Node* pCur = T;
Node* pFind;
pFind =new Node;
queue<Node*> Tree;
Tree.push(T);
while (!Tree.empty()) {
if (Tree.front()->val == val) {
pFind = Tree.front();
break;
}
else {
pCur = Tree.front();
Tree.pop();
if(pCur->lChild!=NULL)
Tree.push(pCur->lChild);
if (pCur->rChild != NULL)
Tree.push(pCur->rChild);
}
}
Node* pNext;
pNext = pFind;
if (pFind->rChild != NULL) {
pNext = pNext->rChild;
while (pNext->lChild != NULL)
{
pNext = pNext->lChild;
}
cout << pNext->val << endl;
}
else {
if (pNext->father == NULL) {
cout << "none";
}
else if (pNext->father->lChild == T)
{
cout << pNext->father->val;
}
else {
if(pNext->father!=NULL)
pNext = pNext->father;
while ((pNext->father!=NULL)&&(pNext->father->rChild == pNext)) {
pNext = pNext->father;
if (pNext->father == NULL) {
pNext = NULL;
cout << "none";
break;
}
}
if (pNext!=NULL&&pNext->father != NULL)
cout << pNext->father->val;
}
}
}
int main() {
tree T=new Node;
T->father = NULL;
createTree(T,T->father);
int val;
cin >> val;
findNextNode(T,val);
return 0;
}
第九题 用两个栈实现双向队列
#include<iostream>
#include<string>
#include<stack>
using namespace std;
template<typename T>
class SQueue {
public:
SQueue(){}
void appendTail(T val);
void deletHead();
void printQueue();
virtual ~SQueue() {};
private:
stack<T> s1, s2;
};
template<typename T>void SQueue<T>::appendTail(T val){
s1.push(val);
}
template<typename T>void SQueue<T>::deletHead() {
if (s2.empty() && !s1.empty()) {
while (s1.size() > 0)
{
s2.push(s1.top());
s1.pop();
}
}
else if (s1.empty() && s2.empty())
cout << "queue is empty, delete error!" << endl;
if (!s2.empty())
s2.pop();
}
template<typename T>
void SQueue<T>::printQueue() {
while (s2.size()>0 ) {
cout << s2.top()<<" ";
s2.pop();
}
while (s1.size()>0)
{
s2.push(s1.top());
s1.pop();
}
while (s2.size() > 0) {
cout << s2.top()<<" ";
s2.pop();
}
}
int main() {
SQueue<int> q;
char order;
int over = 0;
while (cin >> order) {
switch (order) {
case 'i':
int val;
cin >> val;
q.appendTail(val);
break;
case 'd':
q.deletHead();
break;
case 'p':
q.printQueue();
over = 1;
break;
}
if (over == 1)
break;
}
return 0;
}
第九题附加题 两个队列实现栈
#include<iostream>
#include<string>
#include<queue>
using namespace std;
template<typename T>
class QStack {
public:
QStack(){}
void deletTop();
void appendTop(T val);
void printStack();
virtual ~QStack(){}
private:
queue<T> q1, q2;
};
template<typename T>
void QStack<T>::appendTop(T val) {
if(q2.empty())
q1.push(val);
else {
q2.push(val);
}
}
template<typename T>
void QStack<T>::deletTop() {
if (!q1.empty() && q2.empty()) {
while (q1.size() > 1) {
q2.push(q1.front());
q1.pop();
}
q1.pop();
}
else if (q1.empty() && !q2.empty()) {
while (q2.size() > 1) {
q1.push(q2.front());
q2.pop();
}
q2.pop();
}
else
cout << "stack is empty,delete error" << endl;
}
template<typename T>
void QStack<T>::printStack() {
int size;
if (!q1.empty())
size = q1.size();
else
size = q2.size();
T *temp = (T*)malloc(sizeof(T) * size + 1);
for (int i = 0; i < size; i++)
{
if (!q1.empty()) {
temp[i] = q1.front();
q1.pop();
}
else if (!q2.empty()) {
temp[i] = q2.front();
q2.pop();
}
}
for (int i = size - 1; i >= 0; i--)
cout << temp[i] << " ";
}
int main() {
QStack<int> s;
char order;
int over = 0;
while (cin >> order) {
switch (order) {
case 'i':
int val;
cin >> val;
s.appendTop(val);
break;
case 'd':
s.deletTop();
break;
case 'p':
s.printStack();
over = 1;
break;
}
if (over == 1)
break;
}
return 0;
}
斐波那契数列
#include<iostream>
using namespace std;
//递归解法
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
//循环解法
int fibonacci2(int n) {
int next;
if (n == 0)
next= 0;
else if (n == 1)
next= 1;
else {
int front = 0;
int last = 1;
for (int i = 0; i < n-1; i++) {
next = front + last;
front = last;
last = next;
}
}
return next;
}
int main() {
int n;
cin >> n;
cout << fibonacci(n)<<endl;
cout << fibonacci(n)<<endl;
return 0;
}
快排
#include<iostream>
using namespace std;
void swap(int& a, int& b) {
int temp;
temp = a;
a = b;
b = temp;
}
void sort(int* array,int start, int end) {
int first = start,last=end;
int key = array[start];
if (start >= end)
return;
while (first != last) {
while ((array[last] > key)&&(last>first))
last--;
if (last > first) {
array[first] = array[last];
first++;
}
while((array[first] < key)&&(last>first))
first++;
if (last > first) {
array[last] = array[first];
last--;
}
}
array[last] = key;
sort(&array[0], start, last-1);
sort(&array[0], last+1, end);
}
int main() {
int* a = (int*)malloc(sizeof(int) * 10);
for (int i = 0; i < 10; i++)
cin >> a[i];
sort(&a[0], 0, 9);
for (int i = 0; i < 10; i++)
cout<< a[i]<<" ";
return 0;
}
第十一题 旋转数组最小数 折半查找
#include<iostream>
using namespace std;
void halfSearch(int *a,int length) {
if (a[0] < a[length]) {
cout << a[0];
return;
}
int first = 0,second = length;
int mid;
while (first!=(second-1))
{
mid = (first + second) / 2;
if ((a[mid] != a[first] )&& (a[mid] != a[second])&&(a[first] != a[second])) {
if (a[mid] >= a[first])
first = mid;
else if (a[mid] < a[second])
second = mid;
}
else {
for (int i = first; i < second-1; i++) {
if ((a[i + 1] <=a[first]&&(a[i]>=a[first]))) {
cout << a[i + 1];
return;
}
}
}
}
cout << a[second] << endl;
}
int main() {
int n;
cin >> n;
int* a = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
cin>>a[i];
}
halfSearch(&a[0], n-1);
return 0;
}
第十二题 矩阵中的路径
#include<iostream>
#include<string>
using namespace std;
bool findChar(const char* martrix, const string str, int& strIndex, int row, int col, const int rows, const int cols, bool* visited) {
if (str[strIndex] == '\0')
return true;
bool hasPath = false;
if ((martrix[row * cols + col] == str[strIndex]) && !visited[row * cols + col] && col < cols &&col>=0&& row < rows&&row>=0) {
strIndex++;
visited[row * cols + row] = true;
hasPath = findChar(&martrix[0], &str[0], strIndex, row - 1, col, rows, cols, &visited[0]) || findChar(&martrix[0], &str[0], strIndex, row + 1, col, rows, cols, &visited[0]) || findChar(&martrix[0], &str[0], strIndex, row, col + 1, rows, cols, &visited[0]) || findChar(&martrix[0], &str[0], strIndex, row, col - 1, rows, cols, &visited[0]);
if (!hasPath) {
visited[row * cols + col] = false;
--strIndex;
}
}
return hasPath;
}
bool ifHaveStr(const char* martrix, string str, const int rows, const int cols) {
if ((martrix == NULL) || (str.size() == 0)||(rows<0)||(cols<0))
return false;
int strIndex = 0;
//找到一个开始的点
bool* visited = (bool*)malloc(sizeof(bool) * (rows * cols));
memset(visited,0,rows*cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (findChar(&martrix[0], &str[0], strIndex, i, j, rows, cols, &visited[0])) {
return true;
}
}
}
free(visited);
return false;
}
int main() {
int cols, rows;
cin >> cols >> rows;
char* martix = (char*)malloc(sizeof(char) * (cols * rows));
for (int i = 0; i < cols * rows; i++) {
cin >> martix[i];
}
string str;
cin >> str;
cout << ifHaveStr(&martix[0], str, rows, cols);
return 0;
}
题
** 有个旅行团要去不同的地点接团员,有n个地点n辆车,每一辆车载客人数不一样,每个地点上车的人数也不一样,现在一个车只去一个地点载客,问有多少种安排的方法 (车的容量必须大于等于该地点的团员)**
int n;
vector<int> citys;
vector<int> cars;
int temp=0;
int num = 1;
cin >> n;
for (int i = 0; i < n; i++) {
int city;
cin >> city;
citys.push_back(city);
}
for (int i = 0; i < n; i++) {
int car;
cin >> car;
cars.push_back(car);
}
sort(cars.begin(),cars.end());
sort(citys.begin(), citys.end());
if (cars[0] < citys[0]) {
cout << 0;
}
else {
for (int i = 0; i < n; i++) {
int j = i;
while (j < n) {
if (citys[j] <= cars[i]) {
temp++;
}
j++;
}
num = num * temp;
temp = 0;
}
cout << num;
}
** 合并有序链表
#include<iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode* next;
};
//递归解法
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
ListNode* mergeNode;
if (l1->val < l2->val) {
mergeNode = l1;
mergeNode->next = mergeTwoLists(l1->next, l2);
}
else {
mergeNode = l2;
mergeNode->next = mergeTwoLists(l2->next, l1);
}
return mergeNode;
}
int* change(int* a, int* b) {
a = b;
return a;
}
int main() {
ListNode* l1;
ListNode* l2;
int n;
cin >> n;
l1 = new ListNode;
l2 = new ListNode;
ListNode* p1, * p2;
p1 = l1;
p2 = l2;
int val;
cin >> val;
l1->val = val;
for (int i = 1; i < n; i++) {
cin >>val;
ListNode* temp = new ListNode;
temp->val = val;
temp->next = NULL;
p1->next = temp;
p1 = p1-> next;
}
cin >> val;
l2->val = val;
for (int i = 1; i < n; i++) {
cin >> val;
ListNode* temp = new ListNode;
temp->val = val;
temp->next = NULL;
p2->next = temp;
p2 = p2->next;
}
ListNode* answer;
answer=mergeTwoLists(l1, l2);
for (int i = 0; i < 2 * n; i++) {
cout << answer->val;
answer = answer->next;
}
return 0;
}
加密 给出密钥,密钥和其他字母生成编码表,给出单词加密
#include<iostream>
#include<string>
#include<unordered_set>
using namespace std;
int main() {
char* keyword;
keyword = new char[26];
unordered_set<char> temp;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
temp.emplace(s[i]);
}
int j = 0;
for (auto c = temp.begin(); c != temp.end(); ++c)
{
keyword[j] = *c;
j++;
}
for (char c = 'A'; c <= 'Z'; c++) {
if (temp.count(c) == 0) {
keyword[j] = c;
j++;
}
}
for (int i = 0; i < 26; i++)
cout << keyword[i];
string word;
cin >> word;
for (int i = 0; i < word.size(); i++) {
int index = word[i] - 'A';
cout << keyword[index];
}
cout << endl;
return 0;
}
包含min函数的栈
#include<iostream>
#include<stack>
using namespace std;
template<typename T>
class StackWithMin {
private:
T min;
stack<T> min_stack;
stack<T> stack;
public:
StackWithMin(){}
virtual ~StackWithMin(){}
T getMinValue();
void pop();
void push(T val);
};
template<typename T>
void StackWithMin<T>::push(T val) {
if (stack.empty())
min = val;
stack.push(val);
if (val <= min)
min_stack.push(val);
min = min_stack.top();
}
template<typename T>
void StackWithMin<T>::pop() {
if (stack.empty()) {
cout << "StackWithMin is error" << endl;
return;
}
if (stack.top() == min) {
stack.pop();
min_stack.pop();
}
min = min_stack.top();
}
template<typename T>
T StackWithMin<T>::getMinValue() {
if (stack.empty()) {
cout << "StackWithMin is empty" << endl;
return NULL;
}
return min;
}
int main() {
StackWithMin<int> S;
int length;
cin >> length;
for (size_t i = 0; i < length; i++)
{
int val;
cin >> val;
S.push(val);
}
cout<<S.getMinValue();
return 0;
}