列表

详情


JAVA42. 统计一句话中重复单词的个数

描述

统计控制台输入的一句话中不同字⺟字符出现的次数。例如:现有字符串"Hello World!",上述字符串中各个字符的出现的次数为:

H:1
e:1
l:3
o:2
W:1
r:1
d:1
(不考虑数字、空格和特殊字符的个数,按照字符在字符串中出现的顺序显示。相同字母的大小写算两个不同字符


输入描述

控制台任意输入一段话,可以有空格和特殊符号

输出描述

输出字符和字符对应的出现字数(字符和出现次数之间用:隔开,输出逻辑已经给出)

示例1

输入:

Hello World

输出:

H:1
e:1
l:3
o:2
W:1
r:1
d:1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 11ms, 内存消耗: 9588KB, 提交时间: 2022-08-03

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String line = bufferedReader.readLine();
            Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
            char arr[] = line.replace(" ","").toCharArray();
            for(int i= 0;i<arr.length;i++){
                if(map.get(arr[i])!=null){
                    map.put(arr[i],map.get(arr[i])+1);
                }else{
                    map.put(arr[i],1);
                }
            }

            Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
            for (Map.Entry<Character, Integer> entry : entrys) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Java 解法, 执行用时: 11ms, 内存消耗: 9660KB, 提交时间: 2022-06-28

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String line = bufferedReader.readLine();
            Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
            //先把空格转为“”,在转为char数组
            char arr[] = line.replace(" ","").toCharArray();
            //遍历
            for(int i= 0;i<arr.length;i++){
                //判断是否出现过
                if(map.get(arr[i])!=null){
                    map.put(arr[i],map.get(arr[i])+1);
                }else{
                    map.put(arr[i],1);
                }
            }
            
            Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
            for (Map.Entry<Character, Integer> entry : entrys) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Java 解法, 执行用时: 13ms, 内存消耗: 9508KB, 提交时间: 2022-06-15

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String line = bufferedReader.readLine();
            Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
            char[]chars = line.replace(" ","").toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if(map.get(chars[i])!=null){
                    map.put(chars[i],map.get(chars[i])+1);
                }else{
                    map.put(chars[i],1);
                }
            }
            Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
            for (Map.Entry<Character, Integer> entry : entrys) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Java 解法, 执行用时: 23ms, 内存消耗: 10388KB, 提交时间: 2022-02-09

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();

        //write your code here......
        for(int i=0;i<line.length();i++){
            char ch=line.charAt(i);
            if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){
                if(map.containsKey(ch)==true){
                    map.put(ch,map.get(ch)+1);
                }else{
                    map.put(ch,1);
                }
            }
        }
        Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrys) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

Java 解法, 执行用时: 24ms, 内存消耗: 10520KB, 提交时间: 2022-02-08

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();

        //write your code here......
        for(char c : line.toCharArray()){
            if(c!=' '){
                if(map.containsKey(c)){
                    map.replace(c,map.get(c)+1);
                }
                else{
                    map.put(c,1);
                }
            }
        }

        Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrys) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

上一题