目录
1、简化Unix路径
/*********************leetCode72简化Unix路径*****************/
#include <iostream>
#include <string>
#include <stack>
using namespace std;
string simplifyPath(string path)
{
if (path.empty())
return "";
stack<string> ss;
for (int i = 0; i < path.size();)
{
//跳过斜线‘/'
while (i < path.size() && '/' == path[i])
++i;
string s = "";
while (i < path.size() && path[i] != '/')
s += path[i++];
//如果是".."则弹出栈,否则入栈
if (".." == s && !ss.empty())
ss.pop();
else if (s != "" && s != "." && s != "..")
ss.push(s);
}
if (ss.empty())
return "/";
string s = "";
while (!ss.empty())
{
s = "/" + ss.top() + s;
ss.pop();
}
return s;
}
int main()
{
//path = "/home/", => "/home"
//path = "/a/./b/../../c/", => "/c"
//string path = "/home/";
string path = "/a/./b/../../c/";
cout << simplifyPath(path) << endl;
return 0;
}
2、石块向下掉落
/********************石块向下掉落******************/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void stoneFalling(vector<string> &vec)
{
int N, M;
cin >> N >> M;
//vector<string> vec;
for (int i = 0; i < N; i++)
{
string s;
cin >> s;
vec.push_back(s);
}
for (int j = 0; j < M; j++)
{
int i = N - 1;
for (; i >= 0; i--)
{
if (vec[i][j] != 'x')
vec[i][j] = '.';
else
break;
}
int k = i - 1;
for (int m = i - 1; m >= 0; m--)
{
if (vec[m][j] == 'o' && vec[k][j] == '.')
{
vec[k--][j] = vec[m][j];
vec[m][j] = '.';
}
else if (vec[m][j] == 'x')
k = m - 1;
else if (vec[k][j] == 'o')
{
k--;
}
}
}
for (int i = 0; i < N; i++)
{
cout << vec[i] << endl;
}
}
int main()
{
vector<string> P = { "ooxo", "oox.", ".x.o", "xooo"};
stoneFalling(P);
return 0;
}
3、最大连续子数组
/*********************最大连续子数组********************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getMaxConArray(vector<int> & array)
{
if (array.empty())
return 0;
int len = array.size();
int sum = array[0];
int thisSum = array[0];
for (int i = 1; i < len; ++i)
{
thisSum = max(thisSum + array[i], array[i]);
if (sum < thisSum)
sum = thisSum;
}
return sum;
}
int main()
{
vector<int> A = { 1, -2, 3, 5, -3, 2 }; //输出8
vector<int> B = { 0, -2, 3, 5, -1, 2 }; //输出9
vector<int> C = { -9,