POJ1002

對于我來說有點難。
在自己的電腦上沒問題,但是oj上始終過不了~
不知道為什么為報錯


問題描述###

簡單的映射和替換


難點###

無~


代碼實現(xiàn)###

package poj; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.Scanner; public class Poj1002 { public static void main(String[] args){ Scanner in =new Scanner(System.in); String s = in.nextLine(); int c = Integer.valueOf(s); HashMap<String,Integer> map = new HashMap<String, Integer>(); ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 0 ; i < c ; i ++){ String tmp = in.nextLine(); tmp = tmp.replaceAll("-", ""); tmp = tmp.replaceAll("[A-C]", "2"); tmp = tmp.replaceAll("[D-F]", "3"); tmp = tmp.replaceAll("[G-I]", "4"); tmp = tmp.replaceAll("[J-L]", "5"); tmp = tmp.replaceAll("[M-O]", "6"); tmp = tmp.replaceAll("[P,R,S]", "7"); tmp = tmp.replaceAll("[T-V]", "8"); tmp = tmp.replaceAll("[W-Y]", "9"); tmp = tmp.replaceAll("[Q,Z]", ""); Integer count = map.get(tmp); if(count == null){ map.put(tmp, new Integer(1)); list.add(Integer.valueOf(tmp)); } else map.put(tmp, new Integer( count +1)); } in.close(); list.sort(new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { if(o1.equals(o2)) return 0; else if(o1 < o2) return -1; else return 1; } }); // for(int i = 0 ; i < list.size() ; i ++){ // for(int j = 0 ; j < list.size() -1 ; j ++){ // Integer aV = list.get(j); // Integer bV = list.get(j+1); // if(aV > bV){ // list.set(j, bV); // list.set(j+1, aV); // } // } // } boolean no = true; for(Integer key : list){ String ss = key.toString(); if(map.get(ss) == 1){ continue; } no = false; ss = ss.substring(0, 3)+"-"+ss.substring(3, 7); System.out.println(ss+" "+map.get(key.toString())); } if(no) System.out.println("No duplicates. "); } }
使用ArrayList的sort方法會報compiler error,位置就在sort方法那里
使用自己寫的冒泡會報runtime error。
為什么不是超時而是RE呢?不能理解。

2014.08.20 22:00 Update###

找到一個AC的代碼,發(fā)現(xiàn)其為排序,和題目的降序排列不符,但是居然AC了??為什么
自己的代碼不排序的話,就是WA~
代碼如下:
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.IOException; import java.util.Map; import java.util.TreeMap; public class Main { @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { DataInputStream input = new DataInputStream(new BufferedInputStream(System.in)); int number = Integer.parseInt(input.readLine()); Integer count; Map<String, Integer> map = new TreeMap<String, Integer>(); for (int i = 0; i < number; i++) { String phoneNumber = input.readLine().replace("-", ""); StringBuffer sb = new StringBuffer(); for (int j = 0; j < 7; j++) { char c = phoneNumber.charAt(j); if (Character.isDigit(c)) { sb.append(c); continue; } if (c == 'A' || c == 'B' || c == 'C') { c = '2'; } if (c == 'D' || c == 'E' || c == 'F') { c = '3'; } if (c == 'G' || c == 'H' || c == 'I') { c = '4'; } if (c == 'J' || c == 'K' || c == 'L') { c = '5'; } if (c == 'M' || c == 'N' || c == 'O') { c = '6'; } if (c == 'P' || c == 'R' || c == 'S') { c = '7'; } if (c == 'T' || c == 'U' || c == 'V') { c = '8'; } if (c == 'W' || c == 'X' || c == 'Y') { c = '9'; } sb.append(c); } String result = sb.toString(); if (map.containsKey(result)) { count = map.get(result) + 1; map.put(result, count); } else { map.put(result, 1); } } boolean flag = false; for (String key : map.keySet()) { count = map.get(key); if (count > 1) { flag = true; System.out.println(key.substring(0, 3) + "-" + key.substring(3) + " " + count); } } if (!flag) { System.out.println("No duplicates. "); } } }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,899評論 0 33
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,642評論 18 399
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,494評論 0 6
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,011評論 0 11
  • 這篇文章中,我們來講述意外傷害的預(yù)防和緊急的處理。 一、跌傷和碰傷 在我們生活中經(jīng)常都會跌傷和碰傷,無論是成人還是...
    酥小栗閱讀 362評論 0 2

友情鏈接更多精彩內(nèi)容