大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~
Given two strings s
and t
, return true
if t
is an anagram of s
, and false
otherwise.
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
1 <= s.length, t.length <= 5 * 10^4
s
and t
consist of lowercase English letters.What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
首先先简单的翻译一下题目
给你两个字串,要判断这两个字串是不是由相同种类跟数量的字母组成。
作法大致上是这样
calloc
是因为他可以将动态记忆体配置的空间的值初始化为0
,如果是用malloc
的话,会不知道配置空间的初始值。calloc(26, sizeof(int))
,26
代表的是连续空间的大小,跟malloc
一样使用完要用free()
释放掉。class Solution:
def isAnagram(self, s: str, t: str) -> bool:
dict_s = {}
dict_t = {}
for index in range(len(s)):
if s[index] in dict_s:
dict_s[s[index]] += 1
else:
dict_s[s[index]] = 1
for index in range(len(t)):
if t[index] in dict_t:
dict_t[t[index]] += 1
else:
dict_t[t[index]] = 1
return dict_s == dict_t
bool isAnagram(char * s, char * t){
// int *dict_s = malloc(sizeof (int) * 26);
int *dict_s = calloc(26, sizeof(int));
// int *dict_t = malloc(sizeof (int) * 26);
int *dict_t = calloc(26, sizeof(int));
for (int index=0 ; index<strlen(s) ; index++){
if (dict_s[(int)(s[index])-97] != 0){
dict_s[(int)(s[index])-97]++;
} else {
dict_s[(int)(s[index])-97] = 1;
}
}
for (int index=0 ; index<strlen(t) ; index++){
if (dict_t[(int)(t[index])-97] != 0){
dict_t[(int)(t[index])-97]++;
} else {
dict_t[(int)(t[index])-97] = 1;
}
}
for (int index=0 ; index<26 ; index++){
if (dict_s[index] != dict_t[index]){
return false;
}
}
return true;
}
bool isAnagram(char * s, char * t){
// int *dict_s = malloc(sizeof (int) * 26);
int *dict_s = calloc(26, sizeof(int));
// int *dict_t = malloc(sizeof (int) * 26);
int *dict_t = calloc(26, sizeof(int));
for (int index=0 ; index<strlen(s) ; index++){
dict_s[(int)(s[index])-97]++;
}
for (int index=0 ; index<strlen(t) ; index++){
dict_t[(int)(t[index])-97]++;
}
for (int index=0 ; index<26 ; index++){
if (dict_s[index] != dict_t[index]){
return false;
}
}
return true;
}
bool isAnagram(char * s, char * t){
// int *dict_s = malloc(sizeof (int) * 26);
int *dict = calloc(26, sizeof(int));
int index_s=0, index_t=0;
while (index_s != strlen(s) || index_t != strlen(t)){
if (index_s<strlen(s)){
dict[(int)(s[index_s])-97]++;
index_s++;
}
if (index_t<strlen(t)){
dict[(int)(t[index_t])-97]--;
index_t++;
}
}
for (int index=0 ; index<26 ; index++){
if (dict[index] != 0){
return false;
}
}
return true;
}
Python
C
ver. 1
ver. 2
ver. 3
大家明天见
>>: D-10 AoP ? autofac ? DynamicProxy
前言 延续昨天内容今天继续介绍常用 ES6 语法。 展开其余 展开运算符有几个用途 阵列 展开成个别...
前言: 关於前面mlmodel的部分还没搞定 事先测试了一下CoreML的功能 VC程序码: imp...
RAID tags: IT铁人 这个硬碟有多棒 在评断一个硬碟有多高的Availability时,我...
终於要来组合画面噜~ 写了这麽多天的小区块切版, 终於要派上用场了! 是不是常常有一种:「我想要学的...
Hi,搭给贺,我是Charlie! 在Day01当中,我们安装了所需的套件跟软件,还做了版面。 接下...