AcWing 602. 01串-java
原题链接
简单
作者:
单箭头
,
2019-05-16 21:01:33
,
所有人可见
,
阅读 1125
java 代码
public class 串01 {
public static void main1(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
char[] chars=sc.nextLine().trim().toCharArray();
Stack<Character> stack=new Stack<>();
stack.push(chars[0]);
for(int i=1;i<n;i++){
while (!stack.isEmpty()&& i<n && chars[i]!=stack.peek()){
stack.pop();
i++;
}
if (i<n) stack.push(chars[i]);
}
System.out.println(stack.isEmpty()?0:stack.size());
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
char[] chars=sc.nextLine().trim().toCharArray();
int zero=0,one=0;
for (int i=0;i<n;i++){
if (chars[i]=='0') zero++;
else one++;
}
System.out.println(Math.abs(zero-one));
}
}