leetcode 0216

✅ 893. 特殊等价字符串组

https://leetcode-cn.com/problems/groups-of-special-equivalent-strings

描述

你将得到一个字符串数组 A。

如果经过任意次数的移动,S == T,那么两个字符串 S 和 T 是特殊等价的。

一次移动包括选择两个索引 i 和 j,且 i % 2 == j % 2,交换 S[j] 和 S [i]。

现在规定,A 中的特殊等价字符串组是 A 的非空子集 S,这样不在 S 中的任何字符串与 S 中的任何字符串都不是特殊等价的。

返回 A 中特殊等价字符串组的数量。

 

示例 1:

输入:["a","b","c","a","c","c"]
输出:3
解释:3 组 ["a","a"],["b"],["c","c","c"]
示例 2:

输入:["aa","bb","ab","ba"]
输出:4
解释:4 组 ["aa"],["bb"],["ab"],["ba"]


示例 3:

输入:["abc","acb","bac","bca","cab","cba"]
输出:3
解释:3 组 ["abc","cba"],["acb","bca"],["bac","cab"]

//tt 现在我看懂了题目一点: 
// 示例3 你看到 结果 有三组, 而每一组里面,都是等价的,所以就只有3组
示例 4:

输入:["abcd","cdab","adcb","cbad"]
输出:1
解释:1 组 ["abcd","cdab","adcb","cbad"]
 

提示:

1 <= A.length <= 1000
1 <= A[i].length <= 20
所有 A[i] 都具有相同的长度。
所有 A[i] 都只由小写字母组成。


题目描述应该修改为: “现在规定,A 中的特殊等价字符串组是 A 的非空子集 S,这样不在 S 中的任何【A中的】字符串与 S 中的任何字符串都不是特殊等价的。

返回 A 中特殊等价字符串组的数量。”

因为假设输入["abc"]的话,按照官方解答,应该返回1,因为有一组,即为S = ["abc"]。 但是显然不在S中的字符串“cba”跟S中的"abc"是特殊等价的。所以应该加一个前提是考察的都是在在A中的字符串?

解答

题目读不懂,但是看到如下的c++ 和py 代码 是相同的思路 tdo 细细读之, 思路写在 py 里

cpp

class Solution {
public:
    int numSpecialEquivGroups(vector<string>& A) {
        set<string> jihe;
        for(int i=0;i<A.size();i++){
            string a="",b="";
            for(int j=0;j<A[i].size();j++){
                if(j%2==0)a+=A[i][j];
                else b+=A[i][j];
            }
            sort(a.begin(),a.end());
            sort(b.begin(),b.end());
            jihe.insert(a+b);
            
        }
        return jihe.size();
    }
};

py

思路是,似乎,你把所有的奇数位置上的元素拿出来,排列好,几个等价的字符串应该会表现最终结果的一样,同理,你把偶数位置上的拿出来,排列之,几个也会变成一个(tt1),那么我们就看看有几个tt1,那么就返回这个几个即可。ans 是使用把几个等价的字符串 放入set中,然后最后看len(set)

def numSpecialEquivGroups(self, A: List[str]) -> int:
    res = set()
    for sub in A:
        sub = ''.join(sorted(sub[::2]) + sorted(sub[1::2]))# 后面[1::2] 的意思是:从1开始,隔两个进行切片。
        res.add(sub)
    return len(res)

'''
input : ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
output:

acbd
acbd
acbd
xzyz
xzyz
yzxz

执行用时 :
52 ms
, 在所有 Python3 提交中击败了
66.99%
的用户
内存消耗 :
13.4 MB
, 在所有 Python3 提交中击败了
46.00%
的用户
'''

✅ 811. 子域名访问计数

https://leetcode-cn.com/problems/subdomain-visit-count/

描述

一个网站域名,如"discuss.leetcode.com",包含了多个子域名。作为顶级域名,常用的有"com",下一级则有"leetcode.com",最低的一级为"discuss.leetcode.com"。当我们访问域名"discuss.leetcode.com"时,也同时访问了其父域名"leetcode.com"以及顶级域名 "com"。

给定一个带访问次数和域名的组合,要求分别计算每个域名被访问的次数。其格式为访问次数+空格+地址,例如:"9001 discuss.leetcode.com"。

接下来会给出一组访问次数和域名组合的列表cpdomains 。要求解析出所有域名的访问次数,输出格式和输入格式相同,不限定先后顺序。

示例 1:
输入: 
["9001 discuss.leetcode.com"]
输出: 
["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
说明: 
例子中仅包含一个网站域名:"discuss.leetcode.com"。按照前文假设,子域名"leetcode.com"和"com"都会被访问,所以它们都被访问了9001次。
示例 2
输入: 
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出: 
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
说明: 
按照假设,会访问"google.mail.com" 900次,"yahoo.com" 50次,"intel.mail.com" 1次,"wiki.org" 5次。
而对于父域名,会访问"mail.com" 900+1 = 901次,"com" 900 + 50 + 1 = 951次,和 "org" 5 次。
注意事项:

 cpdomains 的长度小于 100。
每个域名的长度小于100。
每个域名地址包含一个或两个"."符号。
输入中任意一个域名的访问次数都小于10000。


解答

cpp

tdo rev me

class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {//注意: vector<string>&
        map<string,long> vitimes;
        for(auto cpdomain:cpdomains)// for each 里面使用了auto 这似乎是弱类型
        {
            stringstream out(cpdomain);
            int times;string domain;
            out >> times >>domain;//两个 >> 似乎就是 先把900 给 times, 然后 把 www.goggle.com 给 domain
            int len=domain.size(),pos=-1;
            vitimes[domain]+=times;//为当前domain 计数
            while((pos=domain.find(".",pos+1))!=string::npos)
            {// string::npos    你可以 约等于 py None
                string dom=domain.substr(pos+1);
                vitimes[dom]+=times;
            }
        }
        vector<string> res;
        for(auto item:vitimes)
            // 输出: 950 www.so.com
            res.push_back(to_string(item.second)+" "+item.first);
        return res;
    }
};

py

比较清晰:

class Solution:
    def subdomainVisits(self, cpdomains: 'List[str]') -> 'List[str]':
        a = {}
        for i in cpdomains:
            s1 = i.split(' ')# 拆分为 两个: one int and one string tt1
            s2 = s1[1].split('.')# then split the one string tt1 by ., u got a list tt2
            s3 = [] # usage: 
            for i in range(len(s2)):# you will get lenOfTheList tt2 not the url string
                s3.append('.'.join(s2[i:]))# this way U got: www.so.com then U got so.com , then U got: com
            for i in s3:
                if i in a:
                    a[i] += int(s1[0])
                else:
                    a[i] = int(s1[0])
        ret = []
        for i in a:
            s = ''
            s += str(a[i]) + ' ' + i
            # 输出的格式like :s = 950 www.so.com
            ret.append(s)
        return ret
'''
执行用时 :
92 ms
, 在所有 Python3 提交中击败了
13.21%
的用户
内存消耗 :
13.2 MB
, 在所有 Python3 提交中击败了
44.45%
的用户
'''

✅ 509. 斐波那契数

https://leetcode-cn.com/problems/fibonacci-number/

描述

斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
给定 N,计算 F(N)。

解答

cpp


//递归
class Solution {
public:
    int fib(int N) {
        if(N == 0 || N == 1) {
            return N;
        }
        return fib(N - 1) + fib(N - 2);
    }
};
// 迭代:
class Solution {
public:
    int fib(int N) {
        if (N == 0 || N == 1) {
            return N;
        }
        int arr[N+1] = {0};
        arr[0] = 0;
        arr[1] = 1;
        for(int i = 2; i <= N; i++) {
            arr[i] = arr[i-2] + arr[i-1];
        }
        return arr[N];
    }
};
//执行用时: 4 ms
class Solution {
public:
    int fib(int N) {
        if (N == 0 || N == 1) {
            return N;
        }
        int x = 0, y = 1, z = 1, i = 0, end = N - 2;
        while(i <= end){//dont: i < end
            z = x + y;
            x = y;
            y = z;
            i++;
        }
        return z;
    }
};
/*
执行用时 :
4 ms
, 在所有 C++ 提交中击败了
74.24%
的用户
内存消耗 :
8.3 MB
, 在所有 C++ 提交中击败了
29.91%
的用户
*/

py


✅ 521. 最长特殊序列 Ⅰ

描述

给定两个字符串,你需要从这两个字符串中找出最长的特殊序列。最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列)。

子序列可以通过删去字符串中的某些字符实现,但不能改变剩余字符的相对顺序。空序列为所有字符串的子序列,任何字符串为其自身的子序列。tt2

输入为两个字符串,输出最长特殊序列的长度。如果不存在,则返回 -1。

示例 :

输入: "aba", "cdc"
输出: 3
解析: 最长特殊序列可为 "aba" (或 "cdc")

解答

注意这个 充满 bug 的题目要求:任何字符串为其自身的子序列。tt2

所以:

这题是一道脑筋急转弯:
出题人:“就是喜欢看你们不敢相信那么简单,又不敢提交的样子。”
简单点来说就是
1.若字符串a和b相同,返回-1
2.若字符串a和b不相同,返回a,b中较长的长度

cpp

class Solution {
public:
    int findLUSlength(string a, string b) {
        if(a.compare(b) == 0) {
            return -1;
        }
        return max(a.length(), b.length());
    }
};

/*执行用时 :
4 ms
, 在所有 C++ 提交中击败了
60.40%
的用户
内存消耗 :
8.5 MB
, 在所有 C++ 提交中击败了
5.24%
的用户*/
原文地址:https://www.cnblogs.com/paulkg12/p/12316267.html