翻转一枚硬币,最大的相同硬币数
package com.code; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Solution03_2 { int solution(int[] A) { int n = A.length; List<Integer> result = new ArrayList<Integer>(1); result.add(1); for (int i = 0; i < n - 1; i++) { if (A[i] == A[i + 1] ){ result.set(result.size()-1, result.get(result.size()-1)+1); }else{ result.add(1); } } int max = 0; int size = result.size(); if(size == 1){ return result.get(0); } for(int i=0;i<size;i++){ Integer ri = result.get(i); max = Math.max(max, ri+1); //if size>1, add 1 at least if(ri -1 ==0){ if(i>0 && i<size-1){ max = Math.max(max, result.get(i-1)+result.get(i)+result.get(i+1)); } } } System.out.println("arrays: "+Arrays.toString(A)); System.out.println("result: "+result); return max ; } public static void main(String[] args) { int [] a = {1,1,0,1,0,0}; Solution03_2 s03 = new Solution03_2(); int i = s03.solution(a); System.out.println(i); int [] b = {1,1,0,1,1,0}; System.out.println(s03.solution(b)); int [] c = {1,0,1}; System.out.println(s03.solution(c)); int [] d = {1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0}; System.out.println(s03.solution(d)); } }