m******t 发帖数: 2416 | 1
I'd go with ArrayList unless your project is on J2ME.
Just remember to use Boolean.valueOf(value) rather than new Boolean(value)
everytime when you insert an element. That way you won't end up with
a bunch of different Boolean objects. |
|
g*****g 发帖数: 34805 | 2 simplest way, keep an array of boolean, mark a boolean when a thread
is done. The main thread use a
while(boolean array is not completely set){
sleep
}
Or you can use notify to achieve better performance. |
|
o****i 发帖数: 1706 | 3 【 以下文字转载自 Programming 讨论区 】
发信人: ouyadi (可乐会捂帮帮众), 信区: Programming
标 题: Java代码,老是compile出错,大家帮我看看哪错了。。。
发信站: BBS 未名空间站 (Wed Apr 27 16:58:23 2011, 美东)
我在git上push到学校的服务器后编译老是不通过,错误如下,麻烦大家帮我查下原因
,谢谢啦!
javac -classpath /usr/share/junit/lib/junit.jar -d classes -sourcepath .
Iterator.java List.java TestUpTree.java UpTree.java
UpTree.java:34: type Iterator does not take parameters
public class NIterator implements Iterator{
^
UpTree.java:104: type Iterator does not take parameters
pub... 阅读全帖 |
|
p*****3 发帖数: 488 | 4 原来java里有balanced的BST, 比C++的好用些,就是接口太多了,记不住。
写了一个求2d overlap矩形面积的题,
核心代码没多少,code都是定义各种结构各种override各种comparator去了:
public class EPI_14_20_3 {
static class Rectangle {
public int xBeg;
public int xEnd;
public int yBeg;
public int yEnd;
public Rectangle(int xb, int xe, int yb, int ye) {
xBeg = xb;
xEnd = xe;
yBeg = yb;
yEnd = ye;
}
}
static class EndPoint {
public int index... 阅读全帖 |
|
n******1 发帖数: 3756 | 5 自己练习写了一个多线程读一个字符串,但是没法退出,想请教两个问题
1.这种写法是我自己想出来,有没有更优雅一点的方法呢
2.线程间应该怎么通知结束信号然后退出的?
感觉多线程难用的原因因为需要全部理解才能用好,一个地方没理解好,就用不好
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
class Reader implements Runnable {
private int id;
private String[] queue;
private Object lock;
private volatile AtomicInteger index;
private int threads;
private volatile boolean done = false;
public Reader(int id, String[] queue, Object lock, AtomicInteger index,
... 阅读全帖 |
|
o****i 发帖数: 1706 | 6 我在git上push到学校的服务器后编译老是不通过,错误如下,麻烦大家帮我查下原因
,谢谢啦!
javac -classpath /usr/share/junit/lib/junit.jar -d classes -sourcepath .
Iterator.java List.java TestUpTree.java UpTree.java
UpTree.java:34: type Iterator does not take parameters
public class NIterator implements Iterator{
^
UpTree.java:104: type Iterator does not take parameters
public class SetIterator implements Iterator{
^
TestUpTree.java:51: type Iterator does not take parameters
Iterator it = up.m... 阅读全帖 |
|
p*****2 发帖数: 21240 | 7 java 400多毫秒,scala 1000毫秒超时。
object test6 extends App {
def add(x:Int, y:Int):Unit={
if(x<1 || x>n || y<0) return;
val yy=Math.min(y,a(x)+1)
if(v(x)(yy)) return
queue+=Array(x,yy)
v(x)(yy)=true
}
val sc=new Scanner(new File("input.txt"))
val out=new PrintWriter("output.txt")
val n=sc.nextInt
val a=new Array[Int](n+1)
val v=Array.ofDim[Boolean](105, 100005)
val queue=Queue[Array[Int]]()
for(i<-1 to n) a(i)=sc.nextInt
... 阅读全帖 |
|
m*****r 发帖数: 37 | 8 我是最近刚转Java的弱弱,有几个小问题,请牛牛们轻拍。
这次转java,总算像是某位说的那样,原来c++转java也就分分钟的事儿。写得顺的时
候,边google边写,也可以写得稀里糊涂、腾云架雾、行云流水;可问题是bug一出现
,立马歇菜!比如,min stack, stack1.peek()==stack2.peek(),顶上的两个数字明明
是相等的,为什么会return false呢?想去google都不知道该搜什么。。。等把java刷
一遍lc就算我可以写java了?
我有个function, public boolean dfdjdf(){return dfkld;} 为什么它一定要我在最
后一句话加个return啊,方法里的逻辑到那一步早就应该已经return了啊?
还有就是,刷lc搭了个local的架,本来没什么心理障碍,可是每遇到有Node,
ListNode, TreeNode的题就一股无名火,我不知道该把这些定义的类塞到哪里?试过两
种:
public class getIntersectionNode {
public class ListNode {
... 阅读全帖 |
|
gw 发帖数: 2175 | 9 谢谢好人回复
public class Percolation {
public Percolation(int N) // create N-by-N grid, with all
sites blocked
public void open(int i, int j) // open site (row i, column j) if
it is not open already
public boolean isOpen(int i, int j) // is site (row i, column j) open?
public boolean isFull(int i, int j) // is site (row i, column j) full?
public boolean percolates() // does the system percolate?
public static void main(String[] args // te... 阅读全帖 |
|
x****u 发帖数: 44466 | 10 你这纯属数理逻辑基础没学好。。。
能用布尔逻辑写出来的东西,都是广义上的推理,此类问题求解已被证明NPC。
In computer science, the Boolean Satisfiability Problem (sometimes called
Propositional Satisfiability Problem and abbreviated as SATISFIABILITY or
SAT) is the problem of determining if there exists an interpretation that
satisfies a given Boolean formula. In other words, it asks whether the
variables of a given Boolean formula can be consistently replaced by the
values TRUE or FALSE in such a way that the formula evaluates to TRUE. If
this i... 阅读全帖 |
|
I***a 发帖数: 704 | 11 In Design Compiler, 我用write_sdf命令得到的.sdf文件back-annotate到综合后的
verilog netlist总是有这个问题:
Instance '/FFT8inputs/\Regs_4/Q_reg[20] ' does not have a generic named 'tpd
_clk_q_posedge'.
tpd_clk_q_posedge, tpd_clk_qbar_posedge, tsetup_d_clk_posedge_posedge,
tsetup_d_clk_negedge_posedge, thold_d_clk_posedge_posedge, thold_d_clk_
negedge_posedge,
这6个generic参数说是对应的DFF cell里没有:
entity DFF_E is
generic(
TimingChecksOn: Boolean := True;
InstancePath: STRING := "*";
Xon: Boolean := False;
... 阅读全帖 |
|
l******t 发帖数: 55733 | 12 弄个稍微复杂点的generic伪代码,如果有lambda就更爽了。不知道新的Java 8有没
filter/map/folder。Number有没扩充。
List filter (final List list, Filter filter) {
if(list == null) return null;
List ret = new LinkedList();
for(T item: list){
if(filter(item) ret.add(item);
}
return ret;
}
interface Filter{
boolean filter(T);
}
List removeNeg(List list){
return filter(list, new Filter{
@Override boolean filter(Integer num){ return n... 阅读全帖 |
|
T**********y 发帖数: 157 | 13 http://www.ccse.uestc.edu.cn/teacher/teacher.aspx?id=414
所有已经发表论文清单
(发表时间序)
【1】 周涛,傅忠谦,周佩玲,张建荣,张德学,”基于遗传算法的大规模流量
工程问题求解”,计算机应用,2003年第6期,43-45
【2】 杨春霞,周涛,周佩玲,刘隽,基于Multi_Agent的股市经济系统建模与
分析,自动化理论、技术与应用卷十,中国科学技术大学出版社,2003年,596-601(
中国自动化学会第18届青年学术年会会议论文集)
【3】 周佩玲,许民,赵亮,周涛,”混沌信号奇异性检测与外界冲击度量”,
数据采集与处理,Vol.19,195-198,2004
【4】 周涛,徐俊明,刘隽,”图直径和平均距离极值问题研究”,中国科学技
术大学学报,Vol.34,410-413,2004
【5】 周佩玲,杨春霞,周涛,李立文,”虚拟股市建模与混沌分析”,中国科
学技术大学学报,Vol.34,442-448,2004
【6】 T. Zhou, P. ... 阅读全帖 |
|
r****x 发帖数: 1250 | 14 运行程序时总是有这个出错信息:
OS version: 5.1.2600.196608
okeoke version: 2.3.2.0
Program Folder: G:\OkeOke.Net\
Object reference not set to an instance of an object.
at OkeOke.Net.Controls.PlayListPanel.<_player_OnPlayerStopped>b__1() in D
:\Dev\OkeOke\OkeOke.Net\Controls\PlayListPanel.xaml.cs:line 566
at OkeOke.Net.Execute.<>c__DisplayClass2.b__0(
Action action) in D:\Dev\OkeOke\OkeOke.Net\Utilities\Execute.cs:line 26
at OkeOke.Net.Execute.OnUIThreadAsync(Action action) in... 阅读全帖 |
|
r****x 发帖数: 1250 | 15 运行程序时总是有这个出错信息:
OS version: 5.1.2600.196608
okeoke version: 2.3.2.0
Program Folder: G:\OkeOke.Net\
Object reference not set to an instance of an object.
at OkeOke.Net.Controls.PlayListPanel.<_player_OnPlayerStopped>b__1() in D
:\Dev\OkeOke\OkeOke.Net\Controls\PlayListPanel.xaml.cs:line 566
at OkeOke.Net.Execute.<>c__DisplayClass2.b__0(
Action action) in D:\Dev\OkeOke\OkeOke.Net\Utilities\Execute.cs:line 26
at OkeOke.Net.Execute.OnUIThreadAsync(Action action) in... 阅读全帖 |
|
z*******y 发帖数: 578 | 16 另外附上java 写的code
写code的时候发现上面两种情况其实是一种情况
private static void calculatePermutation(int N, int k) {
StringBuilder str = new StringBuilder();
boolean B[] = new boolean[N];
for (int i = 0; i < N; i++) {
int f = factorial(N - i - 1);
int num = k / f + 1;
for (int j = 0; j < N; j++) {
if (B[j] == false) {
num--;
if (num == 0) {
str.append(j + 1);
B[j] = true;
break... 阅读全帖 |
|
y****n 发帖数: 192 | 17 有那么夸张吗?
private static Boolean isPrefix(String prefix, String s1) {
if(prefix.length()>s1.length())
return false;
for(int i=0; i
boolean t = prefix.charAt(i)==s1.charAt(i);
if (false==t)
return false;
else if (true==t&& i == prefix.length()-1)
return true;
}
return false;
} |
|
y****n 发帖数: 192 | 18 有那么夸张吗?
private static Boolean isPrefix(String prefix, String s1) {
if(prefix.length()>s1.length())
return false;
for(int i=0; i
boolean t = prefix.charAt(i)==s1.charAt(i);
if (false==t)
return false;
else if (true==t&& i == prefix.length()-1)
return true;
}
return false;
} |
|
m******9 发帖数: 968 | 19 问题是这样的,多线程中典型的模型就是:
对于某个thread p1
Wait(lock)
// enter critical section
Signal(lock)
比如 lock就是一个mutex,当p1获得了这个lock以后,它进入critical section,工作
完了,就释放lock
他的问题就是:
如果用一个普通的global variable:
global boolean lock = false;
进行如下的操作
thread p1
step 1: 获得lock
step 2: lock = true;
step 3: // enter critical section
step 4: 释放lock ( lock reset to false)
假设step 1和step2 是不能被中断的(atomic),任何thread在进入critical section
之前,都要检查global boolean lock。那这种利用global variable的方法能不能达到
线程安全的目的? |
|
c*********n 发帖数: 1057 | 20 多谢指教,用你的思想写了下code,各位看看对不对:
//L[i][j] = L[i+1][j-1] && s[i]==s[j]
public static int findNumberOfPalindrom(String str){
boolean[][] L = new boolean[str.length()][str.length()];
int count=0;
//Init
int i;
for(i=0;i
L[i][i]=true;
}
//From substring with length 3 to max length
for(int k = 2;k
for(i=0;i
L[i][i+k]=L[i+1][i |
|
p******r 发帖数: 2999 | 21 prime的算法不需要数据结构
public static void prime(int a, int b) {
if (b < 1 || a > b) return;
boolean[] p = new boolean[b + 1];
for (int i = 2; i <= b; i ++) {
if (p[i]) continue;
int j = i + i;
while (j <= b) {
p[j] = true;
j += i;
}
}
for (int i = a; i <= b; i ++) {
if (!p[i]) System.out.print(i + " ");
}
}
test case就是检验你的算法的对错
例如prime(-100, -2), prime(1, 1), prime(2, 20)
这些你都知道结果,你的算法应该输出你想要的东西
第一次面试我也弄得很糟糕(事后发现算法有问题),但是还是拿到了第二次面试
good luck
hash |
|
I**A 发帖数: 2345 | 22 MIT 那个 hack google interview 上给的code,是不是有问题?
boolean isValid(Node root) {
return isValidHelper(root, Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
boolean isValidHelper(Node curr, int min, int max) {
if (curr.left != null) {
if (curr.left.value < min ||
!isValidHelper(curr.left, min, curr.value))
return false;
}
if (curr.right != null) {
if (curr.right.value > max ||
!isValidHelper(curr.right, curr.value, max))
return false;
}
return true;
} |
|
y*********e 发帖数: 518 | 23 第一题没有那么复杂把。就是一个DFS搜索BTree。
死劲的朝深走,路堵了就回溯。上Java代码:
boolean hasPath(Point a, Point b) {
if (isBlocked(a))
return false;
if (a.equals(b))
return true;
return hasPath(goDown(a), b) || hasPath(goRight(a), b);
}
Point goDown(Point p) {
return new Point(p.x + 1, p.y);
}
Point goRight(Point p) {
return new Point(p.x, p.y + 1);
}
这的解法只针对a是最左上角,b是最右下角的情况。
若是起始点不在最左上角,那么就需要考虑四个方向的走发了。楼主的面试题没有那么复杂,楼上的诸位多虑了。
而且,即使是可以朝着四个方向走的化,代码也可以写的很简洁的。
boolean hasPath(Point a, Point b) {
|
|
y*********e 发帖数: 518 | 24 关于那个iterator的东西,若是用C#来yield return最是简单了。
using System;
using System.Collections.Generic;
public class InOrderTraverse
{
private BTree tree;
private Stack stack;
public InOrderTraverse(BTree tree) {
this.tree = tree;
this.stack = new Stack();
}
public IEnumerable GetEnumerator() {
BTree current = this.tree;
while (true) {
while (current != null) {
stack.Push(current);
curre... 阅读全帖 |
|
y***m 发帖数: 7027 | 25 可以么?
在网上申请的SDE职位。一共两轮电话面试,一轮onsite。
第一轮电话面试:
1、解释Hash Table,包括“可以用什么数据结构实现hash table”,“what is a
good hash function”,“什么是load factor”。
2、算法:删除一个给定数列中重复的元素。
hashmap 记录,++, 删除 2的
3、merge两个有序数组。要求先给他解释算法,再写代码。他当时给了我十分钟的时间
,让我写好以后发到他邮箱里。
k=i+j
a[i],b[j],c[k]
int ii=jj=kk=0;
while(kk
if(a[ii]>b[jj]){
c[kk]=b[jj];
jj++;
} else {
c[kk]=a[ii];
ii++;
}
kk++;
}
4、OOD:设计一个汽车出租(Car Rental A... 阅读全帖 |
|
y***m 发帖数: 7027 | 26 设计一个汽车出租(Car Rental Agency)的系统。他先问我如果要实现
vehicle search,需要哪些类;然后又问要实现rent a car,又需要哪些类;最后问如
果快到了交车截至时间,需要向用户发送提醒的邮件,应该怎么做。
abstract calss car
calss BMW extends car
calss Honda extends car
....
interface CarManager
class CarManagerImpl {
List T search(Class clazz){
}
T search(Class clazz){
}
}
class User {
List usercarList;
User();
boolean Rent(T entry){
usercarList.add
}
}
class UserCar {
List usercarList;
... 阅读全帖 |
|
s*********3 发帖数: 389 | 27 I wrote some Java code afterwards. It seems not very clean. Please comment.
import java.util.*;
//Assumptions:
//1. We can move either up or down or left or right at any point in time,
but not diagonally.
//2. One item in the matrix can not be used twice to compose of the pattern
string.
//3. If different routes lead to the same indexes of matrix items to compose
of the pattern string, display them.
//4. The maximum number of movable steps is (matrix.length - 1) + (matrix[0]
.length - 1) = matri... 阅读全帖 |
|
g**********y 发帖数: 14569 | 28 抛砖引玉吧,
public ArrayList findIntersection(int[][] a) {
ArrayList list = new ArrayList();
int[] p = new int[a.length];
boolean endSearch = false;
do {
int min = 0;
boolean equals = true;
for (int i=1; i
if (a[min][p[min]] > a[i][p[i]]) {
min = i;
equals = false;
continue;
}
... 阅读全帖 |
|
s*****y 发帖数: 897 | 29 今天研究了一下你的算法,写了一个c++版本的,实在很巧妙,
grass的算法太高了,一般人肯定想不到,你的算法比较适合平常人。
你的算法其实关键的地方在于:
你一开始用这个方程式算出了有多少个点
int N = (int)((1+Math.sqrt(1+8*L))/2);
因为N 个点,总的数组大小就是N + (N-1) + .....+1; 只要解了这个方程式,就知道
点数了。
然后后面,你应用同样的方程式
lb = Math.max(remaining*(remaining-1)/2-1, lb-1);
去推断剩下你只需要search 数组里面的哪些点,这个范围一般就是1-2之内,从而大大
减少了search的空间
真得很巧妙,很容易学习。而且速度的确快,运行火鸡同学的终极测试大概只需要200-
300ms的样子。
但是你得code似乎有个小bug,算
{7,10,5,2,8,3}; 得出2,3,5
原因是因为你的founded 里面是10,8的时候,你check 5的时候没有先把map[5]-- ,但
是10- 5 = 5,所以后面return 了true.
所以我觉得你的fun... 阅读全帖 |
|
g**********y 发帖数: 14569 | 30 主要是Bravethinker的code, 改动很小:
1. 试candidate时,对相同值,用index最小的那个,这样往下滑得更快。
2. founded不用ArrayList, 改成数组,速度更快。
3. 测试candidate时,直接改map; 测试不通过时revert修改。不过这个改动好象无关
紧要,不怎么影响速度。
public class BraveThinker implements IMilestone {
private int dfsCount = 0;
public int[] findMilestones(int[] dists) {
long t0 = System.currentTimeMillis();
dfsCount = 0;
int L = dists.length;
int N = (int) ((Math.sqrt(1 + 8 * L) - 1) / 2);
int[] founded = new int[N+1];... 阅读全帖 |
|
s*****y 发帖数: 897 | 31 今天研究了一下你的算法,写了一个c++版本的,实在很巧妙,
grass的算法太高了,一般人肯定想不到,你的算法比较适合平常人。
你的算法其实关键的地方在于:
你一开始用这个方程式算出了有多少个点
int N = (int)((1+Math.sqrt(1+8*L))/2);
因为N 个点,总的数组大小就是N + (N-1) + .....+1; 只要解了这个方程式,就知道
点数了。
然后后面,你应用同样的方程式
lb = Math.max(remaining*(remaining-1)/2-1, lb-1);
去推断剩下你只需要search 数组里面的哪些点,这个范围一般就是1-2之内,从而大大
减少了search的空间
真得很巧妙,很容易学习。而且速度的确快,运行火鸡同学的终极测试大概只需要200-
300ms的样子。
但是你得code似乎有个小bug,算
{7,10,5,2,8,3}; 得出2,3,5
原因是因为你的founded 里面是10,8的时候,你check 5的时候没有先把map[5]-- ,但
是10- 5 = 5,所以后面return 了true.
所以我觉得你的fun... 阅读全帖 |
|
g**********y 发帖数: 14569 | 32 主要是Bravethinker的code, 改动很小:
1. 试candidate时,对相同值,用index最小的那个,这样往下滑得更快。
2. founded不用ArrayList, 改成数组,速度更快。
3. 测试candidate时,直接改map; 测试不通过时revert修改。不过这个改动好象无关
紧要,不怎么影响速度。
public class BraveThinker implements IMilestone {
private int dfsCount = 0;
public int[] findMilestones(int[] dists) {
long t0 = System.currentTimeMillis();
dfsCount = 0;
int L = dists.length;
int N = (int) ((Math.sqrt(1 + 8 * L) - 1) / 2);
int[] founded = new int[N+1];... 阅读全帖 |
|
f*********i 发帖数: 197 | 33 A的onsite,我尽量客观写,大家帮我分析一下
第一个 美国人, 两个string,问第一个是否包含第二个的全部character,扩展是求第一
个string最短的substring包含第二个中所有的character.
我的解法是hashtable, 然后扩展问题是O(n)那个,先找到第一个包含全部的,然后从头
开始删除,接着从尾巴增加那个.面试官表示满意.
OOD是设计card game
交流过程很融洽.
第二个 hiring manage,印度人, 这个人带我去吃饭,然后问了我电梯设计问题,纠缠在
什么时候读楼层,还有如何判断要不要停下来接人.说了快50分钟.我个人感觉还好,反正
他也是都是笑到最后
第三个,白人,一开始问我LRU cache的设计,我直接告诉他double linked list + hash
table,他很吃惊问我是不是准备过,我说是,然后换了一题Bianry tree找两个节点的最
小公共父亲, 一开始是允许有父节点的,然后扩展到没有父亲节点,我给了O(nlogn)解法
public static BinaryTree tree_least_comm... 阅读全帖 |
|
r**e 发帖数: 226 | 34 为什么答案是 3? 不是2么?
package test;
import java.util.HashSet;
public class Test {
private String str;
public Test(String str) {
this.str = str;
}
@Override
public int hashCode() {
int hascode = this.str.hashCode();
return hascode;
}
@Override
public boolean equals(Object obj) {
return this.str.equals(obj);
}
public static void main(String args[]) {
Test h1 = new Test("1");
Test h2 = new Tes... 阅读全帖 |
|
g**********y 发帖数: 14569 | 35 写了个最原始的,一点智能都没有的:
public class Sudoku {
private int[] t = new int[9];
public boolean solve(int[][] matrix) {
Point p = nextUnknown(matrix);
if (p == null) {
print(matrix);
return true;
}
for (int i=1; i<10; i++) {
matrix[p.x][p.y] = i;
if (pass(matrix) && solve(matrix)) return true;
}
matrix[p.x][p.y] = 0;
return false;
}
private Point nextUnknown(int[][] ma... 阅读全帖 |
|
i**********e 发帖数: 1145 | 36 这题作为面试题应该考察的就是基础 DFS 和 coding skills,如果要智能的话在面试那么短的时间内是写不出来的。
这题思路不难的,只是 coding 方面要注意,不要出错就行。基本思路就是先实现 isValid() 函数,检测这个 board 是否valid。然后,找出第一个空格内填 1-9,如果 isValid() 的话继续 DFS,直到没有空格为止(那就是找到答案了)。要注意函数返回之前把空格还原。
这个函数:
private boolean pass(int[][] matrix)
里面用了一些重复的代码,可以 refactor 成更简洁些,也可以减少错误发生的机率:
private boolean passHelper(int [][] matrix, int startRow, int startCol, int
endRow, int endCol)
这样你在 pass 函数里只要传叫 passHelper() 三次即可。
1)startRow = 0, startCol = col, endRow = 8, endCol = col { for col = 0... 阅读全帖 |
|
g**********y 发帖数: 14569 | 37 写了个Java版的:
public boolean matches(String pattern, String str) {
int i = 0;
while (i
charAt(i)!='*') i++;
if (i == pattern.length()) return pattern.equals(str);
char c = pattern.charAt(i-1);
if (pattern.charAt(i) == '?') {
return pattern.substring(0, i-2).equals(str.substring(0, i-2)) &&
(equals(c, str, i-1) && matches(pattern.substring(i+1),
str.substring... 阅读全帖 |
|
y***m 发帖数: 7027 | 38 这个简单java的,有更简洁明了完善高效结构化的么,thx!
class BinaryTree {
private int data;
private int minNode = Integer.MAX_VALUE;
private BinaryTree leftpoiter;
private BinaryTree rightpoiter;
public BinaryTree() {
}
public BinaryTree(int data) {
this.data = data;
leftpoiter = null;
rightpoiter = null;
}
public void getMinNode(int key) {
getMinNode(this, key);
}
public void getMinNode(BinaryTree root, int key) {
if (root != null) {
i... 阅读全帖 |
|
g**********y 发帖数: 14569 | 39 Hotel reservation 简单设计
先看设计的问题核心:
1. limited resources (room)
2. each resource associates with Time
然后构想简单故事:
1. User checks room availability from Day i to Day j
2. System makes reservation on a Room on a Timespan for a User
3. User cancels a reservation
自顶向下设计class:
BookingSystem {
Room[] rooms;
Vector reservations;
Vector users;
Room[] available(TimeSpan t);
int reserve(int userId, TimeSpan t, int roomId);
void cancel(int reservationNumber);
}
Time... 阅读全帖 |
|
s*****y 发帖数: 897 | 40 再弱问一个问题,
careercup 150上面代码如下:22 public double slope;
23 public double intercept;
24 private boolean infinite_slope = false;
25 public Line(GraphPoint p, GraphPoint q) {
26 if (Math.abs(p.x - q.x) > epsilon) { // if x’s are different
27 slope = (p.y - q.y) / (p.x - q.x); // compute slope
28 intercept = p.y - slope * p.x; // y intercept from y=mx+b
29 } else {
30 infinite_slope = true;
31 intercept = p.x; // x-intercept, since slope is infinite
32 }
33 }
34
35 public boolean isEqual(double a, doub... 阅读全帖 |
|
g**********y 发帖数: 14569 | 41 来自主题: JobHunting版 - 贡献面试题 public long find(int N) {
boolean[] a = new boolean[2*N];
for (int i=2; i*i < 2*N; i++) {
int t = i*i;
for (int j=t; j < 2*N; j+=t) {
a[j] = true;
}
}
int count = 0;
for (int i=1; i<2*N; i++) {
if (!a[i]) count++;
if (count == N) return i;
}
return -1;
} |
|
c**********6 发帖数: 105 | 42 面完就发上来了
第一次面大公司啊 好鸡冻 T____T
1. project
2. 上题:
i> 如何用一个方法返回多个值
ii> 如何check一个二叉树的节点的children互为镜像
简单吧
求各种推荐啊 Google, Facebook, Microsoft, Yahoo, Linkedin, Twitter
我答的是
1. i> 新建一个class,封装多个变量;
ii> 利用java参数传递是传递引用,可以直接修改变量值(这点和c++类似),而且同时还可以返回一个值;
iii> 利用java类库中的数据结构,比如说ArrayList。
2. i> recursive算法比较简单
boolean check(TreeNode root) {
//case 1: root == null
if(root == null) return true;
//case 2: left == right == null;
if(root.left == null && root.right == null) r... 阅读全帖 |
|
S**I 发帖数: 15689 | 43 ☆─────────────────────────────────────☆
fengzhongdi (fzd) 于 (Fri May 27 14:30:18 2011, 美东) 提到:
A的onsite,我尽量客观写,大家帮我分析一下
第一个 美国人, 两个string,问第一个是否包含第二个的全部character,扩展是求第一
个string最短的substring包含第二个中所有的character.
我的解法是hashtable, 然后扩展问题是O(n)那个,先找到第一个包含全部的,然后从头
开始删除,接着从尾巴增加那个.面试官表示满意.
OOD是设计card game
交流过程很融洽.
第二个 hiring manage,印度人, 这个人带我去吃饭,然后问了我电梯设计问题,纠缠在
什么时候读楼层,还有如何判断要不要停下来接人.说了快50分钟.我个人感觉还好,反正
他也是都是笑到最后
第三个,白人,一开始问我LRU cache的设计,我直接告诉他double linked list + hash
table,他很吃惊问我是不是准备过,我说是,然后换了一题Bian... 阅读全帖 |
|
K*****k 发帖数: 430 | 44 判断BT是否BST, 给的代码如下。
看不明白,如果传入的是空树也就是curr为null
curr.left不就crash了?
boolean isValidHelper(Node curr, int min, int max) {
if (curr.left != null) {
if (curr.left.value < min ||
!isValidHelper(curr.left, min, curr.value))
return false;
}
if (curr.right != null) {
if (curr.right.value > max ||
!isValidHelper(curr.right, curr.value, max))
return false;
}
return true;
}
相比下,SEwind520 (东南枫)的方法就靠谱的多
boolean isBST(Node ... 阅读全帖 |
|
D********g 发帖数: 650 | 45 第二题,我的code,主要算法是recursion + memoization,有没有人能分析一下这个
算法的复杂度?
static String findLongestDecomposableWord(Set dict) {
String longest = null;
Set mem = new HashSet();
for (String word : dict) {
if (findLongestDecomposableWordInternal(word, mem, dict)) {
if (longest == null || word.length() > longest.length()) {
longest = word;
}
}
}
return longest;
}
... 阅读全帖 |
|
t****s 发帖数: 1 | 46 a Java version for regex match as below, let me know if there is any issue.
boolean isMatch(String s, String p) throws Exception{
if (p==null || p.length()==0)
return s==null || s.length()==0;
return isMatch(s,p,0,0);
}
boolean isMatch(String s, String p, int is, int ip) throws Exception{
if (ip>=p.length())
return s==null || is>=s.length();
if (ip==p.length()-1 || p.charAt(ip+1)!='*'){
if (p.charAt(ip)=='*')
throw new Exception("illegal");
if (is>=s.lengt... 阅读全帖 |
|
v***n 发帖数: 562 | 47 下面那个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-... 阅读全帖 |
|
d********w 发帖数: 363 | 48 需要逻辑缜密阿,
我实现的好像有bug,大家能想到比较好的办法么
static int encode(String str) {
int num = 0;
int sum = 0;
int cur = 0;
boolean vowelBefore = false;
boolean vowel;
str = str.toLowerCase();
for ( int i=0; i< str.length(); i++) {
if (map.get(str.charAt(i)) != null) {
cur = map.get(str.charAt(i));
vowel = true;
} else {
vowel = false;
}
if (str.char... 阅读全帖 |
|
s**********e 发帖数: 326 | 49 贴个我之前写的代码,仿照java里面的源码写的
public static long stringToLong(String str) throws Exception{
if(!chekValid(str)){
throw new Exception("invalid input!!");
}
long limit;
boolean isNegative = false;
int curIndex = 0;
if(str.charAt(0) == '-'){
limit = Long.MIN_VALUE;
isNegative = true;
curIndex = 1;
}else{
limit = -1 * Long.MAX_VALUE;
}
long preLimit = limit/10;
... 阅读全帖 |
|
z******w 发帖数: 36 | 50 用dfs的方法实现了一下,dfs有些改动,需要记录当前栈内已经访问的节点,发现当前点和路
径不符立即返回false,否则递归处理相邻的元素。一旦发现路径走完立即返回true
-----------------------------
import java.util.HashSet;
import java.util.Set;
public class FindPath {
public boolean findPath(String[] m, String path) {
if (path.length() == 0) return true;
for (int i = 0; i < m.length; i ++) {
for (int j = 0; j < m[0].length(); j ++) {
Set set = new HashSet();
if (dfs(m, i, j, path, 0, set))
return true;
}
}
return false;
}
private boolean dfs(String[] m, int ... 阅读全帖 |
|