当前位置:网站首页>49. 字母异位词分组-排序法
49. 字母异位词分组-排序法
2022-08-02 23:56:00 【Mr Gao】
49. 字母异位词分组-排序
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
示例 1:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
示例 2:
输入: strs = [“”]
输出: [[“”]]
示例 3:
输入: strs = [“a”]
输出: [[“a”]]
解题代码如下:
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */
int cmp(const void *a,const void *b){
return *((char *)a)- *((char *)b);
}
void quick(char **a,int low,int high ,char **b){
if(low<high){
// printf("%s ",a[high]);
int l=low,h=high;
char *ch=a[low];
char *chb=b[low];
while(low<high){
// / printf("%s ",ch);
while(low<high&&strcmp(a[high],ch)>=0){
// printf("%s ",a[high]);
high--;
}
a[low]=a[high];
b[low]=b[high];
while(low<high&&strcmp(a[low],ch)<=0){
low++;
}
a[high]=a[low];
b[high]=b[low];
}
a[low]=ch;
b[low]=chb;
quick(a,l,low-1,b);
quick(a,low+1,h,b);
}
}
char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){
int i;
char **sr=(char **)malloc(sizeof( char *)*strsSize);
char ***re=(char ***)malloc(sizeof(char **)*strsSize);
* returnColumnSizes=(int *)malloc(sizeof(int)*strsSize);
int size=0;
for(i=0;i<strsSize;i++){
sr[i]=(char *)malloc(sizeof(char)*(strlen(strs[i])+1));
strcpy(sr[i],strs[i]);
qsort(strs[i],strlen(strs[i]),sizeof(char),cmp);
}
quick(strs,0,strsSize-1,sr);
char *ch=strs[0];
int index=0;
for(i=1;i<strsSize;i++){
if(strcmp(strs[i],ch)!=0){
int length=i-index;
re[size]=(char**)malloc(sizeof(char *)*length);
// printf("sdf");
(*returnColumnSizes)[size]=length;
int p=0;
for(int j=index;j<i;j++){
re[size][p++]=sr[j];
}
size++;
index=i;
ch=strs[i];
}
}
int length=i-index;
re[size]=(char**)malloc(sizeof(char *)*length);
// printf("sdf");
(*returnColumnSizes)[size]=length;
int p=0;
for(int j=index;j<i;j++){
re[size][p++]=sr[j];
}
size++;
for(i=0;i<strsSize;i++){
printf("%s ",sr[i]);
}
* returnSize=size;
return re;
}
边栏推荐
- 别再到处乱放配置文件了!我司使用 7 年的这套解决方案,稳的一秕
- 线性DP
- 【多线程】线程与进程、以及线程进程的调度
- 高数---二重积分
- js基础知识整理之 —— 闭包
- Last Common Ancestor (LCA) Study Notes | P3379 【Template】Least Common Ancestor (LCA) Problem Solution
- 21天学习挑战赛(1)设备树的由来
- What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
- Merge two excel spreadsheet tools
- NVM和NRM
猜你喜欢
随机推荐
js基础知识整理之 —— 变量和数据类型
从一文中了解SSRF的各种绕过姿势及攻击思路
LVM与磁盘配额原理及配置
js基础知识整理之 —— 全局作用域
4、Citrix MCS云桌面无法安装todesk等软件
TensorFlow学习记录(一):基本介绍
用了TCP协议,就一定不会丢包吗?
matplotlib中的3D绘图警告解决:MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure
NLP常用Backbone模型小抄(1)
淘宝商品销量接口/淘宝商品销量监控接口/商品累计销量接口代码对接分享
HVV红队 | 渗透测试思路整理
新公链时代的跨链安全性解决方案
优秀论文以及思路分析02
机器学习-特征映射方法
nmap: Bad CPU type in executable
js基础知识整理之 —— 五种输出方式
心电记录电路设计(框图/波形以及信号放大器的选择)
dataBinding的import导入
【多线程】Thread类的基本用法
js显示隐藏手机号