由买买提看人间百态

topics

全部话题 - 话题: integal
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
b*****n
发帖数: 760
1
来自主题: JobHunting版 - G面经
1. You have a class that supports to input sample records and to compute the
average of the samples. The class has two members: total and count. How
would you make the class thread-safe? If 99% of the time average() is called
, how to optimize for that?
2. Talk about your recent interesting project/bug.
3. You have 100 files, each containing 10G sorted integers. How to merge all
integers into one sorted file?
4. Write a function to reverse digits of an integer. E.g. 123 --> 321, -890
--> -98.
5.... 阅读全帖
y***m
发帖数: 7027
2
来自主题: JobHunting版 - 有好的merge有序数组算法么
简单java的,有更优化的么,thx!
private static void merge() {
Integer A[] = new Integer[] { 1, 5, 8, 14, 25 };
Integer B[] = new Integer[] { 3, 4, 7, 17, 27 };
int k = A.length + B.length;
Integer C[] = new Integer[k];
int i = 0, j = 0, m = 0;
while (m < k-1) {
if (A[i] > B[j]) {
C[m] = B[j];
j++;
... 阅读全帖
y***m
发帖数: 7027
3
来自主题: JobHunting版 - 有好的merge有序数组算法么
哦... 这样可以?
thx!
private static void merge2() {
Integer B[] = new Integer[] { 1, 3, 8, 14, 25 };
Integer A[] = new Integer[] { 3, 4, 7, 17, 27, 30 };
int k = A.length + B.length;
Integer C[] = new Integer[k];
int i = 0, j = 0, m = 0;
while (m < k - 1) {
if (j < B.length - 1 && (A[i] > B[j] || i > A.length - 2)) {
C[m] = B[j];
j++;
} else {
C[m] = A[i];
i++;
}
log.info(" " + C[m]);
m++;
}
}
D********g
发帖数: 650
4
来自主题: JobHunting版 - 问个amazon面试题
A solution with bfs, node on a complete b-tree should have continuous index
if appropriately indexed:
Boolean isCompleteBinaryTree(Node root) {
If (root == NULL) return true;
Queue> bfs = new Queue>();
Bfs.put(new Entry(root, 0));
Int currentNodeIdx = -1;
While (!bfs.empty()) {
Entry e = bfs.poll();
If (e.getValue() != ++ currentNodeIdx) {
Return false; // Node index is not continuous.
}
Node n = e.get... 阅读全帖
s******n
发帖数: 226
5
来自主题: JobHunting版 - 再问个Amazon面试题
假设x = 1, Y=1
输出 202.
import java.util.*;
class Short{
public static int[][] path;
public static boolean [][]visit;
public static int ii;
public static int jj;
public static int[][]w = { {100, 1, 1, 1, 1, 100},
{100, 1, 100, 100, 100, 1},
{1, 100, 100, 1, 100, 100},
{1, 100, 100, 100, 1, 100},
{1, 1, 1, 1, 100, 100}
};
static int m =5;
static int n =6;


static int ... 阅读全帖
s******n
发帖数: 226
6
来自主题: JobHunting版 - 再问个Amazon面试题
假设x = 1, Y=1
输出 202.
import java.util.*;
class Short{
public static int[][] path;
public static boolean [][]visit;
public static int ii;
public static int jj;
public static int[][]w = { {100, 1, 1, 1, 1, 100},
{100, 1, 100, 100, 100, 1},
{1, 100, 100, 1, 100, 100},
{1, 100, 100, 100, 1, 100},
{1, 1, 1, 1, 100, 100}
};
static int m =5;
static int n =6;


static int ... 阅读全帖
w**z
发帖数: 8232
7
来自主题: JobHunting版 - atoi很不好写,头都大了...
我再贴一下Java的sourcecode。不懂为什么要把正数转成负数再查溢出呢?有什么好处?
code里有Author的comments
// Accumulating negatively avoids surprises near MAX_VALUE
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sig... 阅读全帖
b*****k
发帖数: 26
8
来自主题: JobHunting版 - M 家电话难题
The IntegerSet Class
the IntegerSet class represents the set of integers ranging from 1 to 10,000
,000. This class has a default ctor and a dtor. It also contains two
additional public methods: Acquire and Relinquish.
The Acquire method picks an available value from the set, removes it fro mm
the set, and returns this value to the callers. If the set of available
integers is empty, the Acquire method returns 0.
The Relinquish method adds an available integer value back to the set. If
the value i... 阅读全帖
w**z
发帖数: 8232
9
Source Code from Java
/*
* @author Lee Boynton
* @author Arthur van Hoff
* @author Josh Bloch
* @version 1.92, 04/07/06
* @since JDK1.0
*/
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* A... 阅读全帖
p*****2
发帖数: 21240
10
来自主题: JobHunting版 - 报个offer@FG,回报版面

Some engineers got tired of dealing with all the different ways of encoding
status messages, so they decided to invent their own. In their new scheme,
an encoded status message consists of a sequence of integers representing
the characters in the message, separated by spaces. Each integer is between
1 and M, inclusive. The integers do not have leading zeroes. Unfortunately
they decided to compress the encoded status messages by removing all the
spaces!
Your task is to figure out how many differ... 阅读全帖
p*****2
发帖数: 21240
11
来自主题: JobHunting版 - 报个offer@FG,回报版面

Some engineers got tired of dealing with all the different ways of encoding
status messages, so they decided to invent their own. In their new scheme,
an encoded status message consists of a sequence of integers representing
the characters in the message, separated by spaces. Each integer is between
1 and M, inclusive. The integers do not have leading zeroes. Unfortunately
they decided to compress the encoded status messages by removing all the
spaces!
Your task is to figure out how many differ... 阅读全帖
D********g
发帖数: 650
12
来自主题: JobHunting版 - [google面试]iterator访问
我的java 版本,欢迎指正
public static class Flattener {
final List> _vv;
int _curList = 0;
int _curOffset = 0;
public Flattener(final List> vv) {
if (vv == null) {
throw new IllegalArgumentException();
}
_vv = new ArrayList>();
for (int i = 0; i < vv.size(); ++i) {
if (vv.get(i) == null || vv.get(i).size() == 0) {
continue;
... 阅读全帖
p*****2
发帖数: 21240
13
来自主题: JobHunting版 - [google面试]iterator访问
大概这个样子吧。
class flat implements Iterator
{
Iterator> it1=null;
Iterator it2=null;

void Getit2()
{
it2=null;
if(it1!=null)
{
while(it1.hasNext())
{
Vector v=it1.next();
if(v!=null && v.size()!=0)
{
it2=v.iterator();
return;
};
}
}
}
public flat(Vector> a)
{
if(a!=null)
{
it1=a.iterator();
Getit2();
}
... 阅读全帖
p*****2
发帖数: 21240
14
来自主题: JobHunting版 - G家一面。
我写了一个练练手。
import java.util.*;
public class test3
{
public static void main(String[] args)
{
Interval in = new Interval();
int[][] sam = new int[][]
{
{ 5, 10 },
{ 15, 17 },
{ 18, 25 },
{ 16, 35 } };
for (int i = 0; i < sam.length; i++)
{
in.Merge(sam[i][0], sam[i][1]);
in.Print();
}
}
}
class Interval
{
TreeMap tm = new TreeMap();
void Me... 阅读全帖
w*********0
发帖数: 48
15
来自主题: JobHunting版 - longest valid Parentheses有O(n)算法么
O(n)
import java.util.*;
public class Solution {
public int longestValidParentheses(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s == null || s.length() == 0) return 0;
ArrayList pos = new ArrayList();
HashMap pair = new HashMap();
int len = s.length();
int max = 0;
for (int i = 0; i < len; i++) {
if (s.charAt(i) == '(')... 阅读全帖
w*********0
发帖数: 48
16
来自主题: JobHunting版 - longest valid Parentheses有O(n)算法么
O(n)
import java.util.*;
public class Solution {
public int longestValidParentheses(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s == null || s.length() == 0) return 0;
ArrayList pos = new ArrayList();
HashMap pair = new HashMap();
int len = s.length();
int max = 0;
for (int i = 0; i < len; i++) {
if (s.charAt(i) == '(')... 阅读全帖
p*****2
发帖数: 21240
17
来自主题: JobHunting版 - leetcode 这题insert interval怎么做?
刚才又重写了一遍
class Solution1
{
TreeMap tm = new TreeMap();
void insert(Interval interval)
{
Integer prev = tm.floorKey(interval.start);
Integer next = tm.ceilingKey(interval.start);
if (prev != null && tm.get(prev) >= interval.start || next != null
&& interval.end >= next)
{
int start = -1;
if (prev != null && tm.get(prev) >= interval.start)
{
tm.put(pre... 阅读全帖
p*****2
发帖数: 21240
18
来自主题: JobHunting版 - leetcode 这题insert interval怎么做?
刚才又重写了一遍
class Solution1
{
TreeMap tm = new TreeMap();
void insert(Interval interval)
{
Integer prev = tm.floorKey(interval.start);
Integer next = tm.ceilingKey(interval.start);
if (prev != null && tm.get(prev) >= interval.start || next != null
&& interval.end >= next)
{
int start = -1;
if (prev != null && tm.get(prev) >= interval.start)
{
tm.put(pre... 阅读全帖
p*****2
发帖数: 21240
19

随便写了一个,不过没 怎么调呀。大概这个意思。
HashMap hm = new HashMap();
int isValid(char[] arr)
{
Stack stack = new Stack();
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == '(')
stack.push(i);
else if (arr[i] == ')')
{
if (stack.isEmpty())
return -1;
hm.put(stack.pop(), i);
}
}
if (!stack.is... 阅读全帖
G******i
发帖数: 5226
20
来自主题: JobHunting版 - [合集] guangyi的面经和总结
☆─────────────────────────────────────☆
guangyi ( 光一) 于 (Sat Oct 29 00:10:37 2011, 美东) 提到:
**********************************
M:
phone interview (1 round):
why MS?
biggest challenge
why like coding and algorithm?
what is good code?
your longest code
biggest accomplishment
if you don't want some functions to be modified in java, what to do?
does java allow multiple inheritance?
what does synchronized keyword mean in java?
CEO wants a book, you find it in the system of a nearby bookshop. You ... 阅读全帖
P*********c
发帖数: 35
21
来自主题: JobHunting版 - Two Sigma一道题
Glassdoor上看来的,不知题目到底啥意思,是说先把list分成两块,第一块全是word
,第二块全是integer,两块的element其实都是string,只是第一块要按照string的字
母排序,第二块要按照string所表示的integer大小排序吗?
Write a program that takes a list of strings containing integers and words
and returns a sorted version of the list. The goal is to sort this list in
such a way that all words are in alphabetical order and all integers are in
numerical order. Furthermore, if the nth element in the list is an integer
it must remain an integer, and if it is a word it must remain a wo... 阅读全帖
x*****o
发帖数: 33
22
来自主题: JobHunting版 - G家面试题
求大牛鉴定,这个也N!吗?
public int findMin(int[][] a)
{
if (a == null || a.length == 0)
{
return 0;
}
if (a.length == 1)
{
return a[0][0];
}
int n = a[0].length;
ArrayList avail = new ArrayList();
for (int i = 0; i < n; i++)
{
avail.add(i);
}
return find(a, 0, avail);
}
private int find(int[][] a, int curRow, ArrayList availCols)
{
... 阅读全帖
x*****o
发帖数: 33
23
来自主题: JobHunting版 - G家面试题
求大牛鉴定,这个也N!吗?
public int findMin(int[][] a)
{
if (a == null || a.length == 0)
{
return 0;
}
if (a.length == 1)
{
return a[0][0];
}
int n = a[0].length;
ArrayList avail = new ArrayList();
for (int i = 0; i < n; i++)
{
avail.add(i);
}
return find(a, 0, avail);
}
private int find(int[][] a, int curRow, ArrayList availCols)
{
... 阅读全帖
l**********1
发帖数: 415
24
Given two integers n and k, return all possible combinations of k numbers
out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
the given api is
public ArrayList> combine(int n, int k)
the solution I got is as follows which is really slow O(n!) and time out at
10,7. So please provide any idea of higher efficiency algorithm for this
one. Thanks in advance
public void combination(ArrayList> r... 阅读全帖
Z***A
发帖数: 33
25
来自主题: JobHunting版 - 3sum on LeetCode OJ
写了3sum的solution,但是没通过OJ large judge,Run Status: Time Limit
Exceeded,请分析一下原因。
下面是code:
public ArrayList> threeSum(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
if (num.length < 3)
return new ArrayList>();

Arrays.sort(num);
ArrayList> res = new ArrayList
>();
for (int i=0; i < num.length; i++)
{
... 阅读全帖
s********x
发帖数: 914
26
来自主题: JobHunting版 - 新鲜G面筋(2)
切磋一下. running time 应该是 less than log(K)
public class RandomNextIntExceptK {
private int[] kk;
private int[] kAdd;
private Random rand = new Random();
public RandomNextIntExceptK(int[] k) {
Map kCount = new TreeMap();
for (int i = 0; i < k.length; i++)
{
int key = k[i] - i;
if (kCount.containsKey(key))
{
int count = kCount.get(key);
kCount.put(key, cou... 阅读全帖
c******t
发帖数: 391
27
在多次面试的online coding里都遇到这种情况,当实现一个返回值为int的搜索函数时
,有时会在if语句里判断是否符合搜索条件,如果符合即返回。但问题是,函数的
default返回值该怎样设,如果是搜索对象是数组下标,当然可以返回-1。但如果让返
回搜索的元素,该怎样解决呢?
举个例子,下面是一个用hashMap找数组里出现奇数次元素的代码(没测试):
public int findOdd(int[] arr){
HashMap map = new HashMap();
for(int i = 0; i if(map.get(arr[i])==null)
map.put(arr[i],1);
else{
map.put(arr[i],map.get(arr[i])+1);
}
}
for(Map.Entry item... 阅读全帖
c*****a
发帖数: 808
28
怎么优化space,有报错 Run Status: Memory Limit Exceeded
本地IDE倒能跑
public class Solution {
public ArrayList> zigzagLevelOrder(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> res = new ArrayList
>();
Queue queue = new LinkedList();
queue.offer(root);
queue.offer(null);
ArrayList row = new ArrayList();... 阅读全帖
e****e
发帖数: 418
29
来自主题: JobHunting版 - 狗店面,求BLESS
Bless Louzhu.
Instead of using parentCount, use a map to record its parents. The code
might not be optimal, but it should work.
class TreeNode {
TreeNode left;
TreeNode right;
Map parentCount = new HashMap();
}
private boolean isSame(TreeNode n1, TreeNode n2, TreeNode p1, TreeNode p2) {
if ( n1 == null && n2 == null )
return true;
if ( n1 == null || n2 == null )
return false;

addParent( n1, p1 );
addParent( n2,... 阅读全帖
m*****1
发帖数: 147
30
public ArrayList> levelOrder(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> array=new ArrayList>();
level(0,array,root);
return array;
}
public void level(int level,ArrayList> array,TreeNode
node){
if(node==null) return;
ArrayList list=null;
if(array.size()==level){
list=new ArrayList();
array.add(list)... 阅读全帖
j**7
发帖数: 143
31
来自主题: JobHunting版 - 问一个3 sum的问题
public ArrayList> threeSum(int[] numbers) {
Arrays.sort(numbers);
ArrayList> list = new ArrayList >>();
for (int i = 0; i < numbers.length; i++) {
if (i > 0 && numbers[i] == numbers[i - 1]) {
continue;
}
twoSumWhile(numbers, 0 - numbers[i], i + 1, list);
}
return list;
}
public void twoSumWhile(int[] numbers, int target, int start, ArrayList<... 阅读全帖
j**h
发帖数: 1230
32
来自主题: JobHunting版 - Facebook Phone Interview
void printN(int n) {
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
Set pairSet = getPairSet(i * j);
for (Pair pair : pairSet) {
System.out.println(i + "*" + j + "=" + pair.i + "*" +
pair.j);
}
}
}
}
Map> map = new HashMap>();
Set getPairSet(int product) {
if (map.containsKey(new Integer(product)))
... 阅读全帖
j**h
发帖数: 1230
33
来自主题: JobHunting版 - Facebook Phone Interview
void printN(int n) {
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
Set pairSet = getPairSet(i * j);
for (Pair pair : pairSet) {
System.out.println(i + "*" + j + "=" + pair.i + "*" +
pair.j);
}
}
}
}
Map> map = new HashMap>();
Set getPairSet(int product) {
if (map.containsKey(new Integer(product)))
... 阅读全帖
c********p
发帖数: 1969
34
没人理了,只好又发一次。。。
http://www.mitbbs.com/article_t0/Java/31140155.html
这回我按网友建议把求hashtable size改成直接count删去的个数了,还是不能通过大
oj。所以又跑来问问我到底哪里出了问题?!
http://www.mitbbs.com/article_t0/JobHunting/32465791.html
原帖在这里。
我新的代码:
import java.util.*;
public class Solution {
public int longestConsecutive(int[] num) {

if(num == null || num.length == 0){
return 0;
}
int max = 1;
int count = 1;
Hashtable hash = new Hashtable阅读全帖
z****e
发帖数: 54598
35
来自主题: JobHunting版 - 求个4sum的算法
leetcode 不超过两秒就可以过大oj
我用了800毫秒,还有1200毫秒富余
把你的code贴上来看看
Run Status: Accepted!
Program Runtime: 864 milli secs
public class Solution {
public ArrayList> fourSum(int[] num, int target) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> list = new ArrayList >>();
Set set = new HashSet>();
Arrays.sort(num);
for(int i=0;i ... 阅读全帖
s**********e
发帖数: 326
36
昨天面的,面试官首先迟到了将近五分钟,上来面试官简单介绍了他自己,然后就直接
进入主题,也没有让我做自我介绍啥的,上来问我有没有用过java iterator pattern,
我给听成intepreter, 回答没用过,他不相信,又问了一遍,恍然大悟,赶紧说用过
用过,用过很多,然后他还说用java的人不可能没用过
然后问为什么用iterator, 有啥好处
答了之后接着问java里面有几种list, 答arraylist和linkedlist
又问实现这两种不同list的iterator有什么不同,到此为止都是对答如流,问他我答的
是不是他想要的答案,他说exactly
答完以后开始出题,先是写个data structure, 让我guess这是什么data structure,
class N {
private N l; // can be null
private N r; // can be null
private String data;
}
一紧张说成linkedlist, 赶紧改口说是tree,
然后就是描述问题,要求写一个Iterator, 每... 阅读全帖
D*T
发帖数: 75
37
来自主题: JobHunting版 - 小弟求问LinkedIn那道Deep Iterator的题
想了一下,这个问题好像不是那么容易。楼主的code如果末尾有个空的List会出错。我
不太懂C++所以pdu的只是半懂,不过感觉我的想法和ta估计差不多。moridin的code很
神奇,我实在看不太懂。
下面是我的code,搞ArrayList还成,搞LinkedList可能就惨了。另外remove好像是ok
的。
import java.util.*;
class ArrayPosition {
ArrayList array;
int index;
ArrayPosition(ArrayList array) {
this.array = array;
index = 0;
}
Object peekItem() {
return array.get(index);
}
Object takeItem() {
return array.get(index++);
}
}
public class DeepIteratorII implements ... 阅读全帖
D*T
发帖数: 75
38
来自主题: JobHunting版 - 小弟求问LinkedIn那道Deep Iterator的题
想了一下,这个问题好像不是那么容易。楼主的code如果末尾有个空的List会出错。我
不太懂C++所以pdu的只是半懂,不过感觉我的想法和ta估计差不多。moridin的code很
神奇,我实在看不太懂。
下面是我的code,搞ArrayList还成,搞LinkedList可能就惨了。另外remove好像是ok
的。
import java.util.*;
class ArrayPosition {
ArrayList array;
int index;
ArrayPosition(ArrayList array) {
this.array = array;
index = 0;
}
Object peekItem() {
return array.get(index);
}
Object takeItem() {
return array.get(index++);
}
}
public class DeepIteratorII implements ... 阅读全帖
y***d
发帖数: 1
39
来自主题: JobHunting版 - 发个g的电面
lz第一题是这个意思把?
输出结果是:1223334444
public class IteratorTest {
public static void main(String args[]) {
// ArrayList input = new ArrayList();
MyArrayList input = new MyArrayList();
input.add(1);
input.add(1);
input.add(2);
input.add(2);
input.add(3);
input.add(3);
input.add(4);
input.add(4);
for(Integer item:input){
System.out.println(item);
}
}
}
cla... 阅读全帖
o***g
发帖数: 2784
40
讨论一道题
原帖如下:
6. http://www.mitbbs.com/article_t/JobHunting/32631467.html
发信人: goodbai (八段锦), 信区: JobHunting
标 题: 热腾腾g电面 已挂
发信站: BBS 未名空间站 (Fri Feb 21 00:20:19 2014, 美东)
同胞面试官,上来就gdoc做题。
2d array *代表障碍物 #代表货物 空白就是正常的路

如何找到一个点为出发点 能实现总共取货路径最短? 每次只能拿一个货物,遇到障碍
需要绕开,拿到以后要放回出发点,然后再取另一个
******
* # *
* *** *
* *
* ** *
* # #*
** ****
大牛们有什么好思路?我用的bfs,但因为之前讨论题目要求花了很久,没有写完。。
我还是太弱了,move on
==================================================================
我看了原帖主题里,都在讨论... 阅读全帖
r******g
发帖数: 138
41
来自主题: JobHunting版 - G家面经(已被HC挂,求分析)
对第四轮

public static String getDecimal(int a, int b){
if(a == 0)
return "0.0";
if(b == 0)
return "";

StringBuilder res = new StringBuilder();
res.append(a/b);
res.append(".");
int c = a;
HashMap mod = new HashMap();
ArrayList decimals = new ArrayList();

int index=0;
while(c%b !=0 && !mod.containsKey(c%b)){
... 阅读全帖
p****6
发帖数: 724
42
来自主题: JobHunting版 - 非死不可电面出了新花样
先遍历, 边遍历边给每个节点一个index, 比如root为0, 做left减1, right加1. 然后
建立一个HashMap, key是index, value是list
贴个java版本的
public List> printVertical1(TreeNode root){

List> res = new ArrayList>();
if(root == null)
return res;

Map> map = new HashMap TreeNode>>();
getVertical(root, map, 0);

Set keys = new TreeSet(map.keySet());
... 阅读全帖
g**********y
发帖数: 14569
43
来自主题: JobHunting版 - 讨论一个g题
1. 拉格朗日定理:任何一个整数都可以分解成4个整数的平方和。
2. a_i <= sqrt(T)
3. BFS, search one square sum (0 ~ sqrt(T)), then two square sum, ... it
will end at most level 4.
程序可能可以更高效 --
public List split(int N) {
int n = (int) Math.sqrt(N);

int[] square = new int[n];
for (int i=0; i
List[] res = new List[N];
res[0] = new ArrayList();

Queue queue = new ArrayDeque();
q... 阅读全帖
r*******e
发帖数: 971
44
来自主题: JobHunting版 - 发个L家面经,攒rp
我改了一下,这要不过不了也没办法了,不用recursion实在麻烦。
public List> getFactorCombination(int n) {
List> result = new ArrayList<>();
getFactorCombinationHelper(n, n/2, result, new ArrayList());
return result;
}
private void getFactorCombinationHelper(int n, int start, List
> result, List sub) {
if(n==1) {
result.add(new ArrayList(sub));
return;
}

for(int i = start; i... 阅读全帖
r*******e
发帖数: 971
45
来自主题: JobHunting版 - 发个L家面经,攒rp
我改了一下,这要不过不了也没办法了,不用recursion实在麻烦。
public List> getFactorCombination(int n) {
List> result = new ArrayList<>();
getFactorCombinationHelper(n, n/2, result, new ArrayList());
return result;
}
private void getFactorCombinationHelper(int n, int start, List
> result, List sub) {
if(n==1) {
result.add(new ArrayList(sub));
return;
}

for(int i = start; i... 阅读全帖
w*******s
发帖数: 96
46
来自主题: JobHunting版 - 发个L家面经,攒rp
import java.util.ArrayList;
import java.util.List;
public class FactorTest {
List> getFactor(int n) {
List> result = new ArrayList<>();
List path = new ArrayList<>();
dfs(n, n / 2, path, result);
return result;
}

private void dfs(int n, int start, List path, List >> result) {
if (n == 1) {
result.add(new ArrayList(path));
return;
}

... 阅读全帖
s*********p
发帖数: 130
47
来自主题: JobHunting版 - 问一道FB 的电面题

这题这么写可以吗?
分别把start array and end array排序,然后线性向下扫。
但是加入输入不把start 和 end 分开,而是向leetcode 那样建一个Interval class,
怎么保证start 和 end都有序呢?
public class Solution {
public int numOverLaps(List start, List end) {
if (start == null || start.size() == 0 || end == null || end.size()
== 0) {
return 0;
}

if (start.size() != end.size()) {
return 0;
}

Collections.sort(start);
Collections.sort(end);

... 阅读全帖
l*****a
发帖数: 14598
48
来自主题: JobHunting版 - 请教 print factors 这个题
List> func(int n) {
List> result= new ArrayList>();
List cur=new ArrayList<>();
get(n,2,cur,result);
return result;
}
public get(int target,int start,List cur,List> result
) {
if(target==1) {
result.add(new ArrayList(cur));
return;
}
for(int i=start;i<=target/2;i++) {
if(target%i==0) {
cur.add(i);
get(target/i,i,c... 阅读全帖
s*********p
发帖数: 130
49
来自主题: JobHunting版 - facebook面经
那个最小会议室的这样行吗?先按开始时间和结束时间排序,然后逐个对比,有点类似
merge sort 的merge 阶段。
public class Solution {
public int numOverLaps(List start, List end) {
if (start == null || start.size() == 0 || end == null || end.size()
== 0) {
return 0;
}

if (start.size() != end.size()) {
return 0;
}

Collections.sort(start);
Collections.sort(end);

int startP = 0;
int endP = 0;

int nu... 阅读全帖
s*********p
发帖数: 130
50
来自主题: JobHunting版 - facebook面经
那个最小会议室的这样行吗?先按开始时间和结束时间排序,然后逐个对比,有点类似
merge sort 的merge 阶段。
public class Solution {
public int numOverLaps(List start, List end) {
if (start == null || start.size() == 0 || end == null || end.size()
== 0) {
return 0;
}

if (start.size() != end.size()) {
return 0;
}

Collections.sort(start);
Collections.sort(end);

int startP = 0;
int endP = 0;

int nu... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)