码迷,mamicode.com
首页 > 其他好文 > 详细

一些题目

时间:2014-09-23 01:19:53      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   ar   for   div   sp   

http://blog.csdn.net/zheng0518/article/details/39189805

  1. 给定一个字符串,得到这个字符串中首先出现两次的那个字符

方法一:

 1 public static void main(String[] args) {
 2         Scanner scanner = new Scanner(System.in);
 3         String src = scanner.next();
 4         HashMap<Character,Integer> items = new HashMap<Character,Integer>();
 5         for(int i=0;i<src.length();i++){
 6             if(!items.containsKey(src.charAt(i))){
 7                 items.put(src.charAt(i), 1);
 8             }else{
 9                 System.out.println(src.charAt(i));
10                 break;
11             }
12         }
13     }

方法二:打表

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         char[] array = {‘a‘,‘s‘,‘d‘,‘a‘,‘s‘};
 5     
 6         if (getFirstTwiceChar(array) != -1) {
 7             System.out.println(array[getFirstTwiceChar(array)]);
 8         }
 9         
10     }
11     
12     public static int getFirstTwiceChar(char[] array){
13         
14         if (array == null || array.length < 2) {
15             return -1;
16         }
17         /* 一个整数数组,下标表示字符的大小*/
18         int[] table = new int[256];
19         
20         for (int i = 0; i < array.length; i++) {
21             
22             if(table[array[i]] == 0){
23                 /* 将首次出现的字符在数组中置1 */
24                 table[array[i]] = 1;
25             } else {
26                 /* 字符第二次出现 */
27                 return i;
28             }
29             
30         }
31         return -1;
32     }
33 }
  1. 尝试在以下文本中搜索并打印出包含单词"your"(不区分大小写)的句子,并按照出现次数从高到低排序

Make yourself at home
None of your business
I will be more careful
How about going to a move?
Your life is your own affair

 

 1 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main {
 6 
 7     
 8     public static void main(String[] args) {
 9         
10         Scanner input = new Scanner(System.in);
11         ArrayList<String> strings = new ArrayList<>();
12         
13         while(input.hasNext()){
14             strings.add(input.nextLine());
15         }
16         
17         int[] counts = new int[strings.size()];
18         /* 用于跟踪位置的变化 */
19         int[] labels = new int[strings.size()];
20         
21         for (int i = 0; i < labels.length; i++) {
22             labels[i] = i;
23         }
24         
25         for (int i = 0; i < counts.length; i++) {
26             counts[i] = countSubstr(strings.get(i), "your", true);
27         }
28         sort(counts, labels);
29         for (int i = 0; i < labels.length; i++) {
30             System.out.println(strings.get(labels[i]));
31         }
32     }
33     /**
34      * 冒泡排序
35      * @param counts
36      * @param labels
37      */
38     public static void sort(int[] counts, int[] labels){
39         
40         for(int i = counts.length-1; i > 0 ; i--){
41             for(int j = 0; j < i; j++){
42                 /* 从高到低 */
43                 if (counts[j] < counts[j+1]) {
44                     
45                     int temp = counts[j];
46                     counts[j] = counts[j+1];
47                     counts[j+1] = temp;
48                     
49                     /* 跟踪位置的变化 */
50                     int t = labels[j];
51                     labels[j] = labels[j+1];
52                     labels[j+1] = t;
53                     
54                 }
55                 
56             }
57         }
58     }
59     
60     public static int countSubstr(String src, String sub, boolean isIgnore) {  
61         int count = 0, start = 0;  
62         if (isIgnore) {  
63             src = src.toLowerCase();  
64             sub = sub.toLowerCase();  
65         }  
66         while ((start = src.indexOf(sub, start)) >= 0) {  
67             start += sub.length();  
68             count++;  
69         }  
70         return count;  
71     }   
72 }

 

一些题目

标签:style   blog   http   color   java   ar   for   div   sp   

原文地址:http://www.cnblogs.com/wanghui390/p/3987189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!