中级农民
- 积分
- 135
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-7-23
- 最后登录
- 1970-1-1
|
- public List<List<String>> getPairs(Set<String> dict){
- List<List<String>> result = new ArrayList<List<String>>();
- for(String s:dict){
- for(int i=0 ; i<s.length(); ++i){
- String s1 = s.substring(0, i);
- String s2 = s.substring(i, s.length());
- if(isPal(s1)){
- judge(result, s, s2, dict, false);
- }
- if(isPal(s2)){
- judge(result, s, s1, dict, true);
- }
- }
- }
- return result;
- }
- public void judge(List<List<String>> result, String s, String compare, Set<String> dict, boolean position){
- char[] array = compare.toCharArray();
- for(int j=0; j<compare.length()/2; ++j){
- char c = array[j];
- array[j] = array[compare.length()-j-1];
- array[compare.length()-j-1] = c;
- }
- String reverse = new String(array);
- if(dict.contains(reverse) && !reverse.equals(s)){
- List<String> list = new ArrayList<String>();
- if(position){
- list.add(s);
- list.add(reverse);
- }
- else{
- list.add(reverse);
- list.add(s);
- }
- result.add(list);
- }
- }
- public boolean isPal(String s){
- if(s.length()<=1)
- return true;
- int left = 0;
- int right = s.length()-1;
- while(left<right){
- if(s.charAt(left)!=s.charAt(right))
- return false;
- ++left;
- --right;
- }
- return true;
- }
复制代码 |
|