#include <iostream>
#include <vector>
#include <stack>
using namespace std;
//有序数组去重
int order(vector<int> a)
{//0-slow为去重后有序数组
int size=a.size();
int slow(0),fast(1);
while(fast<size)
{
if(a[slow]!=a[fast])
{
slow++;
a[slow]=a[fast];
}
fast++;
}
return slow+1;
}
//有序链表去重
struct Node{
int val;
Node* next;
Node(int _val):val(_val),next(nullptr){}
};
Node* order(Node* head)
{
if(head==nullptr) return head;
Node* slow=head;
Node* fast=head->next;
while(fast)
{
if(slow->val!=fast->val)
{
slow->next=fast;
slow=slow->next;
}
fast=fast->next;
}
return head;
}
//判断回文字符串
string fun(string &st,int l,int r)
{
while (l>=0 && r<st.size() && st[l]==st[r]) {
l--;
r++;
}
return st.substr(l+1,r-l-1);
}
string huiwen(string &st)
{
string res,s1,s2;
for(size_t i=0;i<st.size();++i)
{
s1=fun(st,i,i);//奇数个数字符串
s2=fun(st,i,i+1);//偶数个数字符串
res=res.size()>s1.size()?res:s1;
res=res.size()>s2.size()?res:s2;
}
return res;
}
//判断(){}[]合法性
char LToR(char c)
{//根据右括号返回左括号
if(c==')') return '(';
if(c==']') return '[';
return '{';
}
bool kuohao(string &s)
{
stack<char> st;
for(size_t i=0;i<s.size();++i)
{
if(s[i]=='(' || s[i]=='[' || s[i]=='{')
{
st.push(s[i]);
}
else
{
if(!st.empty() && LToR(s[i])==st.top() )
{
st.pop();
}
else
{
return false;
}
}
}
return st.empty();//栈为空,才是合法的
}
int main()
{
cout << "Hello World!" << endl;
vector<int> v{0,1,1,2,3,3,4};
cout<<order(v)<<endl;
Node* n1=new Node(0);
Node* n2=new Node(1);
Node* n3=new Node(1);
Node* n4=new Node(2);
Node* n5=new Node(3);
Node* n6=new Node(3);
Node* n7=new Node(4);
n1->next=n2;
n2->next=n3;
n3->next=n4;
n4->next=n5;
n5->next=n6;
n6->next=n7;
Node* res=order(n1);
while(res)
{
cout<<res->val<<" ";
res=res->next;
}
string st="abacd";
cout<<huiwen(st)<<endl;
string s1="([])";
cout<<(kuohao(s1)?"true":"false");
return 0;
}