The following Huawei programming competition questions are sorted online, and the codes are all debugged by yourself. Since there are fewer answers to Java online, everyone is welcome to criticize and correct me. I also hope it will be a little helpful to the children's shoes that are preparing to use Huawei on the computer. Grow up during the practice, come on! ~~
1. Dining random check (30 points)
Problem description:
Due to the large number of people in a certain company, lunch is divided into multiple batches of meals, and the meal time is strictly required for each batch. And check the dining situation regularly. Please write a program to achieve random checks on dining.
Required to implement the function:
void check_lunch(int num, int time,int input[], int output[])
【Input】 int num, total number of people eating
int time, number of meals in batches
char input[], dining situation
【Output】 char output[], illegal dining situation
【Return】None
Note: For the situation where dining is divided into 3 batches, 12 people eat, the correct dining situation should be distributed as follows [1, 2, 3, 1, 2, 3, 1, 2, 3]. If it does not meet this distribution, it is a violation, and the corresponding position is 0 when output.
Example
1) Input: num = 12, time = 3, input = [1,2,3,3,1,3,1,1,1,1,2,3]
Output: output = [1,2,3,0,0,3,1,0,0,1,2,3]
2) Input: num = 11, time = 4, intput = [1,2,3,4,2,3,3,4,1,2,3]
Output: output = [1,2,3,4,0,0,3,4,1,2,3]
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ int num=11,time=4; int[] input={1,2,3,4,2,3,3,4,1,2,3}; // int[] output=new int[]{}; int[] output=new int[num]; HWCompetition hwc=new HWCompetition(); hwc.check_lunch( num, time, input, output); } void check_lunch(int num, int time,int input[], int output[]){ System.out.println(Arrays.toString(input)); int j=0; for(int i=0;i<num;i++){ int yushu=(i+1)%time; if(yushu!=0){ if(input[i]==yushu){ output[j]=yushu; } else output[j]=0; j++; } else{//The case of remainder==0 if(input[i]==time){ output[j]=time; } else output[j]=0; j++; } } System.out.println(Arrays.toString(output)); } }2. Enter Lenovo (30 points)
Problem description:
The input of the association function is a very practical function, please program and implement similar functions.
Required to implement the function:
void auto_complete(char *str, char *tmp, char *output)
【Input】 char *str, candidate string
char *tmp, input string
【Output】 int *output, a string matching for association
【Return】None
Note: Candidate strings are separated by spaces, and the input string matches only from the beginning of the string. Output the matching substrings, also separated by spaces. If there is no substring that matches successfully, an empty string is output.
Example
1) Input: str = chengdu chongqing, tmp = c
Output: output = chengdu Chongqing
2) Input: str = chengdu chongqing, tmp = che
Output: end = Chengdu
3) Input: str = beijing nanjing, tmp = jing
Output: end =
Method 1:
package com.sheepmu.text; import java.util.ArrayList; import java.util.List; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String str="chengdu chongqing"; String tmp="che"; String output=""; HWCompetition hwc=new HWCompetition(); hwc.auto_complete(str,tmp, output); } void auto_complete(String str,String tmp, String output){ String[] strs=str.split("//s"); List<String> list=new ArrayList<String>(); for(int i=0;i<strs.length;i++) list.add(strs[i]); System.out.println("list--->"+list); System.out.println("tmp--->"+tmp); char[] tmps=tmp.toCharArray(); int len_list=list.size(); int len_t=tmps.length; for(int j=0;j<len_list;j++){ int len_list_j=list.get(j).length(); char[] list_j=list.get(j).toCharArray(); for(int k=0;k<len_t;k++){ if(len_t>len_list_j){ list.remove(j); len_list--;//!!!!!!!!!!!!!!! j--;//!!!!!!!!!!!!!!!!!!!!!!!! j--;//!!!!!!!!!!!!!!!!!!!!!!!!!!! If this is not the case, there will be a problem, because after removing the size becomes 1, but j, that is, index becomes 1 break; } else {//The length of temp is smaller than the length of the cable if(tmps[k]!=list_j[k]){ list.remove(j); len_list--;//!!!!!!!!!!!!!!! j--;//!!!!!!!!! break; } } } } // output= list.toString();//This will [chengdu], which will have [] if(!list.isEmpty()) on both sides { StringBuffer sb=new StringBuffer(); sb.append("end="); for(String result:list){ sb.append(result+" ");//Add spaces! ! Finally, remove the " " of the tail; } output=sb.toString().trim();//!! } else{ output="end="; } System.out.println(output); } }Method 2:
package com.sheepmu.text; import java.util.ArrayList; import java.util.List; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String str="chengdu chongqing"; String tmp="che"; String output=""; HWCompetition hwc=new HWCompetition(); hwc.auto_complete(str,tmp, output); } void auto_complete(String str,String tmp, String output){ String[] strs=str.split("//s");//Same as below, it should only be added with /s,/d, etc. List<String> list=new ArrayList<String>(); for(int i=0;i<strs.length;i++) list.add(strs[i]); System.out.println("list--->"+list); System.out.println("tmp--->"+tmp); int len_list=list.size(); for(int j=0;j<len_list;j++){//There is another good method: !list.get(j).startsWith(tmp);!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if(!list.get(j).matches(tmp+"[az]*")){//The regular expression is cool!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! list.remove(j); len_list--; j--; } } if(!list.isEmpty()){ StringBuffer sb=new StringBuffer(); sb.append("end="); for(String result:list){ sb.append(result+" ");//Add spaces! ! Finally, remove the " " of the tail; } output=sb.toString().trim();//!! } else{ output="end="; } System.out.println(output); } } 3. Farm counting problems (20 points)
Problem description:
It is known that there are a group of chickens and rabbits in a certain farm, with a total of M heads and N feet. Calculate how many chickens and rabbits there are in total.
Required to implement the function:
public String getFowlsNum(int iHeadNum, int iFootNum, ArrayList iChickenNum, ArrayList iRabbitNum)
【Input】iHeadNum: The total number of headers
iFootNum: Number of total feet [Output] iChickenNum: Number of chickens
iRabbitNum: Number of rabbits [Return] "0": Number of chickens and rabbits that meet the requirements
"-1": No quantity matching the requirements was found
Example
Input: iHeadNum =201, iFootNum =604
Output: iChickenNum.add(100), iRabbitNum.add(101) Return: "0"
Input: iHeadNum =201, iFootNum =123
Output: iChickenNum.add(0), iRabbitNum.add(0) Return: "-1"
package com.sheepmu.text; import java.util.ArrayList; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ int iHeadNum=201; int iFootNum=604; ArrayList iChickenNum=new ArrayList(); ArrayList iRabbitNum=new ArrayList(); HWCompetition hwc=new HWCompetition(); hwc.getFowlsNum( iHeadNum,iFootNum,iChickenNum,iRabbitNum); } public String getFowlsNum(int iHeadNum,int iFootNum,ArrayList iChickenNum,ArrayList iRabbitNum){ if(iFootNum%2!=0){//!!! System.out.println("iChickenNum.add(0),iRabbitNum.add(0)"); return "-1";//If the number of feet is odd, it is obviously wrong, } else{ int ji=2*iHeadNum-iFootNum/2; int tui=iFootNum/2- iHeadNum; if(ji>=0&&tui>=0) System.out.println("iChickenNum.add("+ji+"),iRabbitNum.add("+tui+")"); return "0"; } } } 4. String compression (30 points)
Problem description:
Compress the given string according to specifications and output the compressed string. Compression specifications are: if the same characters are continuous, then compress it to "character + number", such as "aaaa" compressed to "a4"
Note: 1. Only a single character is compressed continuously, if bababa, it cannot be compressed.
2. The string to be compressed does not contain numbers and escape characters.
Required implementation method:
public String compressStr(String srcStr) [Input] srcStr: The string to be compressed [Output] None [Return] Example input of compressed string: srcStr = "aaacccddef" Return: "a3c3d2ef"
Method 1: (Use ArrayList) See Huawei Computer Summary Question 8
Method 2: (If you use String, those who are not as readable as above should be familiar with the API)
package com.sheepmu.text; import java.util.ArrayList; import java.util.List; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String str="abcddef"; HWCompetition hwc=new HWCompetition(); String result=hwc.compressStr(str); System.out.println(result); } public String compressStr(String str){ StringBuffer sb=new StringBuffer(); for(int i=0;i<str.length();i++){ if(str.length()==0) break; if(str.length()==1) sb.append(str.charAt(i));//For aaacccddef for(int j=i+1;j<str.length();j++){ if(str.charAt(i)==str.charAt(j)){ if(j==str.length()-1){//For aaacccddeff sb.append(str.length()).append(str.charAt(i)); str=str.substring(j);//The length is only 0 left. Must assign to a new str!!!!!!!!!!!!!!!!!!!!!!!!!!! break; } } else{//If(j==1) sb.append(str.charAt(i)); else sb.append(j).append(str.charAt(i)); System.out.println(sb.toString()); str=str.substring(j); i--; break; } } } return sb.toString(); } } 5. Sorting algorithm (20 points)
Problem description:
The given unordered integer array is sorted in descending order and the input unordered array is N and the type is unsigned int
Required to implement functions
void DscSort (const int InputArray[], unsigned int n, int OutputArray[])
【Input】InputArray: The given unordered array
n: array length [Output] OutputArray: Sort array [Return] No example input: InputArray={1,5,4,8,3,2,9,6,7,0}
Output: OutputArray={9,8,7,6,5,4,3,2,1,0}
Method 1: (Directly call the API) Idea: Ascending order and output reversely
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ int[] inputArray={1,5,4,8,3,2,9,6,7,0}; int n=inputArray.length; int[] outputArray=new int[n]; HWCompetition hwc=new HWCompetition(); hwc.dscSort (inputArray,n,outputArray); } void dscSort (int InputArray[], int n, int OutputArray[]){ Arrays.sort(InputArray);//Ascending int i=0; while(--n>=0){ OutputArray[i++]=InputArray[n]; } System.out.println(Arrays.toString(OutputArray)); } }Method 2: (If the question stipulates that the API cannot be called)
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ int[] inputArray={1,5,4,8,3,2,9,6,7,0}; int n=inputArray.length; int[] outputArray=new int[n]; HWCompetition hwc=new HWCompetition(); hwc.dscSort (inputArray,n,outputArray); System.out.println(Arrays.toString(inputArray)); } void dscSort (int InputArray[], int n, int OutputArray[]){//Write yourself: Fast-order descending int high=0; int low=n-1; sort(InputArray,high,low); } void sort(int InputArray[],int high,int low){ int i,j,temp; i=high;//High-end subscript j=low;//Low-end subscript temp=InputArray[i];//Please the first element as the standard element. while(i<j){//The recursive exit is low>=high while(i<j&&temp>InputArray[j])//The backend is smaller than temp and conforms to descending order. Regardless of it, the low subscript is moved forward j--;//After while it is finished, it refers to the larger temp that is larger than temp if(i<j){ InputArray[i]=InputArray[j]; i++; } while(i<j&&temp<InputArray[i]) i++; if(i<j){ InputArray[j]=InputArray[i]; j--; } }//While after, that is, the first disk sorting InputArray[i]=temp;//Put the temp value to its position. if(high<i) //Note that the subscript value sort(InputArray, high,i-1);//Recursive for the left terminal array if(i<low) //Note that the subscript value sort(InputArray,i+1,low);//Recursive for the right terminal array; Comparing the above example, in fact, i and j are the same as the following code!!!!!!!!!!!!!!! } }6. Find the largest non-repetition number (30 points)
Problem description
If there are no consecutive two digits in the same decimal expression, it is called "no repetition number". For example, 105, 1234, and 12121 are all "no repetition numbers", while 11, 100, and 1225 are not. Given a positive integer A, return the smallest "no repetition number" greater than A. A is less than 100,000
Required to implement functions
int getNotRepeatNum(int iValue)
【Input】lValue: The given number returns the minimum non-repeat number greater than the value [Output] None [Return] The minimum non-repeat number greater than the iValue Example input: iValue =54
Return: 56
Input: iValue =10
Return: 12
Input: iValue =98
Return: 101
Input: iValue =21099
Return: 21201
package com.sheepmu.text; import java.util.Arrays; import java.util.Scanner; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ Scanner input=new Scanner(System.in); int a=input.nextInt(); System.out.println("The input number is ---->"+a); HWCompetition hwc=new HWCompetition(); int result=hwc.getNotRepeatNum(a); System.out.println("Returns the minimum non-repeat number greater than "+a+"---->"+result) ; } int getNotRepeatNum( int iValue){ int i=0; for(i=iValue+1 ;i<100000;i++){ if(!isRepeatNum(i)){ break;//!!! Otherwise, it will run many times in vain} } return i; } public boolean isRepeatNum(int a){ String str=a+""; char[] cs=str.toCharArray(); int len=cs.length; for(int i=0;i<len-1;i++){//Because i+1 is needed later, if it is i<len, you must subscript to cross the bounds. if(cs[i]==cs[i+1]) return true; } return false; } } 7. Playing Card Comparison (30 points)
Problem description:
In poker, the types of cards include: A(1), 2, 3, 4, 5, 6, 7, 8, 9, T(10), J(11), Q(12), K(13), D(devilkin), B(belial).
Please make a simple program, enter the characters of two cards, such as "2" and "K", and judge the size of the card. The rules are as follows:
B>D>2>A>K>Q>J>10....>3 The smallest is 3
Judgment rules: Compare cFirstCard and cSecondCard. If FirstCar is large, return 1; if the same, return 0; if FirstCar is small, return -1.
Required to implement the function:
int CompareOneCard(char cFirstCard, char cSecondCard)
【Input】 char cFirstCard: The first card that needs to be compared
char cSecondCard: The second card that needs to be compared Note: the input is the characters 'A', '2',..., '9', 'T', 'J', 'Q', 'K', 'D', 'B'
[Return] int type: Return the comparison result of two cards. Note: The legality of the input is not necessary, and this is guaranteed by the user of the function. The cards entered are all characters '1', '2'…'9', capital 'A', 'T', 'J', 'Q', 'K', 'D', 'B'.
For example:
Enter: '4', '5', return: -1
Enter: '6', '6', return: 0
package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ char cFirstCard='5'; char cSecondCard= '6'; HWCompetition hwc=new HWCompetition(); int result=hwc.CompareOneCard(cFirstCard,cSecondCard); System.out.println("Result of comparison"+result); } int CompareOneCard(char cFirstCard, char cSecondCard){ int iF=getReallN(cFirstCard); System.out.println("iF--->"+ iF); int iS=getReallN(cSecondCard); System.out.println("iS--->"+ iS); return iF>iS ? 1: iF<iS? -1:0 ;//No need to add brackets} int getReallN(char c){ int value=0; switch(c){ case 'T': value=10; break; case 'J': value=11; break; case 'Q': value=12; break; case 'K': value=13; break; case 'A': value=14; break; case '2': value=15; break; case 'D': value=16; break; case 'B': value=17; break; case '3': case '4': case '5': case '6': case '7': case '8': case '9': // value=c; // Super stupid error!!! If you enter 9, '9'=57!!! The value of character 9 is not the number 9 but the value of the character itself. value=Integer.parseInt(c+"") ; break; } return value; } } 8. Glimps (30 points)
Problem description:
In Chengdu, a popular poker game is called "Dry Staring". Playing cards are used, including: A(1), 2, 3, 4, 5, 6, 7, 8, 9, T(10), J(11), Q(12), K(13).
Note: 10 is replaced with T, and the big ghosts and little ghosts are not considered here for the time being.
The size rules for two-hand cards are as follows:
a) Single card: 4 to 3, 5 to 4, and only when the two cards are just a little bigger can you compare. The order of comparison is: A>K>Q>J>T>9>8>7>6>5>4>3.
For example: 6 is greater than 5, but cannot be larger than 4, and 6 and 4 cannot be compared. Single card 2 is a special card, which can be compared with all other ordinary single cards and is the largest.
Please note that 3, it cannot be greater than any card.
b) Pair: that is, the points of the two cards are the same, the rules are similar to those of a single card, and similar processing is also required. Two 2 are special pairs and can be larger than all other pairs.
Note: You cannot compare the sub and single cards.
c) Bomb: 3 cards with the same number of points. A bomb can be larger than any single card and pair. The comparison between bombs does not need to be like a single card and pair. It can only be larger to compare.
As long as the rules are met: 222>AAA>KKK>QQ>JJJ>TT>…>333. That is, 222 is the largest, AAA can be greater than KKK or 333.
d) Other rules are not considered for the implementation. Now please implement a program to automatically determine the size of the cards in both hands. Note: the cards entered will only appear in three types: single, pair, and bomb. The maximum number of pictures is 3.
There will be no 2 single cards. For example, "25", there will be no pair plus a single card, such as "334", etc. You don't need to consider similar input exceptions.
However, pFirstCards is a single card and pSecondCards is a pair, and similar combination inputs are legal.
Required to implement the function:
int CompareCards(char *pFirstCards, char *pSecondCards)
【Input】 char *pFirstCards: First hand cards that need to be compared
char *pSecondCards: The second hand that needs to be compared [return] int type, return value description:
If pFirstCards and pSecondCards cannot be compared, such as "3" and "6"; "55" and "6", etc., return 0.
If pFirstCards is greater than pSecondCards, return 1.
If pFirstCards is equal to pSecondCards, return 2.
If pFirstCards is less than pSecondCards, return 3.
Note: The legality of the input is not necessary, this is guaranteed by the user of the function. The cards entered are all characters '1', '2'..'9', capital 'A', 'T', 'J', 'Q', 'K'.
Example input: "77", "33", return: 0
Enter: "77", "77", return: 2
Ideas: 1. 1vs2 or 2vs1 In this case, you only need to judge the length to get the result, no subsequent comparison is required
2. 1vs1 or 2 vs2 The same way to compare
3. Different comparison methods of 3vs3 and case 2
4. 3 vs non-3 or non-3 vs3
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String pFirstCards="QQ"; String pSecondCards="444"; HWCompetition hwc=new HWCompetition(); int result=hwc.CompareCards(pFirstCards,pSecondCards); System.out.println("Result of comparison"+result); } int CompareCards( String pFirstCards, String pSecondCards){ int len1=pFirstCards.length(); int len2=pSecondCards.length(); if((len1==1&&len2==2)||(len1==2&&len2==1))// 1vs2, cannot be compared. In order to provide efficiency, the ones that can be done first will be done, so there is no need to execute the following situation. return 0; int[] is1=getReallNs(pFirstCards); int[] is2=getReallNs(pSecondCards); // System.out.println(Arrays.toString(is1));//[12, 12] // System.out.println(Arrays.toString(is2));//[4, 4, 4] if((len1==1&&len2==1)||(len1==2&&len2==2)){//1vs1 or 2vs2, the comparison method is the same. In fact, there is no need for brackets if(Math.abs(is1[0]-is2[0])==1) //I know that length 2 is definitely a pair of cases return is1[0]-is2[0]>0? 1:3; else if(is1[0]==is2[0]) return 2; else return 0; } if(len1==3&&len2==3) //The bomb cannot be equal, because there are no 6 identical cards in a deck. return is1[0]>is1[0]? 1:3; if(len1==3&&len2<3||len1<3&&len2==3) return len1==3? 1:3; return 0;//In fact, the test cases should never execute this sentence. } int[] getReallNs(String s){ int len=s.length(); int[] cs =new int[len]; for(int i=0;i<len;i++){ cs[i]=getReallN(s.charAt(i)); } return cs; } int getReallN(char c){ int value=0; switch(c){ case 'T': value=10; break; case 'J': value=11; break; case 'Q': value=12; break; case 'K': value=13; break; case 'A': value=14; break; case '2': value=15; break; case '3': case '4': case '5': case '6': case '7': case '8': case '9': // value=c; // Super stupid error!!! If you enter 9, '9'=57!!! The value of character 9 is not the number 9 but the value of the character itself. value=Integer.parseInt(c+"") ; break; } return value; } } 9. Matrix Transpose (20 points)
Problem description:
Swap rows of an N*N matrix. ・
Required to implement the function:
public String matrixTranspose (String inArr, int n)
【Input】inArr: Input character matrix
n: Number of rows of N*N matrix [Return] Transposed character matrix Note:
The input and output matrix are two-dimensional arrays stored in one-dimensional form. For example, the input is "1,2,3,4,5,6,7,8,9", which actually represents the following 3*3 matrix:
1,2,3,4,5,6,7,8,9
Example input InArr ="1,2,3,4,5,6,7,8,9", n=3 Returns: "1,4,7,2,5,8,3,6,9"
Note: The author artificially added the title to a string rather than a character. Characters are simpler. Pay attention to intercepting strings. 13 is not 1,3
Method 1: Matrix was used by the question. Just think of it as familiar with the two-dimensional array
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String inarr="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16"; int n=4; System.out.println("inarr--->"+inarr); HWCompetition hw=new HWCompetition(); String result=hw.matrixTranspose(inarr,n); System.out.println("result---->"+result); } public String matrixTranspose(String inarr,int n){//Remove the comma in the string, otherwise the comma will be a subscript value of string. When output, add it String[] ss=inarr.split(","); String[][] css=new String[n][n]; int k=0; for(int i=0;i<n;i++){//Convert the string into a two-dimensional array for(int j=0;j<n;j++){ css[i][j]=ss[k]; k++; } } StringBuffer sb=new StringBuffer(); for(int i=0;i<n;i++){// 2D array inverse for(int j=0;j<n;j++){ sb.append(css[j][i]+",");//There is one more tail, } } return sb.substring(0, sb.length()-1); } } Method 2: Simpler, no matrix required at all.
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String inarr="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16"; int n=4; System.out.println("inarr--->"+inarr); HWCompetition hw=new HWCompetition(); String result=hw.matrixTranspose(inarr,n); System.out.println("result---->"+result); } public String matrixTranspose(String inarr,int n){//Remove the comma in the string, otherwise the comma will also be a subscript value of string. When outputting, add it String[] ss=inarr.split(","); StringBuffer sb=new StringBuffer(); for(int i=0;i<n;i++){ //For the question example: The required subscript order is 036147258 for(int j=i;j<ss.length;j+=n){ sb.append(ss[j]+",");//There is a comma on the tail} } return sb.substring(0, sb.length()-1);//Remove the tail comma} } 10. Street lights (20 points)
Streets in a provincial capital city are crisscrossed. In order to monitor the operation of street lights, each street uses a numeric string to identify the operation of all street lights on the street.
Assume that the street light has only the following three states (identified by the numbers 0, 1, 2, respectively, and a street light only corresponds to one of the states):
0 The sign street light is off;
1 The street light is turned on;
2 Identify street light failure;
Please find the maximum number of consecutive street lights in the same state on the street based on the entered string. If the number of street lights in both states is the same, the first street light state will be returned.
Enter a state string composed of continuous street lights on the street. The string contains only numbers, and the state of each street light is one of 0, 1, and 2. For example, "1101" represents 4 street lights, the third street light is in the state of out, and the other 3 are in the state of on.
The maximum number of street lights that are continuously in the same state is output;
The status of the above street lights;
Requirements: First output the quantity, then output the status, and use a space interval between the two integers. For example output:
53 2
Sample input
112200111
Sample output
3 1
package com.sheepmu.text; import java.util.Arrays; /* * @author sheepmu */ public class HWCompetition { public static void main(String[] args){ String s="112001110";//This type of question must pay attention to all the same situations in the end, that is, there is no different from it again, otherwise it is easy to have a dead cycle. System.out.println("inarr--->"+s); HWCompetition hw=new HWCompetition(); String result=hw.ludeng(s); System.out.println("Question result---->"+result); } public String ludeng(String s){//Remove the comma in the string, otherwise the comma is also a subscript value of string. When outputting, add it char[] as=s.toCharArray(); int len=as.length; int maxc=1; // int mubiaobindex=0;//Do not create extra variables// int mubiaovalue=as[0]; int bindex=0;//If you need to return the largest consecutive first subscript, then 6 at this time; if the question has this requirement: then set two variables: the number of times "per part" + the first subscript that appears. char value=as[0];//If the question needs to return the value of the maximum length part, that is, 1 of the state of this question for(int i=0;i<len-1; ){ int count=1;//Each outer loop is to set count to 1, and start a new count bindex=i; value=as[i]; for(int j=i+1;j<len;j++){ if(as[i]!=as[j]){ i=j; break; } else{ i++;//!!!!!!!!!!!! If no different appears afterwards; if this sentence is not added, the outer loop will be executed all the time. count++; } } if(count>maxc){ maxc=count; // mubiaobindex=bindex; // mubiaovalue=value; System.out.println("maxc--->"+maxc+" Start subscript---->"+bindex+" Status---->"+value); } } StringBuffer sb=new StringBuffer(); return sb.append(maxc+" ").append(value).toString(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.