第三题,提交上去一直0%
笔试之后发现之前‘D’的情况有问题,改了代码,有没有大佬帮忙看看对不对呀?
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String s = br.readLine(); char[] cs = s.toCharArray(); StringBuilder sb = new StringBuilder(); int maxDeth = 0, currentDeth = 0; int leftNum = 0, rightNum = 0; int index = 0; for (char c : cs) { if (c == '(') { currentDeth++; leftNum++; sb.insert(index, '('); index++; } else if (c == ')') { currentDeth--; rightNum++; sb.insert(index, ')'); index++; } else if (c == 'L') { if (index > 0) { index--; if (sb.charAt(index) == '(') { currentDeth--; } else if (sb.charAt(index) == ')') { currentDeth++; } } } else if (c == 'R') { if (index < sb.length()) { if (sb.charAt(index) == '(') { currentDeth++; } else if (sb.charAt(index) == ')') { currentDeth--; } index++; } } else if (c == 'D') { if (index > 0) { index--; if (sb.charAt(index) == '(') { currentDeth--; leftNum--; //提交时maxDeth有问题 if (index == 0 || sb.charAt(index-1) == '(') { maxDeth--; } } else if (sb.charAt(index) == ')') { currentDeth++; rightNum--; } sb.deleteCharAt(index); } } maxDeth = Math.max(maxDeth, currentDeth); if (rightNum == leftNum) { System.out.print(maxDeth+" "); } else { System.out.print(Math.abs(rightNum-leftNum)*(-1) + " "); } } } }
全部评论
(1) 回帖