当前位置:网站首页>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;
}
边栏推荐
- 厌倦了安装数据库?改用 Docker
- UE5 官方案例Lyra 全特性详解 8.如何用配置表初始化角色数据
- dataBinding的import导入
- 【多线程】线程与进程、以及线程进程的调度
- 智能合约安全-可重入攻击(SW107-Reentrancy)
- FreeRTOS任务管理
- 程序员英语自我介绍
- 如何修复 SAP UI5 aggregation with cardinality 0..1 相关的错误消息
- 如何突破测试/开发程序员思维?一种不一样的感觉......
- Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
猜你喜欢
随机推荐
绿色版-SQL环境搭建
fifa将采用半自动越位技术计算进球
【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
高数---二重积分
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
random.nextint()详解
DataGuard日常维护常见问题之数据同步异常
Find My技术|智能防丢还得看苹果Find My技术
解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
Nuxt 所有页面都设置上SEO相关标签
德邦科技通过注册:年营收5.8亿 国家集成电路基金为大股东
关于地图GIS开发事项的一次实践整理(上)
我们来浅谈代码语言的魅力
Wireshark数据抓包分析之传输层协议(TCP协议)
RollBack Rx Professional RMC 安装教程
秒懂网络拓扑中的下一跳地址
[NCTF2019]SQLi-1||SQL注入
vant-swipe自适应图片高度+图片预览
DownMusic总结记录









