由买买提看人间百态

topics

全部话题 - 话题: arraylist
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
m*******t
发帖数: 1060
1
A simple solution as shown below.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PathFinder {
private static void printPath(List path) {
for (int i = 0; i < path.size(); i++) {
System.out.format("%2d", path.get(i));
if (i != path.size() - 1)
System.out.print("->");
else
System.out.println();
}
}
private boolean shouldSkip(List阅读全帖
z*******3
发帖数: 13709
2
java.lang.ClassCastException
这个异常最好记一下,常见异常,面试有时候会问你
就是强制类型转换错误
this.document = (ArrayList)document;
this.terms = (ArrayList)terms;
这两个里面有一个类型不是ArrayList,强制转换出错了
c*****g
发帖数: 76
3
来自主题: Programming版 - 有一道Java面试题能不能帮我看看
import java.util.*;
public class Person
{
String name;
int age;
char sex;
Person spouse, mother, father;
List children;
public Person(String name, int age, char sex)
{
this.name = name;
this.age = age;
this.sex = sex;
this.spouse = this.mother = this.father = null;
this.children = new ArrayList();
}
public void setMother(Person mum)
{
this.mother = mum;
mum.children.add( this );
}
public void setFather(Person dad)
{
this.f... 阅读全帖
w*********n
发帖数: 439
4
来自主题: Programming版 - 笨笨的问一个JAVA小问题
我初始化一个ArraList ,然后向这个ArrayList里面加入2个Integer,当我试
图改变其中一个Integer的值的时候,
居然改不掉。请问怎么回事?
ArrayList list = new ArrayList<>();
Integer age1 = 20;
Integer age2 = 20;
list.add(age1);
list.add(age2);
//change age2 int value to 30
age2 = 30;
System.out.println(list.get(0));
System.out.println(list.get(1));
————————————————————————————
Output:
20
20
请问为什么改不掉age2这个Integer的值。
G***l
发帖数: 355
5
这还不简单。
用一个hashtable存储,key是单词,values是arraylist,这个arraylist的第一个位置
存放次数,后面依次存放出现的句子数。
顺序读取文件,一个个句子一个个词的扫描。每扫描到一个词,加入到hashtable里或
者改变对于的value里面的值。
要是在java里,可以用treemap代替hashtable来存储,因为treemap的key是排序的。

the
g******i
发帖数: 354
6
If I am asked this question in interview, my immediate answer will be:
1) Build two HashMap
2) HashMap1 key is phone number, value is Arraylist to store all the name
with that phone number;
3) HashMap2 key is first name, value is ArrayList to store all the phone
number with that first Name.
d****n
发帖数: 233
7
来自主题: JobHunting版 - 请教一道题目
Here is the code in C#, please do let me know if you find any bug
// This program returns the shortest substring is src
// which contains all charactors in dest
// INT_MAX returned if no coverage found
static int MinimumCover(string src, string dst)
{
int minDist = int.MaxValue;
Hashtable ht = new Hashtable();
ArrayList al = new ArrayList();
foreach (char c in dst)
{
ht.Add(c,1);
c**s
发帖数: 23
8
来自主题: JobHunting版 - 请教一道面试题
static void printSum(int[] a, int index, int sum, ArrayList
results) {
if (sum == 0) {
System.out.println(results);
return;
}
for (int i = index; i < a.length; i++) {
if (sum >= a[i]) {
results.add(a[i]);
printSum(a, i, sum - a[i], results);
results.remove(results.size() - 1);
}
}
}
@Test
public void testPrintSum() {
printSum(new int[] { 1, 2, 6, 7, 3 }, 0, 7, new ArrayList());
}
Output:
[1, 1, 1, 1,
g**********y
发帖数: 14569
9
来自主题: JobHunting版 - Google的面经
radiochromatogram * suspensefulnesses = 289
public class WordProduct {
private final static String DIR = "src/test/resources/com/practice/
search";

public void search() {
int N = 30;
String content = FileHelper.readFile(DIR + "/WORD.LST");
String[] words = content.split("\n");
HashSet[] set = new HashSet[30];
HashMap map = new HashMap();

for (int i=0; i set[i] = ... 阅读全帖
E***n
发帖数: 166
10
来自主题: JobHunting版 - amazon intern 三面
1.Design a card game
should have a card class, deck class, hand class.
What should be within Deck? 我说了an arraylist of cards (52张牌)
Where should the cards be created (in deck or hand)? Removed (in deck or
hand)? 我说了created in deck,removed in hand。
2。coding for shuffle念给他听,我用经典的随机数+换牌,career cup上的算法
3. Linked list v.s. arraylist
4. Disadvantage of hashtable
5. Pass by reference v.s. Pass by value
Java use which one? Primitive: pass by value; objects, pass by referenece
6. GET v.s. POST
... 阅读全帖
g***s
发帖数: 3811
11
来自主题: JobHunting版 - 生物男的Google面经节略版
It is very quick even for n = Integer.MAX_VALUE;
public static void main(String arg[]){
preprocess();
int n=Integer.MAX_VALUE;
System.out.println("squares sum of " + n + " : ");
for (int num : getSquaresSum(n)){
System.out.print(" " + num);
}
System.out.println("\ntotal callCount= " + callCount);
}
public static Collection getSquaresSum(int n){
Stack cur = new Stack();
Stack阅读全帖
R***i
发帖数: 78
12
来自主题: JobHunting版 - how would you create the index for a book
TreeMap> map;
iterate each word in book from front to back
if word found in map, append current page number to ArrayList
if word not found in map, put to map, append current page number
TreeMap automatically sort on key, which is String type word
iterate map to generate "word -> page number" pairs
c*****l
发帖数: 879
13
某公司 问PI(就是圆周率那个) 里面有多少 1 2 3 序列
某公司 然我实现2个接口 一个是把一个arraylist里的一些string encode成一个
string
然后另一个接口把这个string还原为原来的string arraylist
答的都不好 估计人家都觉得我脑残了。。。
b**********r
发帖数: 91
14
来自主题: JobHunting版 - 问道amazon的面试题
Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖
b**********r
发帖数: 91
15
来自主题: JobHunting版 - 问道amazon的面试题
Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖
g**********y
发帖数: 14569
16
来自主题: JobHunting版 - 问道google面试题
简化一下code,
1. 对所有数按codingman的比较办法,看a+b, b+a谁大
2. 按次序输出
public String largest(String... numbers) {
StringBuilder builder = new StringBuilder();
ArrayList list = new ArrayList();
for (String number : numbers) list.add(number);
Collections.sort(list, new Comparator() {
public int compare(String a, String b) {
return (b+a).compareTo(a+b);
}
});
for (String s : list) builder.append(s);
return builder.toString();
}
g**********y
发帖数: 14569
17
来自主题: JobHunting版 - 一道G家题目
这个O(n)就够了:
public int[] recover(int[] a) {
int[] s = new int[a.length];
ArrayList list = new ArrayList();
for (int i=0; i
for (int i=0; i s[i] = list.remove(a[i]);
}

return s;
}
g**********y
发帖数: 14569
18
来自主题: JobHunting版 - 问个编程题
带输出的,不用分配那么多空间,只要记住parent index就行。
public int solve2(int[] a, int sum) {
ArrayList numbers = new ArrayList();
for (int i : a) if (i!=1) numbers.add(i);

int[][] m = new int[sum+1][2];
m[0] = new int[]{0, -1};
m[1] = new int[]{1, 0};
for (int i=2; i<=sum; i++) {
m[i] = new int[]{sum+1, -1};
for (int j : numbers) {
if (j<=i && m[i][0] > 1+m[i-j][0]) {
m[i][0] = 1+m[i... 阅读全帖
s*****y
发帖数: 897
19
来自主题: JobHunting版 - 一道G家题目的思路
难道我理解错了
s[i] = list.remove(a[i]); 返回这个list里面排a[i]的值,如果是delete a[i]可以
用skip list,但是现在要delete 第a[i]的值。
public int[] recover(int[] a) {
int[] s = new int[a.length];
ArrayList list = new ArrayList();
for (int i=0; i
for (int i=0; i s[i] = list.remove(a[i]);
}

return s;
}
a***r
发帖数: 93
20
来自主题: JobHunting版 - G onsite面经
LZ的这个题目, 我搞不懂什么意思,难道不是在两个arraylist中求common movie吗?
谁能说说为什么要用graph及怎么用graph来解?
==================================================================
第二个人是个大牛,言谈举止比较古怪,有点高傲,但还算gentle。
大牛废话特别多,讲题blabla一大堆,都没听明白,不知道重点是什么要让我做什么
。交流过后才大概清楚是一道貌似google题库题(我没见过,但是后面的面试官又提到
过这题)题目大概是这样的:actor和actor之间用同时参演的movie连接,给你两个方
法, getActorsByMovie() 和getMoviesByActor(), 让你去找连接两个actor的movies。
上来先问我这两个方法返回值用什么collection比较好,我说arraylist,然后解释为
什么。接下来开始解题,我说可以看成图,节点是actor,路径是movie,然后用广度或
者深度去遍历。我就回忆cracking the coding上4.2那... 阅读全帖
J***n
发帖数: 391
21
来自主题: JobHunting版 - 几个Java面试题 (转载)
【 以下文字转载自 Java 讨论区 】
发信人: JAlan (Alan), 信区: Java
标 题: 几个Java面试题
发信站: BBS 未名空间站 (Tue Sep 27 23:34:20 2011, 美东)
1. 如果数据查找多的话,需要使用哪种数据结构?
// 我复习下来,一直认为插入修改多用LinkedList,查询多的话用ArrayList. 但是好
像都不是正解。ArrayList如果查找value的话,也需要遍历整个列表。后来想了想,查
找最快的话就是binarySearch了,但是要基于sorted list的基础上,那是不是应该使
用SortedLinkedList呢?
2. 1 million的数据 (key-value),多查找,需要使用哪种数据结构?
// TreeMap 吗?
3. 使用线程实现1 billion 整数的求和,最后返回一个数
// 我把数据分成10份,定义10个线程来分别来做求和,最后把每个线程所得数相加,
得到最后的数。不知道思路对不对?
不过我困惑的是,如果是单一任务的话,难道不是单线程要比多线程快吗?可以一口气
运行,为什么还要... 阅读全帖
q*****t
发帖数: 3
22
来自主题: JobHunting版 - 问个Amazon面试题
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[... 阅读全帖
q*****t
发帖数: 3
23
来自主题: JobHunting版 - 问个Amazon面试题
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[... 阅读全帖
s******n
发帖数: 3946
24
来自主题: JobHunting版 - 再上到题
加了重复value处理,选value相同的index最大的一个,这样可以了吧?
class PowerImpl {
static class Node {
public int index;
public int value;
int compare(Node anotherNode) {
return this.value - anotherNode.value;
}
}
PriorityQueue values;
public PowerImpl() {
values.add(new Node(2, 4));
}
public int next() {
Node n = values.pop();
int retValue = n.value;
// find all nodes with least value
// for such nodes, values*=index
// find out n: the least value node with largest index
A... 阅读全帖
v***n
发帖数: 562
25
下面那个is_free方法是什么作用?谢谢!
下面第四版的解答:
􀀙􀀏􀀓􀀁 Imagine a robot sitting on the upper left hand corner of an NxN
grid. The robot can
only move in two directions: right and down. How many possible paths are
there for
the robot?
FOLLOW UP
Imagine certain squares are “o$ limits”, such that the robot can not
step on them.
Design an algorithm to get all possible paths for the robot.
pg 64
Part 1: (For clarity, we will solve this part assuming an X by Y grid)
Each path has (X-... 阅读全帖
p*****2
发帖数: 21240
26
来自主题: JobHunting版 - 关于质数(prime number)的算法题
public static void main(String[] args)
{
ArrayList primes=new ArrayList();
int n=100;
primes.add(2);
for(int i=3;i<=Integer.MAX_VALUE;i+=2)
{
int j=0;
boolean fprime=true;
for(;j {
int k=primes.get(j);
if(k>Math.sqrt(i))
break;
if(i%k==0)
{
fprime=false;
break;
}
}
if(fprime)
{
primes.add(i);
if(primes.size()==n)
break;
}
}
for(int i:primes)
System.out.print(i+" ");
}
H***e
发帖数: 476
27
来自主题: JobHunting版 - 关于质数(prime number)的算法题
几个优化:
1。只要测试到sqrt(i)就可以了
2。每个数都测试从2开始有点浪费了。 可以在得到2的时候,直接把它所有的倍数化掉
,这样以后就不用测试6的倍数了之类的。
这也是我问初始用来划的array size取多少合适.
===========
public static void main(String[] args)
{
ArrayList primes=new ArrayList();
int n=100;
primes.add(2);
for(int i=3;i<=Integer.MAX_VALUE;i+=2)
{
int j=0;
for(;j if(i%primes.get(j)==0)
break;
if(j==primes.size())
primes.add(i);
if(primes.size()==n)
break;
}
for(int i:primes)
System.out.print(i+" ");
}
s*******e
发帖数: 35
28
来自主题: JobHunting版 - facebook一题
用HASHMAP 找出每个WORD 所在的位置 occurrence map,在做一个反向的由所在位置(
position)到WORD 的reverse occurrence map。 吧这些position 排序,然后递归搜索。
package smartnose;
import java.util.Arrays;
import java.util.*;
public class SubWordSeq {


public static void main(String [] args){
List L = new LinkedList();

L.add("fooo");L.add("barr");L.add("wing");L.add("ding");L.add("wing"
);

String S= "lingmindraboofooowingdingbarrwingmonkeypoundcake";

HashMap<... 阅读全帖
s*******e
发帖数: 35
29
来自主题: JobHunting版 - facebook一题
用HASHMAP 找出每个WORD 所在的位置 occurrence map,在做一个反向的由所在位置(
position)到WORD 的reverse occurrence map。 吧这些position 排序,然后递归搜索。
package smartnose;
import java.util.Arrays;
import java.util.*;
public class SubWordSeq {


public static void main(String [] args){
List L = new LinkedList();

L.add("fooo");L.add("barr");L.add("wing");L.add("ding");L.add("wing"
);

String S= "lingmindraboofooowingdingbarrwingmonkeypoundcake";

HashMap<... 阅读全帖
w*********0
发帖数: 48
30
来自主题: 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
31
来自主题: 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) == '(')... 阅读全帖
r****m
发帖数: 70
32
来自主题: JobHunting版 - Text Justification
先计算可以放的最大单词数,然后在单词中插入空格输出行
public class AdjustText {
static List justifyText(String[] words, int L) {
List result = new ArrayList();

int i = 0;
while (i < words.length) {
List lineWords = new ArrayList();
int curLen = 0;
do {
lineWords.add(words[i]);
curLen += words[i].length() + 1;
i++;
} while (i阅读全帖
X*K
发帖数: 87
33
来自主题: JobHunting版 - 讨论一个题目
靠,想来不难,但是很容易出错啊。这个算是验证过了。
public class NextPower implements Powers {
List powerList;
long previous;
public NextPower() {
powerList = new ArrayList();
reset();
}
@Override
public Long next() {
long min = Long.MAX_VALUE;
List minList = new ArrayList();
for (int i = 0; i < powerList.size(); ++i) {
int base = i + 2;
int power = powerList.get(i) + 1;
long n = (long) ... 阅读全帖
z****h
发帖数: 164
34
来自主题: JobHunting版 - 这题被问过两次都不会,请教
定义了如下数据结构。是不是太繁琐了?
class Peolple
{
int ID;
ArrayList peopleIKnow;
public void People (int i)
{
ID = i;
}
public void addAcquaintance(People p)
...
}
public People findCelebrity(ArrayList people)
{
...
}
p*******m
发帖数: 47
35
来自主题: JobHunting版 - 急问一道本周 Microsoft 电面题
BT树结构, 中间节点是算数运算符(只有+ - * / 4种操作), 叶节点是数字, 要求给出
算数表达式 (要求没有冗余括号)
比如
*
/ \
+ *
/ \ / \
1 2 4 5

表达式 = (1 + 2) * 4 * 5, 不能是 (1+2)*(4*5)
+
/ \
* +
/ \ / \
1 2 4 5
表达式 = 1 * 2 + 4 + 5, 不能是 1 * 2 + (4 + 5)
总之, 这题的难点是 算数表达式不能有冗余括号
我当时的思路: in-order 递归遍历, 遇到 + - 给出左右括号 (但这样就有冗余括号).
面试官指出后, 我说我可以再扫描遍得到的表达式,去除冗余括号 (这也是我情急下
蒙的).
他说不行, 只能在遍历BT时一次完成. 他提示考虑运算符的优先级, 但后来时间到了,
也就草草收场
真心请教思路, 多谢
=========================================
class No... 阅读全帖
D********g
发帖数: 650
36
来自主题: JobHunting版 - 一道G家店面题
加上了backtracking:
static class DPElement {
int value;
int prevCostIdx; // 0-based cost idx
int takenThisElement; // 0/1, indicating whether taking current
element, idx 0 based.
public DPElement() {
value = 0;
prevCostIdx = 0;
takenThisElement = 0;
}
}
static String word2Anagram(final String word) {
if (word == null) {
return null;
}

int ht[] = new int[256];
f... 阅读全帖
l*****a
发帖数: 14598
37
来自主题: JobHunting版 - 被gray code打击了
1)why use Integer and int simultaneously?
2)most of the JAVA developers won't write like this
ArrayList al = new ArrayList();
s*****9
发帖数: 93
38
来自主题: JobHunting版 - leetcode的Text Justification的OJ
下面这个测试例通过不了
input output expected
[""], 2 [] [" "]
我加了下面的代码处理这个corner case。 OJ的这个测试例有问题。
ArrayList paragraph = new ArrayList();

if(words.length == 0) {
StringBuffer line = new StringBuffer();
for(int j = 0; j < L; j++) {
line.append(" ");
}
paragraph.add(line.toString());
return paragraph;
}
w***o
发帖数: 109
39
来自主题: JobHunting版 - 问几道题目
int N = 100000000;
ArrayList output = new ArrayList();
output.add(1);

int[] prime = {47, 97};
int K = prime.length;
int[] index = new int[K];
int[] num = new int[K];

for (int i = 0; i < K; i++)
num[i] = output.get(0) * prime[i];

while(min < N)
{
int min = num[0];
for(int j = 0; j < K; j++)
{
if (num[j] < min)
min = num[j];
}

if (min > N)
break;

output.add(min);
for(int j = 0; j < K; j+... 阅读全帖
a*****s
发帖数: 1121
40
来自主题: JobHunting版 - a1b2c3d4 变abcd1234
public ArrayList groupSwap(ArrayList str, int i, int j){
int p1=0;
int p2=str.size()-1;
//first consider boundary condition 1 all belongs to group 1
while(Integer.parseInt(str.get(p1).toString())%2==1 && p1 p1++;
}
//then consider boundary condition 2 all belongs to group2
while(Integer.parseInt(str.get(p2).toString())%2==0 && p1 p2--;
}
if(p1==p2) return str;
//nor... 阅读全帖
l***m
发帖数: 339
41
来自主题: JobHunting版 - 罗马转数字,数字转罗马问题
JAVA 不太熟,尝试着快速写了下这两道题,请各位大牛帮忙看看啊,有没有什么
问题。当然我这里没有考虑大数问题,这个呆会我再想想,或者谁教我也好。
public class RomansToInt {
public static HashMap map;
public static int RomanToInt(String input) {
if (input == null || input.length() == 0) {
System.out.println("The input is not valid");
return -1;
}
int len = input.length();
if (len == 1) {
return map.get(input.charAt(0));
}
int result = 0;
int i =... 阅读全帖
s*****1
发帖数: 134
42
来自主题: JobHunting版 - 关于leetcode的combinationSum题
用java做了leetcode的combinationSum,其实就是DP,
我造了一个ArrayList> 的二维数组,假设数组名为 comb,
我从comb[0][0] 一直到 comb[input数组长度][target]开始DP,
我想算法没问题,但可能太耗空间了,一直是runtime error
我想请问版上是否有高人,给个这题的java版本,能够通过online的大小数据测试?谢
谢啦
c++版本我已经搜过了
t********e
发帖数: 344
43
来自主题: JobHunting版 - G电面题 + 求祝福
另外我弃用arraylist是考虑到arraylist的capacity永远是大于等于size的,我以为
interviewer希望我尽量省memory
唉都是我自己瞎猜的,没有面试经验,当时非常紧张……
c********t
发帖数: 5706
44
Given a binary tree, return the bottom-up level order traversal of its nodes
' values. (ie, from left to right, level by level from leaf to root).
我用层打印存入ArrayList,然后翻转ArrayList解的。大侠女侠们用没有更好的方法?
b*****6
发帖数: 179
45
来自主题: JobHunting版 - 50行code能解决addbinary 问题么
2 public String addBinary(String a, String b) {
3 int i = a.length() - 1;
4 int j = b.length() - 1;
5 int carry = 0;
6
7 ArrayList result = new ArrayList();
8
9 while (i >= 0 || j >= 0 || carry == 1)
10 {
11 int cur = carry;
12
13 if (i >= 0)
14 cur += a.charAt(i--) - '0';
15
16 if (j >= 0)
17 cur += ... 阅读全帖
y***u
发帖数: 174
46
这个行么?recursive的。
我需要暂时把左子树或者右子树变成null,不然太麻烦。
void Traverse(ArrayList output, Node A, Node B){
if(A==null && B==null)
return;
else if(A==null){
output.add(B.val);
return;
}else if(B==null){
output.add(A.val);
return;
}else{
Node t1 = null, t2=null;
if(B.val>A.val){
Trav(output, A, B);
}else if(B.val < A.val){
Trav(output, B, A);
}else{
output.add(A.val);
ou... 阅读全帖
g**********y
发帖数: 14569
47
来自主题: JobHunting版 - java 求助
HashSet>[] set = new HashSet[100];
for (int i=0; i<100; i++) {
set[i] = new HashSet>();
}
S**I
发帖数: 15689
48
☆─────────────────────────────────────☆
princekim (Prince Kim) 于 (Wed Apr 11 09:32:26 2012, 美东) 提到:
BT树结构, 中间节点是算数运算符(只有+ - * / 4种操作), 叶节点是数字, 要求给出
算数表达式 (要求没有冗余括号)
比如
*
/ \
+ *
/ \ / \
1 2 4 5

表达式 = (1 + 2) * 4 * 5, 不能是 (1+2)*(4*5)
+
/ \
* +
/ \ / \
1 2 4 5
表达式 = 1 * 2 + 4 + 5, 不能是 1 * 2 + (4 + 5)
总之, 这题的难点是 算数表达式不能有冗余括号
我当时的思路: in-order 递归遍历, 遇到 + - 给出左右括号 (但这样就有冗余括号).
面试官指出后, 我说我可以再扫描遍得到的表达式,去除冗余括号 (这也是我情急下
蒙的).
他说不行, ... 阅读全帖
S**I
发帖数: 15689
49
☆─────────────────────────────────────☆
bailngw (bailing) 于 (Tue Apr 10 12:32:15 2012, 美东) 提到:
昨天居然又被问到了:
屋子里有n个人,如果i 认识 j, 那么他们之间有条directed edge. 如果有这么一个人
:大家都认识他,但他不认识其他任何人,我们叫他celebrity.
如果在O(n)时间找出celebrity?
谢谢
☆─────────────────────────────────────☆
longway2008 (longway2008) 于 (Tue Apr 10 12:39:51 2012, 美东) 提到:
DFS ?

☆─────────────────────────────────────☆
peking2 (myfacebook) 于 (Tue Apr 10 12:39:58 2012, 美东) 提到:
建个图之有入没有出就是了
昨天居然又被问到了:屋子里有n个人,如果i 认识 j, 那么他们之间有条directed
edge... 阅读全帖
p*****2
发帖数: 21240
50
刚才看了一下ArrayList里边可以加null, 用两个arraylist + BFS应该可以很简单的
完成了。
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)