注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
楼主,最近面了下airbnb
第一题:
You need to implement a simplified `tree` command line tool.-baidu 1point3acres
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./]`
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:
.google и
[
"/bin/usr/bash",
"/var/test/.ssh",
"/var/log/wifi.log",
"/opt",
"/xyz"
]
sample output:
+-- bin
| +-- usr-baidu 1point3acres
| +-- bash
+-- opt
+-- var. ----
| +-- log
| | +-- wifi.log
| +-- test
| +-- .ssh
+-- xyz
the indent should be 4 blanks
* new node starts with `+-- `
* 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() {
- };
- Tree(String val) {
- this.val = val;
- }
- ArrayList<Tree> children = new ArrayList<>();
- }
- .--
- public static void main(String[] args) {
- . ----
- String[] arr = {"/bin/usr/bash",. 1point3acres.com
- "/var/test/.ssh",. 1point 3 acres
- "/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;. From 1point 3acres bbs
- for (String a : tmp) {.google и
- if (a.equals("")) {
- continue;
- }
- boolean flag = false;
- for (Tree tree : t.children
- ) {
- if (a.equals(tree.val)) {
- t = tree;. .и
- flag = true;. Χ
- break;
- }
- }
- if (!flag) {
- Tree tree = new Tree(a);
- t.children.add(tree);
- t = tree;
- }
- }
- }
- //. From 1point 3acres bbs
- -baidu 1point3acres
- dfs(head, 0, "",false);
- // System.out.println("1234");
- return;
. Waral dи,
- }
- 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();.1point3acres
- for (int j = 0; j < num; ++j) {
- Tree tree = t.children.get(j);
- StringBuffer sb = new StringBuffer(s);
- if ( i == 0){
- sb.append("+-- ");
. From 1point 3acres bbs - }
- if (i > 0 ) {
- if (hasFloor) {
- sb.insert(sb.length() - 4, "| ");
- } else {
- sb.insert(sb.length() - 4, " ");
- }
- }
- if(j == num-1) {. 1point3acres
- hasFloor = false;
- }else {
- hasFloor = true;
- }
- dfs(tree, i + 1, sb.toString(),hasFloor);
- }
- }. 1point 3acres
- }
复制代码 第二题:. From 1point 3acres bbs
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.. From 1point 3acres bbs
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();.
- //System.out.println(a);
- System.out.println("Hello World!");. 1point 3acres
-
- // 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};
- //int[]arr = {1,3,1};. 1point 3acres
- Arrays.sort(arr);
- int len = arr.length; // 数组长度
- // 如果数组小于 2 则直接返回 0
- if( len < 2 ) { ..
- 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];
- }. Waral dи,
- // 选择分割点
- int pre = 0; //前缀和. check 1point3acres for more.
- int pre2 = 0; // 前缀平方和
- for ( int i = 0; i < len-1; ++i ) {
. From 1point 3acres bbs - 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);. Waral dи,
- int x2 = (len-i-1)*ave2*ave2;
- //System.out.println("x2:"+x2);
- int mid2 = 2*ave2*(sum-pre);
- //System.out.println("mid2:"+mid2);
- int y2 = sum2-pre2;
- //System.out.println("y2:"+y2);. 1point 3acres
- if(len-i-1>1) {
- b = (double)(x2-mid2+y2)/(len-i-1-1);
- }else{
- b = 0;
- }
- //System.out.println("b:"+b);
- ans= Math.min(a+b,ans);
-
- }
- System.out.println(ans);
-
- }
-
- . check 1point3acres for more.
- }
复制代码 第二题面试的时候有个变量ave2没有写2 导致答案一直不对,时间来不及 面试官就说到这吧。。自己太菜了 虽然挂了 求点大米安慰下~~
|