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