[LC] 336. Palindrome Pairs

Codeforces I. Palindrome Pairs (Hash) I. Palindrome Pairs time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output After learning a lot about space exploration, a little girl ... 阅读详情

这一题有着最straight forward的做法。就是把每俩个字符串组装一下然后检查一下是否是Palindrome。思路非常明白。代码如下:

    public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> result = new LinkedList<>();
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < words.length; j++) {
                if (i == j) continue;
                String s = words[i] + words[j];
                if (isPalindrome(s)) {
                    Integer[] resRow = {i, j};
                    result.add(Arrays.asList(resRow));
                }
            }
        }
        
        return result;
    }
    
    public boolean isPalindrome(String s) {
        int head = 0, tail = s.length() - 1;
        while (head < tail) {
            if (s.charAt(head) != s.charAt(tail)) {
                return false;
            }
            head++;
            tail--;
        }
        
        return true;
    }

如果单词个数是n,单词平均长度是k。那么这个做法的复杂度就是O(kn^2)。这个做法会超时。所以我们得想另一个做法。

下面这个做法可以认为是一种小型的改善,把复杂度变成了O(nk^2)。这种改善基于一个假设就是字符串数组的长度一般要比字符串的平均长度要高。这个算法基于下面一个匹配算法:

如果把一个字符串从中间分成两个部分,如果其中一部分本身已经是palindrome了,那么另一部分只需要反向匹配就可以了。譬如说,ababacd,其中一种分法就是aba和bacd,其中aba就是一个palindrome,所以我们需要在已有的字符串里面找到dcab就可以了,找的话用一个HashMap记住所有字符串,以字符串为键,位置为值就可以了。因为这个题目强调了这个列表里面的全是unique strings,所以不用担心有重复。记住了之后,找dcab只需要O(1)的操作就可以了。ababacd也可以分成ababa和cd,找dc就行了。另外一种分法的例子是cdbab,这里就是右边的是bab是palindrome,我们找是否有dc就行。根据这个匹配算法,我们可以得到下面的算法:

1.先把所有字符串放进哈希表里。

2. 然后对每个字符串遍历分成两边,就是abcbd就分成['', abcbd],[a,bcbd], [ab, cbd], [abc, bd], [abcb, d], [abcbd, ""]。然后应用于上面提到的那个匹配算法。

根据上述算法,得到代码如下:

    public List<List<Integer>> palindromePairs(String[] words) {
        HashMap<String, Integer> strMap = new HashMap<>();
        List<List<Integer>> res = new LinkedList<>();
        if (words == null || words.length < 2) return res;
        for (int i = 0; i < words.length; i++) strMap.put(words[i], i);
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j <= words[i].length(); j++) {
                String fHalf = words[i].substring(0, j);
                String sHalf = words[i].substring(j);
                if (isPalindrome(fHalf)) {
                    String newStr = new StringBuilder(sHalf).reverse().toString();
                    if (strMap.containsKey(newStr) && strMap.get(newStr) != i) {
                        Integer[] resArr = {strMap.get(newStr), i};
                        res.add(Arrays.asList(resArr));
                    }
                }

                if (sHalf.length() != 0 && isPalindrome(sHalf)) {
                    String newStr = new StringBuilder(fHalf).reverse().toString();
                    if (strMap.containsKey(newStr) && strMap.get(newStr) != i) {
                        Integer[] resArr = {i, strMap.get(newStr)};
                        res.add(Arrays.asList(resArr));
                    }
                }

            }
        }
        
        return res;
    }
        
    public boolean isPalindrome(String word) {
        int head = 0, tail = word.length() - 1;
        while (head < tail) {
            if (word.charAt(head) != word.charAt(tail)) {
                return false;
            }
            head++;
            tail--;
        }
        
        return true;
    }

要注意到的是,在sHalf的判断里,我加入了一个sHalf.length() != 0的判断。其实这不是边界判断,而是一个去重复的判断。举个例子,如果列表里面是"abc"和"cba"。abc其中一个分组是[abc, ""]。其中空字符串被算作palindrome,abc可以匹配到cba形成abccba,这样就会出现一次匹配。而cba也会有一个分组["", cba],这个时候cba也会往前匹配到abc形成abccba,但实际上这两组匹配会得到重复的结果。这种情况只会出现在分组两边有一边是完整的字符串另一边是空的情况下出现。所以,我们既可以在第一个fHalf那里加一个!fHalf.isEmpty()或者第二个sHalf那里加一个!sHalf.isEmpty()都行。这样就可以消除其中一边的重复。

 

接下来要介绍第三种算法,这种算法其实算是第二种算法的进化。匹配的原理还是基于上面提到的最基本的匹配算法。就是某一段匹配完之后剩余部分也能形成palindrome这样的一个原理。但为了提高匹配的速度,就不能那么简单的用哈希表了。这里可以用到的是trie tree。其实很多字典匹配里,dictionary的表示都可以用trie tree来替换哈希表来提高字符串匹配的速度。在这里,我们并不能实际提高算法复杂度的公式,公式仍然为O(nk^2)。但实际跑的速度变快了大约一倍左右。首先,先介绍trie tree的节点的结构

class TrieNode {
    // 26 children nodes
    TrieNode[] children;
    // When iterating to this node, how many palindromes substrings
    // it leads to. List of index of words that hit a palindromes substrings
    // at this node
    List<Integer> palindromes;
    // If any word ends at this point, since all words are unique
    // it would be at most one index here, null means no word ends at this node
    Integer index;
    
    public TrieNode() {
        this.children = new TrieNode[26];
    }
}

我加了点备注,除了一般性质的26个节点和表示是否某个单词的终结的index以外,还需要另外一个东西。一个是palindromes列表,这个列表存放的东西比较有意思,就是说当走到这个节点之后,剩下的子字符串是否是一个palindrome。举个例子,aba和ababa。当走到a->b这个节点的时候,aba剩下的a和ababa剩下的aba都是一个有效的palindrome,所以这个列表里面就会放这俩单词的index。另外那个index就比较明白了,就表示这个节点是否为一个单词的终结。因为题目里面表示了所有单词是unique的,所以这里就只需要一个Integer而非一个链表,用这个integer来表示对应的是input链表中的哪一个单词即可。

和解法二相同,构建词典(也就是树)或者匹配过程里,需要有一个过程里用的是reverse过的字符串,在这里,我们选择建树的过程reverse字符串,也就是这里建出来的的trie tree,是一个所有字符串reverse之后的字符串组合的trie tree。建树过程如下:

    public void addBranch(TrieNode root, String word, int index) {
        TrieNode currentNode = root;
        word = new StringBuilder(word).reverse().toString();
        for (int i = 0; i < word.length(); i++) {
            if (this.isPalindrome(word.substring(i))) {
                if (currentNode.palindromes == null) {
                    currentNode.palindromes = new LinkedList<>();
                }
                currentNode.palindromes.add(index);
            }
            
            int next = (int)(word.charAt(i) - 'a');
            if (currentNode.children[next] == null) {
                currentNode.children[next] = new TrieNode();                
            }
            
            currentNode = currentNode.children[next];
        }
        
        currentNode.index = index;
    }

只需要遍历每个词然后跑一遍这个就能把树给建好。至于为什么需要palindromes这个东西,下面进行搜索匹配的时候再解释

    public void searchBranch(TrieNode root, String word, int index, List<List<Integer>> result) {
        TrieNode currentNode = root;
        for (int i = 0; i < word.length() && currentNode != null; i++) {
            if (currentNode.index != null && this.isPalindrome(word.substring(i)) && !currentNode.index.equals(new Integer(index))) {
                Integer[] resRow = {index, currentNode.index};
                result.add(Arrays.asList(resRow));
            }
            
            int next = (int)(word.charAt(i) - 'a');
            currentNode = currentNode.children[next];
        }
        
        if (currentNode != null) {
            if (currentNode.index != null && !currentNode.index.equals(new Integer(index))) {
                Integer[] resRow = {index, currentNode.index};
                result.add(Arrays.asList(resRow));
            }
            
            if (currentNode.palindromes != null) {
                for (Integer rightIndex : currentNode.palindromes) {
                    Integer[] resRow = {index, rightIndex};
                    result.add(Arrays.asList(resRow));
                }
            }
        }
    }
    

在这里会解释一下我们在干嘛。我们首先把每个单词都放进去这个逆向单词树。然后试着是否能不停的往下走。有两种情况是可以认为当前遍历的词和某个节点可以形成一个结果配对的。如果说我们当前遍历到的单词叫str的话。

1. 当前节点index非空。这就表示str在当前节点上在已经走过了一个完整的单词。此时所需要判断的就是如果str剩下的部分是一个palindrome的话,那么str和该节点对应的index的单词就可以合并成为一个palindrome。当然你需要判断str所在的位置和index不是同一个位置即可。举个例子,如果你有一个word是abc,那么它在树里对应的分支就是c -> b -> a (因为是反向的树),如果str是cbaaba,那么它走到c - > b - > a的时候就会发现abc这个单词已经完结了,然后aba也是一个palindrome, 那么,cbaaba + abc就是cbaabaabc也是一个palindrome。

2. 当前节点的palindromes非空,也就是在这个节点有最少一个单词剩余的substring部分是palindrome。如果此时str已经走完了,就表示这个节点所有的palindromes对应的index都可以和str配对。 举个例子,如果有单词dedabc,dabc,edeabc,这样在c->b->a这个branch的a节点上,palindromes链表会有三个index对应前面三个单词。这个时候如果str是"cba"的话,他就会走到这个节点上,然后和前面三个单词凑成palindrome。

所以整个算法的解答代码如下:

class TrieNode {
    // 26 children nodes
    TrieNode[] children;
    // When iterating to this node, how many palindromes substrings
    // it leads to. List of index of words that hit a palindromes substrings
    // at this node
    List<Integer> palindromes;
    // If any word ends at this point, since all words are unique
    // it would be at most one index here, null means no word ends at this node
    Integer index;
    
    public TrieNode() {
        this.children = new TrieNode[26];
    }
}

class Solution {
    public List<List<Integer>> palindromePairs(String[] words) {
        TrieNode root = new TrieNode();
        for (int i = 0; i < words.length; i++) {
            this.addBranch(root, words[i], i);
        }
        
        List<List<Integer>> result = new LinkedList<>();
        for (int i = 0; i < words.length; i++) {
            this.searchBranch(root, words[i], i, result);
        }
        
        return result;
    }
    
    public void addBranch(TrieNode root, String word, int index) {
        TrieNode currentNode = root;
        word = new StringBuilder(word).reverse().toString();
        for (int i = 0; i < word.length(); i++) {
            if (this.isPalindrome(word.substring(i))) {
                if (currentNode.palindromes == null) {
                    currentNode.palindromes = new LinkedList<>();
                }
                currentNode.palindromes.add(index);
            }
            
            int next = (int)(word.charAt(i) - 'a');
            if (currentNode.children[next] == null) {
                currentNode.children[next] = new TrieNode();                
            }
            
            currentNode = currentNode.children[next];
        }
        
        currentNode.index = index;
    }
    
    public void searchBranch(TrieNode root, String word, int index, List<List<Integer>> result) {
        TrieNode currentNode = root;
        for (int i = 0; i < word.length() && currentNode != null; i++) {
            if (currentNode.index != null && this.isPalindrome(word.substring(i)) && !currentNode.index.equals(new Integer(index))) {
                Integer[] resRow = {index, currentNode.index};
                result.add(Arrays.asList(resRow));
            }
            
            int next = (int)(word.charAt(i) - 'a');
            currentNode = currentNode.children[next];
        }
        
        if (currentNode != null) {
            if (currentNode.index != null && !currentNode.index.equals(new Integer(index))) {
                Integer[] resRow = {index, currentNode.index};
                result.add(Arrays.asList(resRow));
            }
            
            if (currentNode.palindromes != null) {
                for (Integer rightIndex : currentNode.palindromes) {
                    Integer[] resRow = {index, rightIndex};
                    result.add(Arrays.asList(resRow));
                }
            }
        }
    }
    
    public boolean isPalindrome(String word) {
        int head = 0, tail = word.length() - 1;
        while (head < tail) {
            if (word.charAt(head) != word.charAt(tail)) {
                return false;
            }
            head++;
            tail--;
        }
        
        return true;
    }
}

 

Yolov2 训练时anchor是如何使用的?build_target 0.简略描述 1.将真值检测框变换到特征图尺度上 2.计算以特征图每个网格中心为中心点时所有锚框与真值检测框的交并比 3.取与真值检测框交并比最大的锚框的id,及真值检测框中心所在cell的id,有此,可将真值检测框与预测结果对应起来 4.计算位置回归损失,置信度损失,分类损失 1,网络的输出 图1 yolov2 网络对于检测框位置大小的输出是: 对于1个batch网络的输出的shape为[batch_size, H*W*num_anchors, 5+num_classes], 其中: num_ 阅读详情

相关推荐

px3 有感分析

px3 后续分析

qq_39707917的博客 3127

Palindrome Pairs 【位运算】

题目 After learning a lot about space exploration, a little girl named Ana wants to change the subject. Ana is a girl who loves palindromes (string that can be read the same backwards as forward). She has learned how to check for a given string whether it’s

QYQHxyh的博客 336

第二十届全国大学生智能车竞赛-地瓜机器人智慧医疗挑战赛技术解析与实战指南

本文深入解析了第二十届全国大学生智能车竞赛-地瓜机器人智慧医疗挑战赛的核心技术。内容涵盖从零搭建数字孪生环境、实现AI视觉任务(二维码识别、引导线追踪、目标检测)、在边缘设备部署轻量化大模型进行图生文描述,到遥操作与自主控制的平滑切换,并提供了基于ROS2的系统集成与赛场实战指南,助力参赛者高效备赛。

weixin_29252171的博客 2794

336. Palindrome Pairs(C++实现)

本文给出五种C++程序实现的代码!前三种解答程序,已Accepted !是从网上整理来的,加上本人一些分析和代码备注,让看的朋友更容易理解。 解答思路一:(最快解答) 用哈希表来建立每个单词和其位置的映射,一个set来保存出现过的单词的长度。 解答思路二:(很容易想到的算法) 考虑所有构成回文的情况 1 存在空单词;遍历所有单词,找回文单词,如{“aa”,“”} 2 将单词反转,若反转后的单词在单词表中,这两个单词构成回文!如{“abc”,“cba”} 3 将每个单词都分成左右两部分,(将aabc分两部分:

jingmiaa的博客 1406

WPS Office 2026免费安装指南:AI办公与安全使用全解析

办公软件作为数字化办公的核心工具,其基本原理是通过文档处理、数据计算和演示展示等功能模块满足用户日常办公需求。随着人工智能技术的发展,现代办公软件逐步集成AI能力,通过自然语言处理和数据分析算法提升工作效率。在技术价值层面,优秀的办公软件不仅需要保持格式兼容性和跨平台同步能力,更要注重用户数据安全防护。从应用场景来看,无论是个人文档编辑、企业报表分析还是团队协作演示,办公软件都发挥着关键作用。WPS Office 2026作为国产办公软件代表,深度融合AI助手功能,支持智能写作和数据分析,同时通过官方免费策

weixin_30599769的博客 806

ESXI 8.0虚拟机安装OpenWRT软路由详细教程

OpenWrt 是嵌入式的 Linux 路由器固件,OpenWrt在稳定运行的同时提供了强大的扩展能力,用户可以完全的定制属于自己的路由系统,满足不一样的个性化需求,本文讲解如何使用ESXI安装OpenWRT,打造属于自己的软路由。本次安装使用的是版本。默认登录信息:用户:root密码:password下载固件:https://github.com/haiibo/OpenWrt/releases/tag/X86_64【CPU配置】:4核心。

qq_27121281的博客 3932

Palindrome Pairs

Given a list ofuniquewords, find all pairs ofdistinctindices(i, j)in the given list, so that the concatenation of the two words, i.e.words[i] + words[j]is a palindrome. Example 1: Input: ["abcd","dcba","lls","s","sssll"] Output: [[0,1],[1,0],[3,...

leetcode 解题思路 367

【用unity实现100个游戏之16】Unity程序化生成随机2D地牢游戏1(附项目源码)

关于使用TileMap生成随机2D地图,其实之前已经有做过类似的,感兴趣可以看看 但是随着学习深入,发现之前做的比较粗糙和不够全面,最近又在外网看到

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2222

I. Palindrome Pairs(字符哈希)

话说是我太菜了嘛… 这道题才1600分,没自信了 方法还是比较巧妙地,字符hash 因为字符串是回文串当且仅当至多有一个字母是奇数因为字符串是回文串当且仅当至多有一个字母是奇数因为字符串是回文串当且仅当至多有一个字母是奇数 所以对于所有的字符串,记录字母i是否为奇数所以对于所有的字符串,记录字母i是否为奇数所以对于所有的字符串,记录字母i是否为奇数 26个字母,可以表示为一个26位的二进制数,1代表该字母是奇数个26个字母,可以表示为一个26位的二进制数,1代表该字母是奇数个26个字母,可以表示为一个26位

jziwjxjd的博客 581

LangGraph-StateGraph 和 Graph API 源码分析

摘要: StateGraph是LangGraph的核心API,用于构建有状态工作流图。本文分析了其实现原理,包括: 核心类关系:StateGraph(构建器)和CompiledStateGraph(继承自Pregel的可执行图) 关键流程:状态模式到通道的映射、节点到PregelNode的转换 实现特点: 支持TypedDict和Pydantic两种状态模式 自动选择通道类型(LastValue或BinaryOperatorAggregate) 通过映射函数处理节点输入输出 边实现基于通道写入机制 编译过程

842

STM32外部中断EXTI原理与飞控实时应用实战

外部中断(EXTI)是嵌入式系统中实现事件驱动和实时响应的核心机制,其本质是将外部电平变化转化为可调度的CPU中断事件。基于ARM Cortex-M NVIC架构,EXTI通过GPIO-EXTI-NVIC三级硬件通路实现低延迟信号捕获,具备边沿触发、优先级管理与挂起控制等关键能力。在四轴飞控、工业传感器、人机交互等对毫秒级响应有强需求的场景中,EXTI显著优于轮询方式,兼顾实时性与CPU资源效率。实际开发需严格遵循SYSCFG映射→EXTI配置→NVIC使能的时序逻辑,并规避常见陷阱如中断号误配、伪触发、低

weixin_36382073的博客 70

lc 336. Palindrome Pairs

https://leetcode.com/problems/palindrome-pairs/description/ 给一个list[str],找到所有两个可以连接成回文的下标可能。 开始以为会有高级的算法,后来发现只是一个优化加速一下的问题。最大的经验教训就是,拿到一个题先想想是不是初步想法就能a掉,万一人家就没考深层次的奇葩算法呢。 题解里面给的是n*n*k,下面我的算法是n*k...

weixin_30662109的博客 118

微信小程序 - 最新实现项目适老化设计全流程及源码,老年模式大字版与常规版页面样式切换,老年人大字模式字体与布局更换关怀版解决方案(新老项目均适用的方案,全局页面统一修改文本字号/DOM排版元素等)

微信小程序,vue2,vue3,老年大字版设计,小程序开发如何实现适老化方案,放大字体字号及改变页面的布局,小程序关怀模式实现思路及代码,统一将布局都改了,全局修改字体大小适配老年人看不清问题,小程序怎么接入老年模式并根据模式改变页面的UI设计,实现全局修改字体大小适老化/关怀版改造,最新详细示例代码全流程,字体放大/缩小,老项目怎么适配老年人模式方案,小程序适老化设计指南,当切换成老年模式时页面所有排版都会发生变化,小程序vue开发,新手详细教程,老年模式字体设置思路和UI布局的设计,开发者可以通过wx.

🏆 前端领域优质创作者 7323

leetcode 336 Palindrome Pairs

lc 336 Palindrome Pairs 最简单的就是每两个都拼接一下,然后判断是否为回文串 时间复杂度为o(n^2*k) n:words个数 k:word平均长度 但是我们仔细观察一下,两个word拼接之后,是如何判断是否为回文串的呢?首尾指针,匹配,向中间收缩。 假如现在有一个单词"asad",有可能和它组成回文串的字母一定结尾是a,"???????a",倒数第二个字母...

weixin_30248399的博客 171

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

336. Palindrome Pairs(Leetcode每日一题-2020.08.06)--抄答案

Problem Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example1 Input: [“abcd”,“dcba”,“lls”,“s”,“sssll”] Output: [[0,1],[1,0]

Bryan要加油 516

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

LeetCode Palindrome Pairs

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1: Give

多看多听多总结 1718

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

336. Palindrome Pairs 寻找回文对

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.words[i] + words[j] is a palindrome. Example 1: Given

u010005161的博客 1430

336. Palindrome Pairs

336. Palindrome Pairs方法1: hash易错点 Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a p...

wilzxu的博客 384

(Java)-LeetCode-336. Palindrome Pairs

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. Example 1: Give

u012848330的博客 1144

【LeetCode】336. Palindrome Pairs 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法HashTable相似题目参考资料日期 题目地址:https://leetcode.com/problems/palindrome-pairs/description/ 题目描述 Given a list of unique words, find all ...

负雪明烛 2723

LeetCode 336. Palindrome Pairs(回文对)

原题网址:https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e.

James Pan 2853

【leetcode】Palindrome Pairs

这题最开始使用暴力破解的方法。时间复杂度(O(k*n^2))k是string中最长的长度。判断是否是回文的程序如下: bool is_palindrome(string word1,string word2) { int word1len = int(word1.size()),word2len = int(word2.size()); if (word1len>word2len

王强_CASIA的博客 866

[leetcode] 336. Palindrome Pairs 解题报告

题目链接: https://leetcode.com/problems/palindrome-pairs/   Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. wo...

小榕流光的专栏 5791

CF.1045I.Palindrome Pairs-字符串+hash

题意: 给出若干个字符串,任意两个字符串可任意拼接组合,任意拼接就相当于把两个字符串的字母都取出来,然后再重新组成新字符串,求有多少对字符串拼接后能构成回文串 思路: 对于一个回文串,至多只有一个字母出现奇数次,其余字母都出现偶数次,通过这性质,我们把一个字符串哈希成26位的01串,01串的第i位表示字母表中第i个字母出现次数的奇偶。 遍历所有字符串,统计与该字符串hash值至多只有一位不同的h...

Ogmx的博客 699

【Leetcode】Palindrome Pairs

题目链接:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words,

业精于勤,荒于嬉 1668

LeetCode 336. Palindrome Pairs

Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.Example 1: Given words =

z6491679的专栏 1243
上一篇: [LC] 334. Increasing Triplet Subsequence
下一篇: [LC] 338. Counting Bits
Daisy么么哒
博客等级 码龄12年 10粉丝 310原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值