Title Description:
The first occurrence of a character is found in a string (1<= string length <=10000, all composed of uppercase letters). If you enter Abaccdeff, then output B.
Input:
Enter more than one set of data
Enter a string for each group.
Output:
Output the first occurrence of a character Poute, no one-time characters are output-1
We have two ways to solve this problem.
1. Brute force method.
Iterating through the array, each character is found to traverse the entire array, to see if the character exists in the array below the table is inconsistent, but the Assic code value of the same character exists, if it does not exist, it is returned that character, if present, continue to traverse. The time complexity of the algorithm is O (n*n), obviously this is not very high efficiency.
1 /**2 * Using two cycles, time complexity O (n*n)3 * 4 * @authorYfy5 * @paramargs6 * String to search for7 * @paramLen8 * String Length9 * @returnreturns the Assic code value of the first occurrence of a character, or 1 if there are no characters appearing only once.Ten */ One intFirstnotrepeatingchar (Char[] args,intLen) { A intJ; - for(inti = 0; i < Len; i++) { - for(j = 0; J < Len; J + +) { the if(I! =j) { - if(Args[i] = =Args[j]) { - Break; - } + } - } + //No characters are found that are equal to this character. A if(J = =Len) { at returnArgs[i]; - } - } - return-1; -}
View Code
Finds the first occurrence of a character in a string