(Java代码)返回输入字符串中出现次数最多的字符

huangapple go评论90阅读模式
英文:

(Java Code) Return maximum occurring character in an input string

问题

以下是要翻译的部分:

"Can anyone please explain the process of this for loop which comes first in the code. If you print these two lines in the console, you get output [0 0 0 1 2]. I don't know 'how it works behind the scene to increment the character count every time'."

第一个for循环的过程可以解释如下。如果你在控制台中打印这两行,你会得到输出[0 0 0 1 2]。我不知道它是如何在幕后工作的,每次都会增加字符计数。

英文:

Can anyone please explain the process of this for loop which comes first in the code. If you print these two lines in the console, you get output [0 0 0 1 2]. I don't know "how it works behind the scene to increment the character count every time".

for (int i=0; i<len; i++) 
 count[str.charAt(i)]++; 

//Code
     public class GFG  
        { 
            static final int ASCII_SIZE = 256; 
            static char getMaxOccuringChar(String str) 
            { 
                // Create array to keep the count of individual 
                // characters and initialize the array as 0 
                int count[] = new int[ASCII_SIZE]; 
               
                // Construct character count array from the input 
                // string. 
                int len = str.length(); 
                for (int i=0; i<len; i++)   //bit confused lines
                    count[str.charAt(i)]++; 
               
                int max = -1;  // Initialize max count 
                char result = ' ';   // Initialize result 
               
                // Traversing through the string and maintaining 
                // the count of each character 
                for (int i = 0; i < len; i++) { 
                    if (max < count[str.charAt(i)]) { 
                        max = count[str.charAt(i)]; 
                        result = str.charAt(i); 
                    } 
                } 
               
                return result; 
            } 
              
            // Driver Method 
            public static void main(String[] args) 
            { 
                String str = "abcaa"; 
                System.out.println("Max occurring character is " + 
                                    getMaxOccuringChar(str)); 
            } 
        } 

答案1

得分: 3

循环遍历字符串,str.charAt(i) 的值是索引为 istr 的字符。

由于每个字符对应一个整数值(更多信息请参阅此处),count[str.charAt(i)]++; 增加了给定字符对应的整数值的 count 数组中的索引的值(请参阅ASCII 表 查看对应的值)。

因此在循环结束后,count 包含了字符串 str 中每个 ASCII 字符出现的次数。

英文:

The for loop iterates over the string, the value of str.charAt(i) is the character of str at the index i.

Since every char corresponds to an int value (see more about that here) count[str.charAt(i)]++; increases the value of the count array at the index of the given char's corresponding int (see the ascii table to see which one that is).

So after the for loop count contains the number of occurences of every ascii character in str.

答案2

得分: 1

str.charAt(i)` 返回在字符串 `str` 中位置为 `i` 的字符。字符可以用作数组的索引,例如 `myarray['c']`,因为 `'c'` 可以表示为数字(参见 ASCII 表)。

因此基本上
`for(int i=0; i<len; i++)
  count[str.charAt(i)]++;`

正在计算相同字母在字符串中出现的次数。

所以对于输入字符串 "aabc"
`count['a'] = 2`
`count['b'] = 1`
`count['c'] = 1`
英文:

str.charAt(i) return char from str at the i position. Char can be used as index in array for example myarray[&#39;c&#39;] because &#39;c&#39; can be represented as number (see ASCII table).

So basically
for(int i=0; i&lt;len; i++)
count[str.charAt(i)]++;

is counting how many times the same letter appears in string.

so for input string "aabc"
count[&#39;a&#39;] = 2
count[&#39;b&#39;] = 1
count[&#39;c&#39;] = 1

答案3

得分: 0

    public static char getMaxOccuringChar(String str) {
        char result = ' ';
        int len = str.length();
        HashMap<Character, Integer> StrArray = new HashMap<Character, Integer>();
        int val = 1;
        for(int i = 0; i < len; i++) {
            char temp = str.charAt(i);
            if(!StrArray.containsKey(temp)) {
                StrArray.put(temp, val);
            } else {
                StrArray.put(temp, StrArray.get(temp) + 1);
            }
        }
        int MaxVal = 0;
        for(char i : StrArray.keySet()) {
            if(StrArray.get(i) > MaxVal) {
                result = i;
                MaxVal = StrArray.get(i);
            }
        }
        return result;
    }
英文:

Maximum occurring character in a single input string

public static char getMaxOccuringChar(String str) {
	char result = &#39; &#39;;
	int len = str.length();
	HashMap&lt;Character, Integer&gt; StrArray = new HashMap&lt;Character, Integer&gt;();
	int val = 1;
	for(int i =0; i&lt;len; i++) {
		char temp = str.charAt(i);
		if(!StrArray.containsKey(temp)) {
			StrArray.put(temp, val);
		}else {
			StrArray.put(temp, StrArray.get(temp)+1);
		}
	}
	int MaxVal = 0;
	for(char i : StrArray.keySet()) {
		if(StrArray.get(i) &gt; MaxVal) {
			result = i;
			MaxVal = StrArray.get(i);
		}
	}
	return result;
}

huangapple
  • 本文由 发表于 2020年4月6日 03:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61048586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定