算法:
目录
判断一个数组是否是递增的
对于一个排序数组,找到给定元素的最小上界(大于它的最小值)
不用加运算实现加法
双栈队列
去除重复数字
链表的交点
动态规划回文子串
链表合并
两个数组交集
字符串去空格
十六进制转十进制
栈实现队列,队列实现栈
求一个集合的所有子集,递归实现,非递归实现(深度优先搜索)
一个数组,里面大多数都是成对的,只有两个数没有成对,求这两个数
求一个栈的pop序是否合法
bool asc(vector& data){
int n = data.size();
for (int i = 0; i < n-1; ++i){
if (data[i]>data[i + 1])
return 0;
}
return 1;
}
//递归版本
bool asc(vector& data,int index){
if (index <= 0)return 1;
return asc(data, index - 1) && data[index - 1] <= data[index];
}
int my_lowbound(vector& data,int num){
int n = data.size();
if (n == 0||(n==1&&data[0]=num)
return left;
else return -1;
}
int my_add(int n1,int n2){
int carry = (n1&n2)<<1;
int sum = n1^n2;
while (carry!=0){
n1 = sum;
n2 = carry;
sum = n1^n2;
carry = (n1&n2)<<1;
}
return sum;
}
class stack_queue{
stack s1;
stack s2;
public:
void push(int num){
s1.push(num);
}
int pop(){
if (s1.empty() && s2.empty()){
return -1;
}
else if (!s2.empty()){
int temp = s2.top();
s2.pop();
return temp;
}
else{
while (!s1.empty()){
s2.push(s1.top());
s1.pop();
}
int temp = s2.top();
s2.pop();
}
}
};
int myunique(vector&data){
sort(data.begin(), data.end());
int n = data.size();
int tail = 0;
for (int i = 1; i < n; ++i){
if (data[tail] != data[i]){
data[++tail] = data[i];
}
}
return tail;
}
listnode* meetpoint(listnode* head1,listnode* head2){
if (head1 == nullptr || head2 == nullptr)return nullptr;
int len1 = 0;
listnode* cur1 = head1;
while (cur1!=nullptr){
len1++;
cur1 = cur1->next;
}
int len2 = 0;
listnode* cur2 = head2;
while (cur2 != nullptr){
len2++;
cur2 = cur2->next;
}
cur1 = head1;
cur2 = head2;
if (len1 > len2){
for (int i = 0; i < len1 - len2; ++i){
cur1 = cur1->next;
}
}
else{
for (int i = 0; i < len2 - len1; ++i){
cur2 = cur2->next;
}
}
while (cur1!=nullptr&&cur2!=nullptr&&cur1 != cur2){
cur1 = cur1->next;
cur2 = cur2->next;
}
return cur1;
}
思路:向外扩展,dp[i][j]表示从i开始到j结束的子串是回文子串的长度(不是回文子串长度为0),那么dp[i-1][j+1]=(dp[i][j]!=0,dp[i][j]+2),长度为2的回文串需要特殊处理,dp[i][j]=(a[i]==a[i+1],2);另外这个填表的过程是从对角线开始再到次上对角线,再到次次上对角线最后到右上角即dp[0][n-1]
int huiwen(string& str){
int n = str.size();
vector> dp(n,vector(n,0));
for (int i = 0; i < n; ++i){
dp[i][i] = 1;
}
int result = 0;
int maxi = 0;
int maxj = 0;
for (int len = 2; len <= n; ++len){
for (int i = 0; i < n-1; ++i){
int j = i + len - 1;
if (j>=n)continue;
if (len == 2){
dp[i][j] = str[i] == str[j] ? 2 : 0;
}
if (str[i] == str[j] && dp[i + 1][j - 1]>0){
dp[i][j] = dp[i + 1][j - 1] + 2;
}
result = max(result, dp[i][j]);
if (result == dp[i][j]){
maxi = i;
maxj = j;
}
}
}
for (int i = maxi; i <= maxj; i++){
cout << str[i];
}
return result;
}
int main(){
string str = "aabcddcbc";
cout<
listnode* list_merge(listnode* l1,listnode* l2){
if (l1 == nullptr)return l2;
if (l2 == nullptr)return l1;
listnode* result = nullptr;
if (l1->val < l2->val){
l1->next=list_merge(l1->next, l2);
return l1;
}
else{
l2->next = list_merge(l1, l2->next);
return l2;
}
}
vector jiaoji(vector& array1,vector& array2){
vector result;
sort(array1.begin(), array1.end());
sort(array2.begin(), array2.end());
int n = array1.size();
int m = array2.size();
int i = 0;
int j = 0;
while (i < n&&j < m){
if (array1[i]>array2[j]){
j++;
}
else if (array1[i] < array2[j]){
i++;
}
else{
result.push_back(array1[i]);
i++;
j++;
}
}
return result;
}
void drop_blanck(string& str){
int tail = 0;
for (int i = 0; i < str.size(); ++i){
if (str[i] != ' '){
if (i != tail){
swap(str[tail],str[i]);
}
tail++;
}
}
str[tail] = '\0';
}
int six2ten(string num){
int result = 0;
int j = 0;
for (int i = num.size() - 1;i>=0; --i){
if (num[i] >= '0'&&num[i] <= '9'){
result += (num[i]-'0')*pow(16, j);
j++;
}
else if (num[i] >= 'a'&&num[i] <= 'f'){
result += (num[i] - 'a'+10)*pow(16, j);
j++;
}
else if (num[i] >= 'A'&&num[i] <= 'F'){
result += (num[i] - 'A'+10)*pow(16, j);
j++;
}
else{
return -1;
}
}
return result;
}
class queueformstack{
public:
void push(int x){
if (!q1.empty()){
q1.push_back(x);
}
else{
q2.push_back(x);
}
}
int pop(){
if (q1.empty() && q2.empty()){
cout << "stack empty" << endl;
return -1;
}
else if (q1.empty()){
while (q2.size() > 1){
q1.push_back(q2.front());
q2.pop_front();
}
int temp = q2.front();
q2.pop_front();
return temp;
}
else{
while (q1.size() > 1){
q2.push_back(q1.front());
q1.pop_front();
}
int temp = q1.front();
q1.pop_front();
return temp;
}
}
private:
deque q1;
deque q2;
};
void combine(string& str,int n,int dep,string select){
if (dep == n){
cout << select << endl;
return;
}
combine(str, n, dep + 1,select+str[dep]);
combine(str, n, dep + 1, select );
}
vector fun(vector& data){
vector result;
int temp = 0;
for (int i = 0; i < data.size(); ++i){
temp ^= data[i];
}
if (temp == 0)return result;
int index = 0;
while (temp){
if (temp&(1 << index)){
break;
}
index++;
}
int temp2 = 0;
temp = 0;
int x = 1 << index;
for (int i = 0; i < data.size(); ++i){
if (data[i] & x){
temp^=data[i];
}
else
temp2^=data[i];
}
if (temp != 0)result.push_back(temp);
if (temp2 != 0)result.push_back(temp2);
return result;
}
bool fun(vector& data,vector& popdata){
int n = data.size();
int m = popdata.size();
if (n != m||n==0)return false;
stack s;
int j = 0;
int i = 0;
s.push(data[i]);
i++;
while (j < m){
while (i