C语言 | Leetcode C语言题解之第126题单词接龙II

2024-06-02 10:20

本文主要是介绍C语言 | Leetcode C语言题解之第126题单词接龙II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

题解:

char** list;
int** back;
int* backSize;// DFS uses backtrack information to construct results
void dfs(char*** res, int* rSize, int** rCSizes, int* ans, int last, int retlevel) {int i = ans[last];if (i == 0) {res[*rSize] = (char**)malloc(sizeof(char*) * retlevel);(*rCSizes)[*rSize] = retlevel;for (int j = 0; j < retlevel; ++j) {res[*rSize][j] = list[ans[j]];}++(*rSize);}if (last == 0) return;for (int j = 0; j < backSize[i]; ++j) {int k = back[i][j];ans[last - 1] = k;dfs(res, rSize, rCSizes, ans, last - 1, retlevel);}
}char*** findLadders(char* beginWord, char* endWord, char** wordList, int wordListSize, int* returnSize, int** returnColumnSizes) {*returnSize = 0;int size = wordListSize + 1;  // new sizeint wlen = strlen(beginWord);list = (char**)malloc(sizeof(char*) * size);  // new wordlistback = (int**)malloc(sizeof(int*) * size);    // data to backtrack indexes while BFSbackSize = (int*)malloc(sizeof(int) * size);int* visited = (int*)malloc(sizeof(int) * size);  // visited flag in BFSint** diff = (int**)malloc(sizeof(int*) * size);  // list diff[i] are indexes which can connect to word[i]int* diffSize = (int*)malloc(sizeof(int) * size);int endidx = 0;// Intialization 1for (int i = 0; i < size; ++i) {list[i] = i == 0 ? beginWord : wordList[i - 1];visited[i] = 0;diff[i] = (int*)malloc(sizeof(int) * size);diffSize[i] = 0;back[i] = (int*)malloc(sizeof(int) * size);backSize[i] = 0;if (strcmp(endWord, list[i]) == 0) {endidx = i;}}if (endidx == 0) return 0;  // endword is not in the list// collect diff datafor (int i = 0; i < size; ++i) {for (int j = i; j < size; ++j) {int tmp = 0;  // tmp is the difference between word[i] & word[j]for (int k = 0; k < wlen; ++k) {tmp += list[i][k] != list[j][k];if (tmp > 1) break;}if (tmp == 1) {diff[i][diffSize[i]++] = j;diff[j][diffSize[j]++] = i;}}}// BFSint* curr = (int*)malloc(sizeof(int) * size);  // curr level in BFSint* prev = (int*)malloc(sizeof(int) * size);  // prev level in BFSint prevSize, currSize = 1;int* currvisited = (int*)malloc(sizeof(int) * size);  // to make sure curr members are uniqueint level = 1;                                        // level of laddercurr[0] = 0;visited[0] = 1;int retlevel = 0;  // ladder size, marks the end of BFSwhile (retlevel == 0 && currSize > 0) {++level;// switch prev and currint* tmp = prev;prev = curr;curr = tmp;prevSize = currSize;currSize = 0;// reset currvisited for each level currfor (int i = 0; i < size; ++i) {currvisited[i] = 0;}for (int i = 0; i < prevSize; ++i) {for (int j = 0; j < diffSize[prev[i]]; ++j) {int k = diff[prev[i]][j];  // k is the candidate of curr (next level of BFS)if (visited[k]) continue;back[k][backSize[k]++] = prev[i];   // backtrackif (k == endidx) retlevel = level;  // find the lowest ladder level, finish current BFSif (currvisited[k]) continue;       // no duplicates in curr listcurr[currSize++] = k;currvisited[k] = 1;}}// set visited after search through one level, different from Q127for (int i = 0; i < currSize; ++i) {visited[curr[i]] = 1;}}if (retlevel == 0) return 0;  // no ladder found// DFS to construct resultchar*** res = (char***)malloc(sizeof(char**) * size);int* ans = (int*)malloc(sizeof(int) * retlevel);*returnColumnSizes = (int*)malloc(sizeof(int) * size);ans[retlevel - 1] = endidx;dfs(res, returnSize, returnColumnSizes, ans, retlevel - 1, retlevel);return res;
}

这篇关于C语言 | Leetcode C语言题解之第126题单词接龙II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1023691

相关文章

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达

Go语言实现桥接模式

《Go语言实现桥接模式》桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,本文就来介绍一下了Go语言实现桥接模式,感兴趣的可以了解一下... 目录简介核心概念为什么使用桥接模式?应用场景案例分析步骤一:定义实现接口步骤二:创建具体实现类步骤三:定义抽象类步骤四:创建扩展抽象类步

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

GO语言zap日志库理解和使用方法示例

《GO语言zap日志库理解和使用方法示例》Zap是一个高性能、结构化日志库,专为Go语言设计,它由Uber开源,并且在Go社区中非常受欢迎,:本文主要介绍GO语言zap日志库理解和使用方法的相关资... 目录1. zap日志库介绍2.安装zap库3.配置日志记录器3.1 Logger3.2 Sugared

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

GO语言中gox交叉编译的实现

《GO语言中gox交叉编译的实现》本文主要介绍了GO语言中gox交叉编译的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录一、安装二、使用三、遇到的问题1、开启CGO2、修改环境变量最近在工作中使用GO语言进行编码开发,因

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求: