1、
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
String cur="";
String end="";
int i=0;
for( i=0;i
char ch=str.charAt(i);
if(ch-'0'>=0&&ch-'0'<=9){
cur=cur+ch+"";//字符串加字符要加上""
}
else{
if(cur.length()>end.length()){
end=cur;
}else{
cur="";
}
}
}
if(i==str.length()&&end.length()
}
System.out.println(end);
}
}
2、数组中出现次数超过一半的数字
import java.util.*;
public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
int half=array.length/2;
int count=0;
int sum=0;
if(array.length==1){
return array[0];
}
Arrays.sort(array);
for(int i=0;i
if(array[i]==array[i+1]){
count++;
if(count>=half){
sum=array[i];
}
}
else{
count=0;
}
}
return sum;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)