英文:
(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)
的值是索引为 i
处 str
的字符。
由于每个字符对应一个整数值(更多信息请参阅此处),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['c']
because 'c'
can be represented as number (see ASCII table).
So basically
for(int i=0; i<len; i++)
count[str.charAt(i)]++;
is counting how many times the same letter appears in string.
so for input string "aabc"
count['a'] = 2
count['b'] = 1
count['c'] = 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 = ' ';
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论