C语言 | Leetcode C语言题解之第139题单词拆分

题目:

题解:

cpp 复制代码
unsigned long long Hash(char* s, int l, int r) {
    unsigned long long value = 0;
    for (int i = l; i < r; i++) {
        value = value * 2333ull;
        value += s[i] - 'a' + 1;
    }
    return value;
}
bool query(unsigned long long* rec, int len_rec, unsigned long long x) {
    for (int i = 0; i < len_rec; i++) {
        if (rec[i] == x) return true;
    }
    return false;
}
bool wordBreak(char* s, char** wordDict, int wordDictSize) {
    unsigned long long rec[wordDictSize + 1];
    for (int i = 0; i < wordDictSize; i++) {
        rec[i] = Hash(wordDict[i], 0, strlen(wordDict[i]));
    }
    int len_s = strlen(s);
    bool dp[len_s + 1];
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for (int i = 1; i <= len_s; i++) {
        for (int j = 0; j < i; j++) {
            if (dp[j] && query(rec, wordDictSize, Hash(s, j, i))) {
                dp[i] = true;
                break;
            }
        }
    }
    return dp[len_s];
}
相关推荐
夏鹏今天学习了吗6 小时前
【LeetCode热题100(87/100)】最小路径和
算法·leetcode·职场和发展
不忘不弃7 小时前
模拟内存分配器1
c语言
Lips6118 小时前
2026.1.20力扣刷题笔记
笔记·算法·leetcode
鱼跃鹰飞9 小时前
Leetcode347:前K个高频元素
数据结构·算法·leetcode·面试
52Hz11811 小时前
力扣24.两两交换链表中的节点、25.K个一组反转链表
算法·leetcode·链表
老鼠只爱大米11 小时前
LeetCode经典算法面试题 #160:相交链表(双指针法、长度差法等多种方法详细解析)
算法·leetcode·链表·双指针·相交链表·长度差法
老鼠只爱大米12 小时前
LeetCode经典算法面试题 #84:柱状图中最大的矩形(单调栈、分治法等四种方法详细解析)
算法·leetcode·动态规划·单调栈·分治法·柱状图最大矩形
重生之后端学习12 小时前
19. 删除链表的倒数第 N 个结点
java·数据结构·算法·leetcode·职场和发展
sycmancia13 小时前
C语言学习07——变量的作用域
c语言·学习
鹿角片ljp13 小时前
力扣14.最长公共前缀-纵向扫描法
java·算法·leetcode