其他未完成
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
// 【场景】字符串反转
|
||||
// 编写一个函数 reverseString,接受一个字符串作为参数,并返回该字符串的反转版本。
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string reverseString(string &other)
|
||||
{
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "反转后的字符串 hello 为: " << reverseString("hello") << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// 【场景】向量vector求和
|
||||
// 编写一个函数 sumVector,接受一个整数向量作为参数,并返回向量中所有元素的和。
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int sumVector(vector<int> &vec)
|
||||
{
|
||||
int sum = 0;
|
||||
vector<int>::iterator it = vec.begin();
|
||||
while (it != vec.end())
|
||||
{
|
||||
sum += *it;
|
||||
it++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> v;
|
||||
for (int i = 0; i < 10; i++)
|
||||
v.push_back(i);
|
||||
cout << "向量中所有元素的和为: " << sumVector(v) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// 【场景】双端队列操作
|
||||
// 编写一个程序,演示双端队列(deque)的以下操作: 1)在队列的前面插入一个元素。 2)在队列的后面插入一个元素。 3)从队列的前面删除一个元素。 4)从队列的后面删除一个元素。
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(deque<T> &dq)
|
||||
{
|
||||
// cout << "******************" << endl;
|
||||
typename deque<T>::iterator it = dq.begin();
|
||||
for (; it != dq.end(); it++)
|
||||
{
|
||||
cout << *it << endl;
|
||||
}
|
||||
cout << "------------------" << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
deque<string> dq;
|
||||
dq.push_front("push_front");
|
||||
dq.push_back("push_back");
|
||||
cout << "删除前: " << endl;
|
||||
print(dq);
|
||||
|
||||
dq.pop_front();
|
||||
dq.pop_back();
|
||||
cout << "删除后: " << endl;
|
||||
print(dq);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// 【场景】栈操作
|
||||
// 编写一个程序,演示栈(stack)的以下操作: 1)将一个元素压入栈顶。 2)从栈顶弹出一个元素。 3)获取栈顶元素的值。
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(stack<T> &s)
|
||||
{
|
||||
cout << "当前栈中元素(按出栈顺序)打印如下: " << endl;
|
||||
// 先将栈转存到另一个临时栈中,临时栈的元素顺序和原栈相反
|
||||
// 临时栈用于打印,而不改变原栈的元素顺序
|
||||
stack<T> tmp = s; // 拷贝原来的栈,避免改变原栈
|
||||
while (!tmp.empty())
|
||||
{
|
||||
cout << tmp.top() << " ";
|
||||
tmp.pop();
|
||||
}
|
||||
cout << endl;
|
||||
cout << "------------------" << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
stack<int> stk;
|
||||
stk.push(1);
|
||||
stk.push(2);
|
||||
stk.push(3);
|
||||
stk.push(4);
|
||||
stk.push(5);
|
||||
|
||||
print(stk);
|
||||
|
||||
cout << "从栈顶弹出一个元素: " << stk.top() << endl;
|
||||
stk.pop();
|
||||
print(stk);
|
||||
|
||||
cout << "获取栈顶元素的值: " << endl;
|
||||
cout << stk.top() << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 【场景】字符串拼接
|
||||
// 编写一个函数 concatenateStrings,接受两个字符串作为参数,并返回它们的拼接结果。
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string concatenateStrings(const string &str1, const string &str2)
|
||||
{
|
||||
return str1 + str2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string s1 = "hello";
|
||||
string s2 = "world";
|
||||
s1 = concatenateStrings(s1, " "); // 先拼接一个空格
|
||||
s1 = concatenateStrings(s1, s2); // 再拼接 s2
|
||||
cout << s1 << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 【场景】向量vector查找
|
||||
// 编写一个函数 findElement,接受一个整数向量和一个目标值作为参数,并返回目标值在向量中的索引。
|
||||
// 如果目标值不存在于向量中,则返回 -1。
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int findElement(vector<int> &v, const int &item)
|
||||
{
|
||||
int index = 0;
|
||||
vector<int>::iterator it = v.begin();
|
||||
while (it != v.end())
|
||||
{
|
||||
if (*it == item) // 如果找到了
|
||||
return index; // 返回索引
|
||||
index++; // 索引加一
|
||||
it++; // 迭代器指向下一个元素
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
string print(int item)
|
||||
{
|
||||
if (item == -1)
|
||||
return "目标不存在";
|
||||
else
|
||||
{
|
||||
// to_string() 函数可以将整数转换为字符串
|
||||
// 需要 #include <string> 头文件
|
||||
// 需要 c++11 标准,编译时加上 -std=c++11
|
||||
string s = to_string(item);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5, 4, 6, 7, 8, 9, 10}; // 定义数组
|
||||
vector<int> v1(arr, arr + sizeof(arr) / sizeof(arr[0])); // 将数组转换为向量
|
||||
cout << "5 在目标值向量中的索引为: " << print(findElement(v1, 5)) << endl; // 4
|
||||
cout << "11 在目标值向量中的索引为: " << print(findElement(v1, 11)) << endl; // -1
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// 【场景】双端队列排序
|
||||
// 编写一个程序,演示如何使用双端队列对一组整数进行排序。
|
||||
// 【提示】应用插入排序算法: 从第2个开始,确认当前位置数在前面有序(从小到大或从大到小)队列中的位置。
|
||||
@@ -0,0 +1,62 @@
|
||||
// 【场景】栈应用
|
||||
// 编写一个程序,判断给定的字符串是否是有效的括号序列。例如,对于字符串 "{[()]}",应返回 true;对于字符串 "{[(])}",应返回 false。
|
||||
// 【提示】华为OD的机试题
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool judge(const string &str)
|
||||
{
|
||||
stack<char> stk;
|
||||
int max_depth = 0;
|
||||
|
||||
for (char c : str) // 增强型 for 循环,用于遍历字符串
|
||||
{
|
||||
if (c == '(' || c == '[' || c == '{')
|
||||
{
|
||||
stk.push(c); // 当左括号出现时,入栈
|
||||
}
|
||||
else if (c == ')' || c == ']' || c == '}') // 当碰到右括号时,开始匹配
|
||||
{
|
||||
if (stk.empty()) // 如果栈已经为空,说明没有左括号与之匹配
|
||||
{
|
||||
return false; // 返回无法匹配
|
||||
}
|
||||
|
||||
char top = stk.top(); // 拿出栈顶元素
|
||||
|
||||
if (max_depth < stk.size())
|
||||
max_depth = stk.size();
|
||||
|
||||
stk.pop(); // 弹出栈顶元素
|
||||
|
||||
// 判断本次取出的字符与栈顶元素是否匹配,即左右括号是否匹配,不匹配则返回无效,匹配则继续进行下一轮遍历循环
|
||||
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{'))
|
||||
{
|
||||
return false; // 括号不匹配
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "括号栈的最大深度为: " << max_depth << endl;
|
||||
return stk.empty(); // 栈为空表示括号完全匹配 = 有效 = true = 1,反之,如果栈非空,说明还有未匹配的左括号,返回无效 = false = 0
|
||||
}
|
||||
|
||||
string print(bool flag)
|
||||
{
|
||||
if (flag) // flag 为 true = 1
|
||||
return "有效";
|
||||
else
|
||||
return "无效";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// string str1 = "{[(])}";
|
||||
// string str1 = "{(])}";
|
||||
string str1 = "{[(s[s])s]}[][[[[()]]]]";
|
||||
cout << "括号序列 " << str1 << " 是否有效: " << print(judge(str1)) << endl; // 1
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user