Leetcode进阶之路——Weekly Contest 140

37 篇文章 0 订阅

1078. Occurrences After Bigram

Given words first and second, consider occurrences in some text of the form “first second third”, where second comes immediately after first, and third comes immediately after second.
For each such occurrence, add “third” to the answer, and return the answer.
Example 1:
Input: text = “alice is a good girl she is a good student”, first = “a”, second = “good”
Output: [“girl”,“student”]
Example 2:
Input: text = “we will we will rock you”, first = “we”, second = “will”
Output: [“we”,“rock”]

给定一个字符串,和一个由两个元素组成的字符串数组[first, second]
在原字符串中,找出紧跟着second出现的下一个字符串
按空格分割,一次遍历即可(熟悉数据结构的话可以用istringstream直接按空格分割)

class Solution {
public:
    vector<string> findOcurrences(string text, string first, string second) {
		vector<string> vs, res;
		int fir = 0;
		for (unsigned i = 0; i < text.length(); ++i)
		{
			if (text[i] == ' ')
			{
				vs.emplace_back(text.substr(fir, i - fir));
				fir = i + 1;
			}
            else if(i == text.length() - 1)
            {
                vs.emplace_back(text.substr(fir, i - fir + 1));
				fir = i + 1;
            }
		}
		for (int i = 0; i < vs.size() - 2; ++i)
		{
			if (vs[i] == first && vs[i + 1] == second)
				res.emplace_back(vs[i + 2]);
		}
		return res;
	}
};

1080. Insufficient Nodes in Root to Leaf Paths
这道题… 我就不多说了,第一次这么恨一道题,题意表述不清,而且contest结束之后,原先accept的代码竟然WA了
大致的思路就是dfs找出左右子节点中和的最大值,与limit比较,若比limit小,则置该节点为null,然而比如图1这样一棵树若limit为22,官方的答案竟然如图二,可能是对“任意一条路径”、“子节点”的定义不同,不知道后续是否还会补充
图1图2
1079. Smallest Subsequence of Distinct Characters

Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.
Example 1:
Input: “cdadabcc”
Output: “adbc”
Example 2:
Input: “abcd”
Output: “abcd”
Example 3:
Input: “ecbacba”
Output: “eacb”
Example 4:
Input: “leetcode”
Output: “letcod”

给定一个字符串,找出一个子串使其满足:

  1. 字典序最小
  2. 字符不重复
  3. 包含所有在原串中出现过的字符

递归调用即可,每次找出第一个字典序最小的字符,将除该字符外的其它字符组成新串,进行下一轮查找,直至新串为空

class Solution {
public:
    string smallestSubsequence(string text) {
        if(text.length() == 0) return "";
        map<char, int> m;
        for(char c: text)
        {
            m[c]++;
        }
        int pos = 0;
        char c = 'z' + 1;
        for (int i = 0; i < text.length(); ++i)
        {
            if(text[i] < c) 
            {
                
                c = text[i];
                pos = i;
                //break;
            }
            m[text[i]]--;
            if(m[text[i]] == 0) break;
        }
        c = text[pos];
        string s = "";
        for(int i = pos + 1; i < text.length(); ++i)
            if(text[i] != c) s += text[i];
        return c + smallestSubsequence(s);
    }
};

1081. Letter Tile Possibilities

You have a set of tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make.
Example 1:
Input: “AAB”
Output: 8
Explanation: The possible sequences are “A”, “B”, “AA”, “AB”, “BA”, “AAB”, “ABA”, “BAA”.
Example 2:
Input: “AAABBC”
Output: 188

给定一个字符串,输出所有可能的全排列的数目
如果是python,里面有个itertool的工具,可以实现该功能,而在C++中,则直接想到了dfs,用一个set存储所有可能的字符串,用一个visited数组存储该位置的字符是否被访问过,最后输出set的大小即可

class Solution {
private:
    set<string> ss;
public:
    int numTilePossibilities(string tiles) {
        vector<int> visited(tiles.length(), 0);
        string cur = "";
        dfs(tiles, cur, visited);
        return ss.size();
    }
    
    void dfs(string s, string cur, vector<int>& visited)
    {
        if(cur.length() > 0) ss.insert(cur);
        for(int i = 0; i < s.length(); ++i)
        {
            if(visited[i] == 0)
            {
                visited[i] = 1;
                dfs(s, cur + s[i], visited);
                visited[i] = 0;
            }
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值