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的更多相关文章

  1. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  2. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  3. 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 ...

  4. 【题解】Palindrome pairs [Codeforces159D]

    [题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...

  5. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  6. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  9. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  10. 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 ...

随机推荐

  1. 没想到cnblog也有月经贴,其实C#值不值钱不重要。

    呵呵,就不倚老卖老了,从basic走过来,一路经历vb,vf,delphi,C#,php,asp,html,js,css,太多太多的开发语言,包括面向对象编程思想,语义化页面结构等等,除了高级的编程技 ...

  2. showModalDialog 的重要提示

    模态对话框,没有opener,不能用window.opener.location.reload();或window.parent.location.reload();要通过返回值来判断关闭后刷新. f ...

  3. Birt导出Excel图片

    有一段时间没有使用Birt了,最近突然之间发现新版的Birt可以支持导出Excel附带图片.我目前下载的是Birt 4.3版本的,导出图片的也只能在Excel 2007下面能够实现,2003的xls格 ...

  4. 造轮子之数据库对比工具DataBaseComparer

    最近同时在维护好几个项目,有些项目是SqlServer的,另一些是MySql的,DBA推荐了一个线上库和线下库的对比工具,用的时候经常会在对比时,半天都没有进度.索性自己这次造个轮子,做了一个纯对比数 ...

  5. qt 环境下mapx组件打包后编译产生c2248和c2512错误

    C:\Qt\Qt5.6.0\5.6\msvc2013\include\QtCore\qmetatype.h:760: error: C2248: “MapSpace::IRowCursor::IRow ...

  6. SQL存储过程解密

    首先要建立一张表和一个存储过程: SQL_DECODE表: CREATE TABLE [dbo].[SQL_DECODE]( ,) NOT NULL, [SQLTEXT] [nvarchar](max ...

  7. jquery 上传回显图片预览

    /******************************************************************************* * 异步上传文件,兼容IE8,火狐和谷 ...

  8. Error:Execution failed for task ':app:dexDebug'.

    com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ...

  9. zxing--条码图像处理库

    ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口.Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码.   该项目可实现的条形码 ...

  10. 关于shape_trans (ConnectedRegions, ConvexRegions, 'convex')的作用于对比

    * crystal.hdev: extraction of hexagonally shaped crystals via local thresholding and region post-pro ...