您的位置:首页 > 其它

统计字符(去哪儿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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  去哪儿