B站后端开发笔试题
B站的笔试题,题目我就不写了,直接贴代码
第一题:递归
boolean result = false; public boolean Game24Points (int[] arr) { if(arr == null || arr.length == 0) { return false; } f(arr, 1, arr[0]); return result; } public void f(int[] arr, int index,int sum) { if(index >= arr.length || result == true) { if(sum == 24) { result = true; } return; } f(arr, index+1, sum + arr[index]); f(arr, index+1, sum - arr[index]); f(arr, index+1, sum * arr[index]); if(arr[index]!= 0) { f(arr, index+1, sum / arr[index]); } }第二题:利用栈
public boolean IsValidExp (String s) { Stack<Character> stack = new Stack<>(); for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i); switch (ch) { case '{': stack.push('{'); break; case '[': stack.push('['); break; case '(': stack.push('('); break; case '}': { if(stack.isEmpty()) { return false; } if(stack.peek() == '{') { stack.pop(); } else { return false; } } break; case ']': { if(stack.isEmpty()) { return false; } if(stack.peek() == '[') { stack.pop(); } else { return false; } } break; case ')': { if(stack.isEmpty()) { return false; } if(stack.peek() == '(') { stack.pop(); } else { return false; } } break; default: break; } } if(stack.isEmpty()) { return true; } else { return false; } }第三题:贪心
public int GetCoinCount (int N) { int money = 1024 - N; int count = 0; count += money/64; money -= (64 * (money/64)); count += money / 16; money -= (16 * (money/16)); count += money / 4; money -= (4 * (money/4)); count += money / 1; return count; }