✅ 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%
的用户*/

leetcode 0216的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. flutter web 配置环境及运行(windows)

    此下 操作 都是基于 windows  一, 将镜像添加到 用户环境变量中 由于在国内访问Flutter有时可能会受到限制,Flutter官方为中国开发者搭建了临时镜像,大家可以将如下环境变量加入到用 ...

  2. 【转载】Windows环境下JNI的实现实例

    转自:http://blog.csdn.net/jjunjoe/article/details/6987183 一.关于JNI: JNI(Java Native Interface):Java本地调用 ...

  3. TXT文件也能挂木马

    什么?TXT文件也能挂马?是的!TXT文件不仅有挂马的危险,而且有时候可能非常的危险!不过,严格说来,应该给这个所谓的"TXT"文件加个引号,因为它们是看起来是TXT文件,实则是隐 ...

  4. 8.10-DayT3游走(wander)

    题目大意 lue.. 题解 先跑一遍tarjan缩点,在新图中加入两个强连通分量之间的边,则此图为一个有向无环图(DAG).则最终答案为1点所在的强连通分量或包括1点的几个强连通分量的点数之和. 如果 ...

  5. AS报错:Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 'com.and

    build->Rebuid-project 寻找错误根源: 报错里可以发现: Resolved versions for app (26.1.0) and test app (27.1.1) d ...

  6. MySQL 学习(三)事务学习

    事务隔离级别     SQL标准的事务隔离级别包括:读未提交(read uncommitted).读提交(read committed).可重复读(repeatable read)和串行化(seria ...

  7. C语言当中int,float,double,char这四个有什么区别?

    区别在以下方面: 一.定义方面: 1.int为整数型,用于定义整数类型的数据 . 2.float为单精度浮点型,能准确到小数点后六位 . 3.double为双精度浮点型,能准确到小数点都十二位 . 4 ...

  8. 《C语言程序设计》王希杰 课后答案

    仅供参考,好好学习,不要骗自己哦! 在线预览 预览链接: https://www.kdocs.cn/l/shOy4IgXl 下载: 链接1: http://t.cn/AiBK2mgJ 链接2: htt ...

  9. C#中字符串常用方法

    string str = "123@163.com"; int index = str.IndexOf('@'); // 返回3  从左向右第一个@ int index = str ...

  10. pyqt5 通过QLinearGradient 绘制取色板

    要绘制HSV取色板,一般通过绘制前景色和背景色的方式实现,先绘制前景,然后绘制背景,前景是HSV颜色空间,从左到右,背景是亮度,从上到下,xs和ys是鼠标的当前的位置. def graphicsVie ...