美团——交错序列
public class Main8 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); StringBuffer sb = new StringBuffer(); for (int i=0;i<n;i++) { sb.append(sc.nextInt()); } String s=sb.toString(); System.out.println(inverseSequence(s)); } } public static int inverseSequence(String s){ Stack<Character> stack=new Stack<Character>(); char[] ch=s.toCharArray(); stack.push(ch[0]); for(int i=1;i<ch.length;i++){ int popNum=stack.peek(); if(ch[i]!=popNum){ stack.push(ch[i]); } } int count=0; while(!stack.isEmpty()){ stack.pop(); count++; } return count; } }
其实真的很简单,只是心里素质不好,一到考试就完蛋