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 = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]
Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
方法一:暴力解法
public class PalindromePairs {
public boolean isPalindrome(String string) {
char c[]=string.toCharArray();
boolean flag=true;
for (int i = 0; i < c.length/2; i++) {
if (c[i]!=c[c.length-i-1]) {
flag=false;
break;
}
}
return flag;
}
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> b= new ArrayList<List<Integer>>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (i!=j) {
String string=words[i]+words[j];
if (isPalindrome(string)) {
List<Integer> a= new ArrayList<Integer>();
a.add(i);
a.add(j);
b.add(a);
}
}
}
}
return b;
}
public static void main(String[] args) {
String [] words={"hijajcd","hjhgahbcegj"};
PalindromePairs pairs=new PalindromePairs();
System.out.println(pairs.palindromePairs(words));
}
}
方法二:
利用
public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> resultList= new ArrayList<List<Integer>>();
Map<String,Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++) {
map.put(words[i], i);
}
for (int i = 0; i < words.length; i++) {
for (int j = 0; j <= words[i].length(); j++) {
String subString=words[i].substring(0,j);
String subString2=words[i].substring(j);
if (isPalindrome(subString)) {
if (map.get(reverseString(subString2))!=null) {
ArrayList<Integer> arrayList=new ArrayList<Integer>(2);
arrayList.add(map.get(reverseString(subString2)));
arrayList.add(map.get(words[i]));
if (!arrayList.get(0).equals(arrayList.get(1))) {
resultList.add(arrayList);
}
}
}
if (isPalindrome(subString2)) {
if (map.get(reverseString(subString))!=null) {
ArrayList<Integer> arrayList=new ArrayList<Integer>(2);
arrayList.add(map.get(words[i]));
arrayList.add(map.get(reverseString(subString)));
if (!arrayList.get(0).equals(arrayList.get(1))) {
resultList.add(arrayList);
}
}
}
}
}
HashSet hashSet=new HashSet(resultList);
resultList.clear();
resultList.addAll(hashSet);
return resultList;
}
public boolean isPalindrome(String string) {
char c[]=string.toCharArray();
boolean flag=true;
for (int i = 0; i < c.length/2; i++) {
if (c[i]!=c[c.length-i-1]) {
flag=false;
break;
}
}
return flag;
}
public String reverseString(String string) {
char[] chars=string.toCharArray();
char[] result=new char[chars.length];
for (int i = 0; i < chars.length; i++) {
result[chars.length-i-1]=chars[i];
}
return new String(result);
}
}
Palindrome Pairs的更多相关文章
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- 336. Palindrome Pairs(can't understand)
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
随机推荐
- 使用Visual Assistant X创建C程序注释模板
本文将讲解C程序注释模板的使用背景.创建方法,并在结束时进行总结. 1.使用背景 在项目开发过程中,为方便组内其他成员能够快速学习自己编写的代码,需要对自己写的函数添加注释.在正规的软件开发流程中,一 ...
- 将txt文件转换成EXCEL文件的方法
地址:http://wenku.baidu.com/view/fcdbe8cca1c7aa00b52acbad.html 1.在EXCEL程序中点击“打开”,将文件类型选择为“文本文件”,找到以前用过 ...
- OGC学习课程
1.引言 由于项目需要,需要学习OGC相关地图标准,包括WMS.WFS.GML.SLD等,只是国内相关书籍大家都懂的,特向Google大师请教,得一秘籍<Open Web Mapping> ...
- 使用BigDecimal进行精确运算
首先我们先来看如下代码示例: 1 public class Test_1 { 2 public static void main(String[] args) { 3 System.out.print ...
- centos6 安装python2.7
yum -y install centos-release-scl yum -y install python27 临时生效 scl enable python27 bash 登录自动生效 cat & ...
- 关于html自闭合标签要不要加空格和斜杠的问题?
问题描述:可能很多人都遇到过这个问题,写网页时,link img br input等等这些标签时到底要不要在结尾加上空格和斜杠呢? 我曾经貌似在<编写高质量代码>上看到过这样的介绍,遇到l ...
- MFC创建文件和文件夹
1.使用PathIsDirectory判断文件夹是否存在需要引用下面头文件: #include "shlwapi.h"#pragma comment(lib,"shlwa ...
- PHP集成百度Ueditor 1.4.3
下载安装 1.首先到官网下载最新版的UE1.4.3UE官方下载地址:http://ueditor.baidu.com/website/download.html#ueditor 这里我下载的是1.4. ...
- PHP+Mysql+jQuery实现文件下载次数统计
数据表 CREATE TABLE IF NOT EXISTS `downloads` ( `id` int(6) unsigned NOT NULL AUTO_INCREMENT, `file ...
- ThinkPHP升级指导
升级指导 http://www.kancloud.cn/manual/thinkphp5/163239 从V5.0.1升级到V5.0.2 从V5.0.1升级到V5.0.2需要注意如下事项: 下列模型属 ...