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 ...
随机推荐
- [转载]《民航科技》2012年4月专家论坛:罗喜伶《SWIM技术国际研究动态及对中国民航的借鉴意义》
专家介绍:罗喜伶,北京航空航天大学电子信息工程学院副教授,工学博士,硕士生导师,国家空管新航行系统技术重点实验室和协同式网络化空中交通管理系统研究教育部创新团队核心成员,民航空管广域信息系统专家组成员 ...
- day8-异常
异常处理 1.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! try: pass except Ex ...
- Access restriction错误解决办法
Access restriction错误, XX方法 is not accessible due to restriction on required library XXlib 解决方案: Ecli ...
- 在CentOS或RHEL防火墙上开启端口
转载自:https://linux.cn/article-4243-1.html 如果希望在服务器上提供服务,诸如CentOS或RHEL的企业级Linux发行版包含内置的强大防火墙,它们默认的防火墙规 ...
- git&sourcetree安装及在IntelliIJ下拉取项目基础使用
be careful: 1)git版本与Sourcetree版本最好一致 ,不能git为2.5,sourcetree为1.8 2)先安装git再安装Sourcetree 3)拥有git和sourcet ...
- opendove中的odgw所需要的内核模块
最近组里要做opendove相关的东西,需要odgw的一个kernel-module. 以前安装过,但备份不见了,在此做个链接备忘 : https://git.opendaylight.org/ger ...
- linux系统下的软连接与硬链接
前几天在linux系统下安装mongoDB,然后运行脚本导入数据的时候遇到了链接库查询不到的情况,如图 1所示.当时是通过创建软连接的方式解决的这个问题.虽然,通过网上的教程解决了这个问题,但是对于软 ...
- 【原创】使用Fiddler抓取手机网络包
一: 下载安装Fiddler 二: 打开 tools--Telerik Fiddler Options, 进行如下设置
- xml---sax操作
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>书名1& ...
- highCharts 饼图动态加载
饼图的动态加载 (1):导入样式 <script type="text/javascript" src="<%=request.getContextPath( ...