Day 12

1.删除公共字符 (pass)

删除公共字符
哈希、字符串、枚举

1.1 解析

在这里插入图片描述

1.2 代码

#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    //将第二个字符串存放hash
    // unordered_set<char> hash;
    // for(auto& e:s2)
    //     hash.insert(e);
    bool hash[300]={0};
    for(char ch:s2) hash[ch]=true;
    string tmp;
    int n=s1.size();
    for(int i=0;i<n;i++)
    {
        // if(!hash.count(s1[i]))
        //     tmp+=s1[i];
        if(!hash[s1[i]]) tmp+=s1[i];
    }
    cout<<tmp;
    return 0;
}

2.两个链表的第一个公共结点 (pass)

两个链表的第一个公共结点
链表

2.1 解析

在这里插入图片描述

2.2 代码

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* head1, ListNode* head2) {
        //1.统计2个链表的长度
		int len1=0,len2=0;
		ListNode* cur=head1;
		while(cur)
		{
			len1++;
			cur=cur->next;
		}
		cur=head2;
		while(cur)
		{
			len2++;
			cur=cur->next;
		}
		//2.让长的链表先走
		ListNode* maxhead=head1,*minhead=head2;
		if(len2>len1)
		{
			maxhead=head2;
			minhead=head1;
		}
		int tmplen=abs(len1-len2);
		while(tmplen--)
		{
			maxhead=maxhead->next;
		}
		//3.让2个链表一起走
		while(maxhead!=minhead)
		{
			maxhead=maxhead->next;
			minhead=minhead->next;
		}
		return maxhead;
    }
};

3. mari和shiny

mari和shiny
多状态线性dp

3.1 解析

在这里插入图片描述

3.2 代码

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    long long s=0,h=0,y=0;
    int n=0;
    cin>>n;
    string str;
    cin>>str;
    //初始化
    s=str[0]=='s'?1:0;
    //2.填表
    for(int i=1;i<n;i++)
    {
        if(str[i]=='s')s++;
        else if(str[i]=='h') h+=s;
        else if(str[i]=='y') y+=h;
    }
    cout<<y;
    return 0;
}
/*int main()
{
    int n=0;
    cin>>n;
    string str;
    cin>>str;
    //1.创建dp表
    vector<long long> s(n);
    vector<long long> h(n);
    vector<long long> y(n);
    //2.初始化
    s[0]=str[0]=='s'?1:0;
    //3.填表
    for(int i=1;i<n;i++)
    {
        s[i]=str[i]=='s'?s[i-1]+1:s[i-1];
        h[i]=str[i]=='h'?s[i-1]+h[i-1]:h[i-1];
        y[i]=str[i]=='y'?h[i-1]+y[i-1]:y[i-1];
    }
    cout<<y[n-1];
    return 0;
}*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值