注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
楼主,最近面了下airbnb
第一题:
You need to implement a simplified `tree` command line tool.
1. You will get some paths like '/aaa/bcd/c.txt' which split by '/' and consisted with several characters, every path starts with '/'
2. All the characters are in `[a-zA-Z0-9./]`. Waral dи,
3. You need to output files/sub-folders in the same folder in dictionary order
4. You need to output the hierarchical path tree as following: ..
sample input:. .и
[
"/bin/usr/bash",
"/var/test/.ssh",
"/var/log/wifi.log",
"/opt",
"/xyz"
]
-baidu 1point3acres
sample output:
+-- bin
| +-- usr
| +-- bash
+-- opt-baidu 1point3acres
+-- var.1point3acres
| +-- log
| | +-- wifi.log
| +-- test
| +-- .ssh
+-- xyz
the indent should be 4 blanks
* new node starts with `+-- `. 1point 3acres
* the siblings are linked by '|'
Note: How to handle the input/output is not important. You can even hard code the input as a string array in the code.
我的代码:
- import java.util.*;. Χ
- .
- public class Main {
- public static class Tree {
- String val;
- Tree() {.google и
- };
- Tree(String val) {
- this.val = val;
- }
- ArrayList<Tree> children = new ArrayList<>();
- }. From 1point 3acres bbs
- -baidu 1point3acres
- public static void main(String[] args) {
- String[] arr = {"/bin/usr/bash",
- "/var/test/.ssh",
- "/var/log/wifi.log",
- "/opt",
- "/xyz"
- };
- Arrays.sort(arr);
- Tree head = new Tree("/");
- Tree t = head;
- for (String s : arr) {
- String[] tmp = s.split("\\/");
- t = head;
- for (String a : tmp) {-baidu 1point3acres
- if (a.equals("")) {
- continue;
- }
- boolean flag = false;
- for (Tree tree : t.children
- ) {
- if (a.equals(tree.val)) {
- t = tree;. ----
- flag = true;
- break;
- }
- }. 1point 3acres
- if (!flag) {. ----
- Tree tree = new Tree(a);. 1point 3acres
- t.children.add(tree);
- t = tree;
- }
- }
- }
. 1point 3acres - //
- dfs(head, 0, "",false);
- // System.out.println("1234");
- return;.
- }
- public static void dfs(Tree t, int i, String s,Boolean hasFloor) {.
- System.out.println(s + t.val);
- if (t.children.isEmpty()) {
- return;
- } ..
- int num = t.children.size();
- for (int j = 0; j < num; ++j) {
- Tree tree = t.children.get(j);. 1point3acres
- StringBuffer sb = new StringBuffer(s);
- if ( i == 0){
- sb.append("+-- ");
- }
- if (i > 0 ) {
- if (hasFloor) {. Waral dи,
- sb.insert(sb.length() - 4, "| ");. From 1point 3acres bbs
- } else {
- sb.insert(sb.length() - 4, " ");
- }
- }
- if(j == num-1) {
. Waral dи, - hasFloor = false;. From 1point 3acres bbs
- }else {
- hasFloor = true;
- }
- dfs(tree, i + 1, sb.toString(),hasFloor);
- }
- }. From 1point 3acres bbs
- . 1point 3acres
- }
复制代码 第二题:
Problem statement
Given a nonnegative number set (can have duplicates), divide it into two sets so that the sum of the variances of these two sets is minimized. You can shuffle set order. The values of two sets cannot overlap. All number in the original set must appear in one of the two sets.
Finally return the value of the sum. The result include one decimal place.
The definition of variance: S^2= ∑ (X-a) ^2 / (n-1), where X is the average, a is every number in the set, and n is the size of the set.
For example: There is a set [1,3,1] . We can divide it into [1,1] and [3], Then return the result:0.0
Please simply proof the correctness of your solution.
Follow up: What if we want to divide the set into three subsets?- //CASE 1:
.. - // Input
- // [4,3,2,1]
- // Output
- // 1.0
- import java.util.*; ..
- public class Main {
- public static void main(String[] args) {
- //Scanner in = new Scanner(System.in);. Χ
- //int a = in.nextInt();. Waral dи,
- //System.out.println(a);
- System.out.println("Hello World!");
-
- // n 知道 sum 知道 min ((sumA/a -ai)/a-1 + ((sum-aumA)/n-a - bi)/n-a-1)
- // sum 可以分成 a+b个数 (sumA/a-ai)^2/ a-1 + sumB/b
- int []arr = {4,3,2,1};. 1point 3 acres
- //int[]arr = {1,3,1};.--
- Arrays.sort(arr);
- int len = arr.length; // 数组长度
- // 如果数组小于 2 则直接返回 0
- if( len < 2 ) {. 1point 3acres
- System.out.println(0);
- return ;
- }
- int sum = 0; // 前缀求和
- double ans = 10000; // 存储最小的结果
- int sum2 = 0;
- for( int i = 0; i < len; ++i ) {
- sum += arr[i];.--
- sum2 += arr[i]*arr[i];
- }
- // 选择分割点
- int pre = 0; //前缀和
- int pre2 = 0; // 前缀平方和
- for ( int i = 0; i < len-1; ++i ) {. .и
- pre += arr[i];
- pre2 += arr[i]*arr[i];
- int ave = pre/(i+1);
- int x =(i+1)*ave*ave;
- int mid = 2*ave*pre;
- int y = pre2;
- // 长度为0 则直接为0
- double a = 0;
- double b = 0;
- if(i > 0) {
- a = (double)(x-mid+y)/i;
- }else{
- a = 0;
- }. Χ
- //System.out.println("a:"+a);. Χ
- int ave2 = (sum-pre)/(len-i-1);
- // System.out.println("ave2:"+ave2);
- int x2 = (len-i-1)*ave2*ave2;
- //System.out.println("x2:"+x2);
- int mid2 = 2*ave2*(sum-pre);
- //System.out.println("mid2:"+mid2);. check 1point3acres for more.
- int y2 = sum2-pre2;
- //System.out.println("y2:"+y2);
- if(len-i-1>1) {
- b = (double)(x2-mid2+y2)/(len-i-1-1);
- }else{. 1point 3acres
- b = 0;
- }
- //System.out.println("b:"+b); ..
- ans= Math.min(a+b,ans);
-
- }
- System.out.println(ans);
- . .и
- }
-
-
- }
复制代码 第二题面试的时候有个变量ave2没有写2 导致答案一直不对,时间来不及 面试官就说到这吧。。自己太菜了 虽然挂了 求点大米安慰下~~
|