统计字符(去哪儿2017校招真题)
2017-09-04 16:30
423 查看
//import java.util.HashMap; //import java.util.Map; //import java.util.Scanner; // ///** // * 统计字符 // * // * 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。 // * // */ //public class Main { // @SuppressWarnings("resource") // public static void main(String[] args) { // Scanner in = new Scanner(System.in); // String string = in.nextLine(); // System.out.println(Main.countChar(string)); //// System.out.println(Main.binarySearch(string.toCharArray(), 'f')); // } // //// /** //// * 算法思想描述: //// * 使用一个辅助数组foundChar, //// * 遍历字符串str, 对于每个字符, 查找(先使用顺序查找,后使用二分查找) //// * 在foundChar数组中是否包含该字符 //// * 若找到, index++; //// * 若未找到, 将其插入foundChar数组, index++; //// * //// * 如果index == 3, break; //// * //// * 如何为每一个字符做一个计数器? //// * //// * //// * @param str //// */ //// public static char countChar(String str) { //// int strLen = str.length(); //// char[] foundChar = new char[strLen]; //// int[] index = new int[strLen]; //// for(int i: index) System.out.print(" " + index[i]); //// System.out.println(); //// char temp; //// //// int tempInt; //// //// for(int i = 0; i < strLen; i++) { //// temp = str.charAt(i); ////// System.out.println(temp); ////// System.out.println(search(foundChar, temp)); //// if(search(foundChar, temp) >= 0) { //// foundChar[i] = temp; //// tempInt = search(foundChar, temp); //// index[i] = index[tempInt] + 1; //// System.out.println("查找到:" + index[i]); //// } else { //// foundChar[i] = temp; ////// System.out.println(foundChar[i]); //// tempInt = index[i]; //// index[i] = tempInt + 1; //// System.out.println("未查找到:" + index[i]); //// } //// //// if(index[i] == 3) return foundChar[i]; //// } //// //// return 0; //// } //// //// /** //// * 顺序查找——遍历数组,查找数组中是否包含指定的元素 //// * //// * @param chars:待查找字符数组 //// * @param target:待查找字符 //// * @return //// */ //// public static int search(char[] chars, char target) { //// if(chars.length == 0 || chars == null) return -1; //// //// for(int i = 0; i < chars.length; i++) { //// if(target == chars[i]) return i; //// } //// //// return -1; //// } //// //// /** //// * 二分查找算法——优化版 //// * //// * @param chars:待查找数组 //// * @param target:待查找元素 //// * @return //// */ //// public static int binarySearch(char[] chars, char target) { //// //// // 输入参数检查 //// if (chars == null || chars.length == 0 || //// target < chars[0] || target > chars[chars.length - 1]) //// return -1; //// //// int left = 0; // 左指针 //// int right = chars.length - 1; // 右指针 //// //// // 目标值的查找 //// while (left < right - 1) { //// int mid = left + (right - left) / 2; // 中间值 //// //// if (chars[mid] < target) left = mid + 1; // 目标值在后半部分 //// else if (chars[mid] > target) right = mid - 1; // 目标值在前半部分 //// else right = mid; // 中间值的重新赋值 //// } //// //// // 返回目标值的位置 //// if (chars[left] == target) return left; //// else if (chars[right] == target) return right; //// else return -1; //// } // // public static int countChar(String str) { // int strLen = str.length(); // char[] foundChar = new char[strLen]; // int[] index = new int[strLen]; // char temp; // // Map<Character, Integer> map = new HashMap<>(); // // for(int i = 0; i < strLen; i++) { // temp = str.charAt(i); // if(search(foundChar, temp) >= 0) { // Integer integer = map.get(temp); // map.replace(temp, integer + 1); // } else { // map.put(temp, index[i]++); // } // // if(map.get(temp) == 3) return map.get(temp); // } // // return 0; // } // // /** // * 顺序查找——遍历数组,查找数组中是否包含指定的元素 // * // * @param chars:待查找字符数组 // * @param target:待查找字符 // * @return // */ // public static int search(char[] chars, char target) { // if(chars.length == 0 || chars == null) return -1; // // for(int i = 0; i < chars.length; i++) { // if(target == chars[i]) return i; // } // // return -1; // } //} import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * 题目描述: * * 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。 * */ public class Main { @SuppressWarnings("resource") public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < str.length(); i++) { if(map.containsKey(str.charAt(i)) && isEnglish(str.charAt(i))) { int temp = (Integer) map.get(str.charAt(i)); map.put(str.charAt(i), temp + 1); if((Integer)map.get(str.charAt(i)) == 3) { System.out.println(str.charAt(i)); break; } }else{ map.put(str.charAt(i), 1); } } } public static boolean isEnglish(char a){ if((a > 'a' && a < 'z') || (a > 'A' && a < 'Z')) return true; return false; } }
相关文章推荐
- 【newCoder】2017春招真题 [编程题] 统计字符
- 循环二叉树(去哪儿2017校招真题)
- 2的次方(去哪儿2017校招真题) & O(1)时间检测2的幂次(LintCode)
- 联通图形(去哪儿2017校招真题)
- 2017校招真题1 星际穿越
- 2017校招真题编程训练--幸运数 进制转换
- 1042. 字符统计(20)-PAT乙级真题
- 删除公共字符(好未来2017秋招笔试真题二)
- 【nowcoder-2017校招真题】保留最大的数
- 2017校招真题编程训练--水仙花树
- 【真题】京东2017校招编程题 集合
- hd 2017 统计字符串 逐个读取字符法注意getchar();
- 美团点评2017校招笔试真题-算法工程师A
- 2017-统计字符个数
- 2017校招真题在线编程
- 九度考研真题 浙大 2007-1浙大 题目1021:统计字符 字符串输入
- 头条校招(今日头条2017秋招真题)
- 2017好未来校招真题----倒置字符串
- 2017校招真题编程训练--求数列的和
- 题目1021:统计字符 2007年浙江大学计算机及软件工程研究生机试真题