网易2018笔试题三

xiaoxiao2021-02-28  123

如果一个01串任意两个相邻位置的字符都是不一样的,我们就叫这个01串为交错01串。例如: “1”,”10101”,”0101010”都是交错01串。 小易现在有一个01串s,小易想找出一个最长的连续子串,并且这个子串是一个交错01串。小易需要你帮帮忙求出最长的这样的子串的长度是多少。

运行时间: 45 ms 占用内存:9032K

import java.util.Scanner; public class Wangyic { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String str = in.nextLine(); Boolean tag = false; int count=0,s=0; char ss[]=str.toCharArray(); //System.out.println(ss.length); for(int i= 1;i<ss.length;i++ ){ //int temp = str.charAt(i); if(ss[i] != ss[i-1] && tag == false){ tag = true; count++; }else if(ss[i] != ss[i-1] && tag == true){ count++; }else if(ss[i] == ss[i-1] && tag == true){ tag=false; count=0; } if(count>s){ s=count; } } System.out.println(s+1); } }
转载请注明原文地址: https://www.6miu.com/read-33594.html

最新回复(0)