3月5号奇安信笔试编程题
第一题,最长上升子序列
public static int LSubSequence (int N, ArrayList<Integer> sequence) { // write code here int[] dp = new int[N]; Arrays.fill(dp,1); int max=1; for (int i = 0; i < sequence.size(); i++) { for (int j = 0; j < i; j++) { if (sequence.get(j)<sequence.get(i)){ dp[i]=Math.max(dp[j]+1,dp[i]); max=Math.max(dp[i],max); } } } return max; }第二题,满足正确括号闭合的字符串个数
public static int count (ArrayList<String> arr) { // write code here int count=0; for (int i = 0; i < arr.size(); i++) { if(test(arr.get(i))){ count++; } } return count; } public static boolean test(String s){ if (!s.contains("(")&&!s.contains("(")){ return false; } Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i)=='('){ stack.push(')'); }else if (stack.isEmpty()||stack.pop()!=s.charAt(i)){ return false; } } return stack.isEmpty(); }