赞
踩
题目: 随机输入一串字符,请按照字符串中字符的频率和ASCII排序。
例如:
输入:ababccc 输出:cccaabb
输入:tree 输出:eert
输入:abbacccddddyyy 输出:ddddcccyyyaabb
解题思路:字符串中获取单个字符的个数,根据个数从大到小降序排列,如果个数相同则根据ASCII码来排序:
首先,需要记录每个字符的个数,其次要根据个数排序从大到小降序排列,判断个数相同再根据ASCII码排序;
知识点:1. 正则记录个数,或者循环记录每个字符的个数;
2.然后排序,先根据个数排序(排序的方法 android中Collections.sort可以对对象排序)
3.ASCII码的获取,String.toCharArray()获取单个字符; char类型可以直接转换为int, int类型就是字符的ASCII
看到这道题的时候,有思路但真正着手写的时候,基础的代码排序之类的都忘记如何使用了,为了能写出来,所以禁止百度使劲尝试了一下,但是方法性能低、代码繁琐,可以粗略参考一下我也就是记录一下:
private static void getSortStr1() { Scanner sc = new Scanner(System.in); System.out.println("输入字符串"); String str = sc.next(); char[] chars = str.toCharArray(); //频率集合 HashMap<String, Integer> mapNum = new HashMap<>(); for (int i = 0; i < chars.length; i++) { String s = String.valueOf(chars[i]); if(mapNum.size()<=0){ mapNum.put(s, 1); }else { boolean isEqual = false; Iterator<Map.Entry<String, Integer>> iterator = mapNum.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String, Integer> next = iterator.next(); String key = next.getKey(); Integer value = next.getValue(); if (s.equals(key)) { ++value; mapNum.put(s, value); isEqual = true; } } if(!isEqual){ mapNum.put(s, 1); } } } //频率排序 String[] strPx = mapNum.keySet().toArray(new String[0]); for (int i = 0; i < strPx.length - 1; i++) { for (int j = i + 1; j < strPx.length; j++) { Integer value_i = mapNum.get(strPx[i]); Integer value_j = mapNum.get(strPx[j]); if (value_i < value_j) { String tmp = strPx[j]; strPx[j] = strPx[i]; strPx[i] = tmp; } else if (value_i == value_j) { //排序ASCII int ascii_i = strPx[i].toCharArray()[0]; int ascii_j = strPx[i].toCharArray()[1]; if (ascii_i > ascii_j) { String tmp = strPx[j]; strPx[j] = strPx[i]; strPx[i] = tmp; } } } } StringBuffer sb = new StringBuffer(); for (int i = 0; i < strPx.length; i++) { String px = strPx[i]; for (int j = 0; j < mapNum.get(px); j++) { sb.append(px); } } System.out.println(sb.toString()); }
参考了同事的写法后,精简百度版
private void getSortStr2(){ String str = "abbacccddddyyy"; //排序的字符对象集合(字符、个数、ASCII) List<StrBean> strBeans = new ArrayList<>(); //获取单个字符的个数并构造字符对象 char[] chars = str.toCharArray(); for (char c:chars) { StrBean strBean = new StrBean(); Pattern pattern = Pattern.compile(Character.toString(c)); Matcher matcher = pattern.matcher(str); int num = 0; while (matcher.find()){ num++; } strBean.setNum(num); strBean.setAscii(c); strBean.setWord(Character.toString(c)); strBeans.add(strBean); } //根据num来排序,相等则用ASCII排序,否则用num排序 Collections.sort(strBeans, new Comparator<StrBean>() { @Override public int compare(StrBean t1, StrBean t2) { //用法t1-t2升序排序 t2-t1降序排序 t2-t1 == 0相等 if(t2.num - t1.num == 0){ return t1.getAscii() - t2.getAscii(); } return t2.num - t1.num; } }); //构造排序串 StringBuffer sb = new StringBuffer(); for (StrBean strBean:strBeans) { sb.append(strBean.getWord()); } String result = sb.toString(); Log.i("TAG", "jisuan2: " + result); } class StrBean{ private Integer num; private String word; private Integer ascii; public void setNum(Integer num) { this.num = num; } public void setWord(String word) { this.word = word; } public void setAscii(int ascii) { this.ascii = ascii; } public Integer getNum() { return num; } public String getWord() { return word; } public Integer getAscii() { return ascii; } @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); sb.append("\"num\":") .append(num); sb.append(",\"word\":\"") .append(word).append('\"'); sb.append(",\"ascii\":") .append(ascii); sb.append('}'); return sb.toString(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。