由买买提看人间百态

topics

全部话题 - 话题: boolean
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
m*****n
发帖数: 204
1

The following is an O(nlgn) solution for string of size n.
It maintains a heap for eligible chars, preferring longer streams.
Ineligible chars are maintained in a candidate queue, and added back
to the working queue when their position constraint is removed.
public class MinDistance {
public String rearrange(String source, int distance) {
// TODO Check params.

// WorkingQueue of eligible streams. Longest stream at head,
// but BackupStream at tail.
PriorityQueue阅读全帖
b****z
发帖数: 176
2
代码如下:
基本思路是,用queue 进行BFS,记录了每个node的parent,找到之后,通过parent来
recover path。感觉没有任何冗余计算啊!!!为什么过不了?
谢谢各位!!
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
public class Solution {
public List> findLadders(String start, String end, Set<
String> dict) {

List> res = new ArrayList>();
Set visited = new HashSet();

... 阅读全帖
b****z
发帖数: 176
3
代码如下:
基本思路是,用queue 进行BFS,记录了每个node的parent,找到之后,通过parent来
recover path。感觉没有任何冗余计算啊!!!为什么过不了?
谢谢各位!!
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
public class Solution {
public List> findLadders(String start, String end, Set<
String> dict) {

List> res = new ArrayList>();
Set visited = new HashSet();

... 阅读全帖
y**********a
发帖数: 824
4
来自主题: JobHunting版 - 热腾腾的 LinkedIn 电面题攒RP

改了一下第一题。
boolean equal(int[][]mx, int i, int j) {
int m=mx.length,n=mx[0].length;
if (i<0||j>=m*n||j<0||i>=m*n) return false;
return mx[i/n][i%n]==mx[j/n][j%n];
}
int binarySearchBound(int[][]mx, int k, boolean lower) {
for (int m=mx.length,n=mx[0].length,l=0,r=m*n-1;l<=r;) {
int mid=l+(r-l)/2;
if (mx[mid/n][mid%n]==k) {
if (lower)
if (!equal(mx, mid, mid-1)) return mid;
e... 阅读全帖
y**********a
发帖数: 824
5
来自主题: JobHunting版 - 热腾腾的 LinkedIn 电面题攒RP

改了一下第一题。
boolean equal(int[][]mx, int i, int j) {
int m=mx.length,n=mx[0].length;
if (i<0||j>=m*n||j<0||i>=m*n) return false;
return mx[i/n][i%n]==mx[j/n][j%n];
}
int binarySearchBound(int[][]mx, int k, boolean lower) {
for (int m=mx.length,n=mx[0].length,l=0,r=m*n-1;l<=r;) {
int mid=l+(r-l)/2;
if (mx[mid/n][mid%n]==k) {
if (lower)
if (!equal(mx, mid, mid-1)) return mid;
e... 阅读全帖
o****s
发帖数: 143
6
来自主题: JobHunting版 - codility一道题

S
按照这个思路写了一个:
public int longestSetSize(int A[], int k){
int count = 0;
int max = 0;
boolean[] visited = new boolean[A.length];
while(count < A.length){
if(k >= A.length || visited[k]){
for(int i=0;i if(!visited[i]){
k = i;
break;
}
}
}
int temp = 0;
while... 阅读全帖
m**********n
发帖数: 97
7
来自主题: JobHunting版 - leetcode Valid Sudoku 就是通不过
Valid Sudoku,不是Sudoku solver,只需要检测三条rules就可以了,这是我的代码
public class Solution {
public boolean isValidSudoku(char[][] board) {

int m = board.length;
int n = board[0].length;
if(m != 9 || n != 9) return false;

char[] cube = new char[9];
// check row rule
for(int i = 0; i < m; i++)
{
cube = board[i];
if(!check(cube)) return false;
}
//check column rule
for(int j = 0; j < n; j++)... 阅读全帖
x*********w
发帖数: 533
8
来自主题: JobHunting版 - 请教一道题:skyline
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class EPI_15_1 {
static class Rect {
public int lft;
public int rgt;
public int height;

public Rect(int l, int r, int h) {
lft = l;
rgt = r;
height = h;
}
}

static class Point {
... 阅读全帖
l********g
发帖数: 372
9
报点最近的面经,都是电面中的,攒点rp顺便求bless!
L家的:
1. Given two (dictionary) words as Strings, determine if they are isomorphic
. Two words are called isomorphic if the letters in one word can be remapped
to get the second word. Remapping a letter means replacing all occurrences
of it with another letter while the ordering of the letters remains
unchanged. No two letters may map to the same letter, but a letter may map
to itself.
Example:
Given "foo", "app"; returns true
we can map 'f' -> 'a' and 'o' -> 'p'
G... 阅读全帖
a**d
发帖数: 85
10
来自主题: JobHunting版 - 问一道g的题
之前面的一个题,现在又重新看了一下。之前有大神说用network的leaky bucket.试着
写了一个简单的,这样合理吗?
interface RateLimit {
/** Sets the rate, from 1 to 1000000 queries per second */
void setQPS(int qps);
/** accept or reject a request, called when request is received */
boolean allowThisRequest();
}
brief example:
server instantiates your object, calls setQPS(1)
at at time t, user1 makes a request, allowThisRequest() returns true
at time t+0.01 sec, user2 makes a request, allowThisRequest() returns false
at at time ... 阅读全帖
p*y
发帖数: 108
11
来自主题: JobHunting版 - 最新L家面经
店面是两个中国人,一开始知道是国人还比较欣喜. 结果证明完全不是这么回事,反而感
觉很严格,最终挂了. 请大家分析下为啥挂? 难道第二题没有按面试官心中理想的答案
在面试时给他写出来? 以后看来一定要注意时间.
1. two sum
一开始根据题目理解以为是排好序的数组, 于是从两头开始找:
boolean twoSum(int[] nums, int sum){
if(nums==null || nums.length<2)
return false;
int low = 0, high = nums.length-1;
while(low if( (nums[low]+nums[high]) == sum ){
return true;
}else if((nums[low]+nums[high]) < sum){
low++;
}else{
... 阅读全帖
c*******e
发帖数: 621
12
来自主题: JobHunting版 - 求问一道multithreading问题
其实volatile bool就是thread safe的,不是吗?
thread safe是说get和set都按你的要求完成,不会出现undefined behavior的情况
比如int,你一个thread去set 1,另一个去set 2,结果int里可能变成3,这叫不
threadsafe
而如果一个volatile bool, 如果一个thread去set 1,另一个去set 0,结果不是0,就
是1
如果一个volatile bool, 如果一个thread去set 1,另一个也去set 1,结果只能是1
我觉得volatile bool就是thread safe,只不过它的threadsafety很难帮助你在不用
lock的情况下使你整个程序thread safe,因为如下的case
volatile boolean b;
void foo() {
if( b ) {
//Here another thread might have already changed the value of b to
false
b = false;
}
}
... 阅读全帖
g**e
发帖数: 6127
13
来自主题: JobHunting版 - 求问一道multithreading问题
也对,哈哈。我只是想写个不用semaphore的,肯定要麻烦点,而且容易写错。这个应
该对了。。。
public class PrintFooBar2 {
public void print(int n) {
AtomicBoolean flag = new AtomicBoolean(true);
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new Worker("foo", flag, n / 2, true));
exec.execute(new Worker("bar", flag, n / 2, false));
exec.shutdown();
}
public static void main(String[] args) {
new PrintFooBar2().print(6);
}
}
class Worker implements Runnabl... 阅读全帖
s******n
发帖数: 226
14
来自主题: JobHunting版 - 昨天G面经里的这一题怎么做?
目前我只想到DFS:
int minAreaBoxes(box[] b)
{
int n=b.length;
if(n==0) return 0;
if(n==1) return b[0].getArea();
Arrays.sort(b,new boxComp());
return dfs(b,new boolean[n],0);
}
int dfs(box[] b, boolean[] visited, int k)
{
if(k==b.length) return 0;
int re = visited[k]?0:b[k].getArea();

int local = 0;
for(int i=k+1;i local +=(visited[i])?0:b[i].getArea();

for(int i... 阅读全帖
y*****e
发帖数: 712
15
大神们看在我夜里1点还在做题的份上,给点思路吧,都是以前mark的帖子的上的题 >_<
1. Convert map > to map > >
这题要干嘛? 毫无思路啊
2. Write a function to get a positive integer n as input and return 0 or 1.
The probability of returning 1 should be 1/(2^n)
这题有点思路,先generate一个fair coin的function, 然后call N次。附上我的丑小
鸭解法,希望抛砖引玉啊
public class probability_N_true{
public static boolean fair(){
Random rand = new Random();
return rand.nextInt(2) == 0;
}

public static boolean probNth(int N){
... 阅读全帖
s******3
发帖数: 61
16
来自主题: JobHunting版 - 请问LC上一道题:Validate BST
原先的OJ很容易通过,
public class Solution {
public boolean isValidBST(TreeNode root) {
return rec(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}

public boolean rec(TreeNode root, int min, int max) {
if (root == null) return true;

if (root.val <= min || root.val >= max) return false;

return rec(root.left, min, root.val) && rec(root.right, root.val,
max);
}
}
但现在OJ加了新的test case,会有
Input: {2147483647}
Output: false
Expected: tr... 阅读全帖
y*****e
发帖数: 712
17
来自主题: JobHunting版 - 请问LC上一道题:Validate BST
这题是我面groupon的店面题,所以印象深刻。。
有两个写法,一个是5楼说的,记录前一个值,比较是不是一直在变大。我和你是一样
的写法,都是比较左右两个叶子。对于max和min两个test case,最简单的还是直接拿
出来判断一下
public boolean validate(TreeNode root){
if(root == null)
return true;
return helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}

private boolean helper(TreeNode root, int low, int high){
if(root == null)
return true;
if(root.val == Integer.MIN_VALUE && root.left != null)
return false;
if(root.val == Integer.MAX_VALUE &... 阅读全帖
h**********c
发帖数: 4120
18
来自主题: JobHunting版 - 请教G家新题 continental divider
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* This is a brain-tease. To find from array elements, monotone (less than
not equal) decreasing from adjacent element, leading to all '0's.
* Connected zeros is one ocean.
* The solution is growing the sea level, when all oceans become one BODY,
the none-basin (none-prohibited) element is the answer.
*/
//DONE Identify water bodies
//DONE Manually design other cases: trivial change
//TODO... 阅读全帖
l******9
发帖数: 579
19
I am working on RESTful Web services design in Visual Studio 2013 on Windows
7.
I need to run the command of Update-Database to create the database in code-
first migrations in a project.
But I get an error:
System.Data.SqlClient.SqlException : Cannot attach the file 'C:MyPathApp
_DataProductReview_newContext-20150216131927.mdf' as database 'ProductReview
_newContext-20150216131927'.
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(
DbConnection owningObject, UInt32 waitForM... 阅读全帖
S***w
发帖数: 1014
20
题目好难
dfs暴力试吧
boolean dfs(String s, String p, Map s_to_p, Map , String> p_to_s) {
if (s.isEmpty() && p.isEmpty()) return true;
if (s.isEmpty() || p.isEmpty()) return false;
for(int i = 2; i <= s.length(); ++i) {
String pre = s.substring(0, i);
if (s_to_p.containsKey(pre)) {
if (s_to_p.get(pre) == p.charAt(0) &&
dfs(s.substring(i), p.substring(1), s_to_p, p_to_s))
return true;
}
else if (p_to_s.containsK... 阅读全帖
S***w
发帖数: 1014
21
题目好难
dfs暴力试吧
boolean dfs(String s, String p, Map s_to_p, Map , String> p_to_s) {
if (s.isEmpty() && p.isEmpty()) return true;
if (s.isEmpty() || p.isEmpty()) return false;
for(int i = 2; i <= s.length(); ++i) {
String pre = s.substring(0, i);
if (s_to_p.containsKey(pre)) {
if (s_to_p.get(pre) == p.charAt(0) &&
dfs(s.substring(i), p.substring(1), s_to_p, p_to_s))
return true;
}
else if (p_to_s.containsK... 阅读全帖
T******7
发帖数: 1419
22
来自主题: JobHunting版 - 出道题。perfectPermutation
import java.util.*;
public class PerfectPermutation {
public int reorder(int[] permutation) {
int result = 0;
int n = permutation.length;
boolean visit[] = new boolean[n];
for (int i = 0; i < n; ++ i) {
if (!visit[i]) {
int j = i;
do {
j = permutation[j];
System.out.println(j);
visit[j] = true;
} while (j != i);
for(int ii = 0... 阅读全帖
y*****e
发帖数: 712
23
在一亩三分地上看到的,听说挂了很多人。咱板上牛人多,发上来和大家讨论一下呀
class IntFileIterator {
boolean hasNext();
int next();
}
class{
public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b);
}
// return if the distance between a and b is at most 1..
// Distance: minimum number of modifications to make a=b
// Modification:
// 1. change an int in a
// 2. insert an int to a
// 3. remove an int from a
这题就是one edit distance的变形题,难点在于给的Iterator,事先不知道两个file
的长度,也不允许用extra space(所以不能转成两个string再比),那么一个个往前
跑的... 阅读全帖
s******s
发帖数: 2721
24
Code snippets:
boolean isDistanceZero(IntFileIterator a, IntFileIterator b) {
while (a.hasNext() && b.hasNext()) {
if (a.next() != b.next()) {
return false;
}
}
return a.hasNext() == b.hasNext();
}
boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b) {
while (a.hasNext() && b.hasNext()) {
IntFileIterator curA = a;
IntFileIterator curB = b;
if (a.next() != b.next()) {
return isDistanceZero(a, curB) || isDistanceZero(curA, b)... 阅读全帖
y*****e
发帖数: 712
25
他的意思是一旦碰到两个不同的,就把可能的三种情况,ins_a (a has extra letter),
ins_b(b has extra letter), replace都turn on, diff表示至少有一个不同的,这
时也turn on. 如果一直跑到最后所有的letter都相同,diff就remain as false, 也
return false.
一旦碰到两个不一样的,就要一直比较currA, currB, preA, preB, 如果preA !=
currB, 但insertionB是true, 说明insert extra letter to B是不可能的,就turn
off ins_B.同理其他的判断。
跑到最后总结,看A还是B还剩一个letter, 或者两者都跑到头了,再做判断。
我把这个超级genius的答案改写成java的了。。。。
public class FileCompare {
public boolean isDistanceZeroOrOne(IntFileIterator a,
IntFileIterator b) {
... 阅读全帖
S*******C
发帖数: 822
26
下面答案能过所有test case,如果谁发现有错请告诉我
public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b)
{
boolean ins_a = false, ins_b = false, replace = false, diff = false;
int pre_a = 0, pre_b = 0;
while (a.hasNext() && b.hasNext()) {
int cur_a = a.next(), cur_b = b.next();
if (!ins_a && !ins_b && !replace) {
if (cur_a != cur_b) {
ins_a = ins_b = replace = diff = true;
}
} else {... 阅读全帖
l*****n
发帖数: 246
27
来自主题: JobHunting版 - G 店面
没什么思路,想这样做,不知道会不会被人鄙视:
public int[][] generateRandomBoard(int NColors, int N, int M) {
int[][] board = new int[N][M];
boolean isEnd = false;
while(!isEnd){
generateRandomBoard(board, NColors);
isEnd = checkBoard(board);
}
return board;
}
private void generateRandomBoard(int[][] board, int NColors) {
Random rm = new Random();
int N = board.length;
int M = board[0].length;
for(int i=0; i for(int j=0; j int te... 阅读全帖
y*****e
发帖数: 712
28
L家最爱考的面试题之一就是nested integer了,
还爱考各种iterator的implementation
这题是把两个最爱合在一起了。。。。感觉很有可能出,但网上没找到满意的答案.
题目是这样的
eg: {{1,2},3,{4,{5,6}}}
不断调用iterator的next()返回的序列是 1 2 3 4 5 6
这个data structure的interface是这样的
public interface Data {
// Does this Data hold a collection?
public boolean isCollection();
// Returns the collection contained by this Data, or null if it is a
single element
public Collection> getCollection();
// Returns the single element contained by this Data, or nul... 阅读全帖
y*****e
发帖数: 712
29
来自主题: JobHunting版 - 请教L家新面经题,种花
题目从一亩看来的:
public boolean canPlaceFlowers(List flowerbed, int numberToPlace)
如果flowerbed当中为true,说明已经栽过花了,附近两个不能再栽花。numberToPlace
代表想再栽多少花到flowerbed里。让return是不是还能栽那么多谢花进去。.
这个应该是说如果flowerbed是true false false false false, 那么在Index = 3种一朵
这题是不是用greedy解就行啊?看到一个可以种花的spot就种, 然后decrement
numberToPlace, 如果到最后numberToPlace > 0, 那就不能种。。。
还是需要用DP啊?来一个dp(startPos, flowerLeft), 每一步选择种或者不种?
Y**G
发帖数: 1089
30
来自主题: JobHunting版 - 继续发个snapchat面经题
react.js
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object other) {
return (other instance Point) && ((Point)oher).x == this.x && ((
Point)oher).y == this.y;
}
}
boolean isValid(Point p) {
return (p.x >= 0 && p.x < 8 && p.y >= 0 && p.y < 8);
}
Point delta(Point p, int deltaX, int deltaY) {
new Point(p.x + deltaX, p.y + deltaY);
}
f(p1, p2, k) {
if (k == 0) {
return p1... 阅读全帖
S*******C
发帖数: 822
31
这题用递归即使只传下标也不能通过长String的oj
有没有什么改进方法
我这种写法应该是O(N) time, O(N) space的吧
Runtime Error Message: Line 12: java.lang.StackOverflowError
public boolean isPalindrome(String s) {
if (s == null || s.length() < 2)
return true;
if(!rec(s, 0, s.length() - 1))
return false;
return true;
}
private boolean rec(String s, int start, int end){
if(start >= end)
return true;
if (!Character.isLetterOrDigit(s.charAt(start)))
... 阅读全帖
S*******C
发帖数: 822
32
这个答案是对的,也能通过OJ
但改成JAVA版就是不能过大的test case
public boolean isPalindrome(String s) {
if (s == null || s.length() < 2)
return true;
return rec(s, 0, s.length() - 1);
}
private boolean rec(String s, int start, int end){
if(start >= end)
return true;
while (start < end && !Character.isLetterOrDigit(s.charAt(start)))
start++;
while (start < end && !Character.isLetterOrDigit(s.charAt(end)))
end--;
r... 阅读全帖
M**********g
发帖数: 59
33
其实再store的方法加个synchronized 就可以了,
每当有线程调用store方法的时候,这个对象就上锁了,其他线程就不能访问了。
下面是原题,和我写的实现
public interface TwoSum {
/**
* Stores @param input in an internal data structure.
*/
void store(int input);

/**
* Returns true if there is any pair of numbers in the internal data
structure which
* have sum @param val, and false otherwise.
* For example, if the numbers 1, -2, 3, and 6 had been stored,
* the method should return true for 4, -1, and 9, but false for 10, ... 阅读全帖
y*****e
发帖数: 712
34
今天面的第二轮。。。面完很伤心很失望,下午上了一下午班后,感觉好了点,开始觉
得自己发挥的好差,题也不容易,为啥别人都能碰到常见的常规的题,我就碰不到。。
。。不够难过的时候怪运气是太容易的事了,但现在冷静下来感觉,不过是给自己找借
口罢了。发面经上来,给自己差劲的人品增值,希望将来的面试顺利。
第一轮两道题
1. first missing positive
2. 写一个file line iterator
Implement a (Java) Iterable object that iterates lines one by one from a
text file..
/** A reference to a file. */
public class TextFile implements Iterable. From 1point 3acres bbs
{
public TextFile(String fileName) { // please implement this
/** Begin reading the file, line by li... 阅读全帖
g******n
发帖数: 10
35
Sample Input
1 2 3
2 1 3
3 2 1 5 4 6
1 3 4 2
3 4 5 1 2
Sample Output
YES
YES
YES
NO
NO
--------------------------------------
Python 版:
import sys
def check(nodes):
return helper(nodes, 0, len(nodes), -sys.maxsize-1, sys.maxsize)
def helper(nodes, start, end, min_value, max_value):
if start >= end:
return True
root = nodes[start]
if not (min_value < root < max_value):
return False
i = start + 1
while i < end and node... 阅读全帖
b*****p
发帖数: 9649
36
来自主题: JobHunting版 - FB Onsite新题,有人能看看吗?
我的伪码
假设有N个房间,strategy[0..k]
首先initialize一个N+2的数组[0..N+1],其中0和N+1是dummy
boolean eval(int N, int[] strategy)
{
//assert N>1
ArrayList room = new ArrayList();
//room[0..N+1]都为initialized为1

for (i=0;i if (finished(room)){
return true;
}
room[strategy[i]] = 0; //执行strategy
expand(room); //根据题意来确定下一状态
}
if (finished(room)){
return true;
}
return false;
}
boolean finish(ArrayList room){
if room[1..room.size()-2] 都为 ... 阅读全帖
b**********5
发帖数: 7881
37
A Blob is a shape in two-dimensional integer coordinate space where all
cells have at least one adjoining cell to the right, left, top, or bottom
that is also occupied. Given a 10x10 array of boolean values that represents
a Blob uniformly selected at random from the set of all possible Blobs that
could occupy that array, write a program that will determine the Blob
boundaries.
Optimize first for finding the correct result, second for performing a
minimum number of cell
Boolean value reads, and ... 阅读全帖
x*****0
发帖数: 452
38
Assume in a system, users will register a callback function with system and
that callback function will be called when an event is triggered
but if the event is already triggered, the user will synchronously call
the callback function
For example
suppose user i registers cb i at time Ti,
and the event happens at time Te
and assume that T1 < T2 < T3 <...< Tm < Te < Tm+1 < ... < Tn
at time Te, cb1..cbm will be called
cbm+1 .. cbn will be called synchronously
impleme... 阅读全帖
s*******h
发帖数: 105
39
来自主题: JobHunting版 - FB 面经
一个月以前面的了,没什么营养,还是发给大家看看吧, 希望对大家有帮助,已经跪了。
电面: 中国大叔面的,大叔很nice,遇到我写有bug的时候都会着急的提醒我,题也很
简单。
1: 给n个点找出离远点最近的k个, k< 2: 给三个 api isSmall() isMid() isBig() 给一个array 排序,只要不被迷惑, 知
道其实是lc 上 sort color的变种就很简单了。
On Site:
1: 聊自己的research,白人manager,说自己以前是faculty,人非常nice。气场也比
较合, 我讲完之后还说把email给我,说我面试后有问题可以问他,然后问了个 two
sum。
2:亚洲小哥,也很nice,第一道题是 Lc 上的String Multiplication。 然后出了一
个打印 tree路径的题,后来问我做过没有,只能说做过类似的,后来换了一道 的
decode way 变形,要把所有的可能的组合都打印出来,写了一个recursion。
中午和内推我的本版汤唯姐姐吃饭,在此谢谢汤唯姐姐,大牛非常nice,大家内推可以
去找他。吃饭的时... 阅读全帖
w*****h
发帖数: 423
40
有人不愿意进去看的话,我贴这
private static List findMaxIsLand(int[][] m)
{
List poslist = new ArrayList();
if(m.length == 0 || m[0].length == 0)return poslist;

int x = m.length, y = m[0].length;
boolean[][] visited = new boolean[x][y];

for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
if(m[i][j] == 0 || visited[i][j])continue;

Stack stk = new Stack阅读全帖
k****r
发帖数: 807
41
不是很懂多线程,请教我这样的设计可以满足面试官对于deposit和withdraw的提问吗?
哪里可以找到代码学习学习,谢谢。
class bank2 {
private double balance;
private int acountNum;
private Object lock = new Object();
public bank2(int a, double b) {
acountNum = a;
balance = b;
}

public boolean withdraw(double w) {
synchronized(lock) {
if (balance < w) return false;
balance -= w;
return true;
}
}
public boolean deposit(double d) {
synchronize... 阅读全帖
e********3
发帖数: 229
42
来自主题: JobHunting版 - fb电面面经
抛个砖.有错哪里可以优化请吃出.
public class IntegerToEnglishWord {
public static void main(String[] args) {
IntegerToEnglishWord itew = new IntegerToEnglishWord();
System.out.println(itew.integerToEnglishWord(123456789));
}
public String integerToEnglishWord(long num) {
String res = "";
if (num == 0) {
return "zero";
}
boolean neg = false;
if (num < 0) {
num = -num;
neg = true;
}
Map阅读全帖
k****r
发帖数: 807
43
public static Point minDistanceInMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int res = Integer.MAX_VALUE;
int numOfOne = countOne(matrix);
Point minP = new Point(-1,-1,-1);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
int newRes = goHelper(matrix, i, j, res, numOfOne);
if ( newRes < res) {
... 阅读全帖
d********i
发帖数: 582
44
来自主题: JobHunting版 - 求教Valid Sudoku
看到一个答案,可以用一个for loop来检测sudoku 是否valid。
但是dan实在看不懂这句话: board[row / 3 * 3 + i / 3][col / 3 * 3 + i % 3] ==
num
不知道为什么可以check 3 * 3 blocks, 一会除一会乘一会求余,把我看晕。有能帮
忙解释下么?
public static boolean isValidSudoku(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] != '.') {
if (!isValid(board, board[i][j], i, j)) {
return false;
}
... 阅读全帖
s*******n
发帖数: 305
45
来自主题: JobHunting版 - 再来问道面经题
public int[] recovery(int[] B){
int len = B.length;
int[] A = new int[len];
boolean[] flag = new boolean[len];

for(int i=0; i int biggerLeft = B[i];
for(int j=0; j if(flag[j]==true) continue;
if(biggerLeft==0){
flag[j]=true;
A[i]=j+1;
break;
} else {
biggerLeft--;
}
}
}
return A;
}
s**o
发帖数: 30
46
看似很简单的一个函数,查看一个BST有没有loop,我的代码是,没有那一行“remove
”的代码就会错,我现在还没想明白。。。。
public static boolean cycle(TreeNode root, HashSet visited) {
if (root == null) {
return true;
}
if (visited.contains(root.c)) {
return false;
}
visited.add(root.c);
boolean res = cycle(root.left, visited)&& cycle(root.right, visited);
//没有这一行就会错,为啥??????????
visited.remove(root.c);

return res;
}
n*******e
发帖数: 37
47
来自主题: JobHunting版 - 求問一題G家面經
输入为List,string的格式类似于: (peer,Bob,David), (peer,David,
Christina),(manager,Tina,Bob) 表示Bob和David是同事,David和Christina是同事,
Tina是Bob的manager
让你设计一种数据结构储存这些关系,并且implement以下function:
boolean is_peer(某个员工,某个员工)
boolean is_manager(某个员工,某个员工)
peer的意思是他们有共同的direct manager才算peer,如果他们的manager的manager相
同,不算peer
is_manager返回时,不需要是直接的manager,例如假设Jessica 是 Eric的manager的
manager的manager,这种情况也返回true.
这题是版上某位大牛的面试题, 我想了很久不确定怎麽做最好, 请各位大牛开释

发帖数: 1
48
来自主题: JobHunting版 - 求問一題G家面經
这题是我发的嘛,但我不是大牛,我只是一个还没工作的学生。。。
面挂了以后我本来是已经感累不爱了不想参加任何google的题的讨论了,看你这么执着
发了3个地方的问,我还是说一下我那会的做法
加油!你会拿下google的!
这个题不难,就是有一点点绕
我最直接的想法是并查集,很容易返回is_manager。 但是并查集无法给出is_peer
所以设计一个
class Employee{
List peer;
Employee manager;
Employee(){
peer = new ArrayList();
manager = null;
}
}
总共实现三五个函数
boolean is_manager(employee a, employee b){
while(b != null){
if(a == b.manager) return true;
b = b.manager;
}
return false;
... 阅读全帖
m******0
发帖数: 222
49
来自主题: JobHunting版 - How do you OO design a chicken
suppose in the context of a shooter game:
class Matter {
public String matterType; // "meat", "wood", "stone", "shell"
public String texture; // "solid color", "strip", transparent"
public int length, width, weight;
public void Move(Direction direct) {
}
}
class CreaturePart implements Matter {
public boolean canMove, canOffBody;
public CreaturePart connectsTo; // e.g. Wing connect to body
public offBody() {
Move(new Direction(random));
connectsTo = null;
}
}
class Egg ... 阅读全帖
V***a
发帖数: 206
50
来自主题: JobHunting版 - 面试题,min steps to reduce n to 1
变形版本是hackerrank原题Down To Zero II
https://www.hackerrank.com/challenges/down-to-zero-ii
从归类看是bfs做的,下面这个bfs做法pass了。
static Map> route = new HashMap
>();
static Map cache = new HashMap();
public int downZero(int K) {
if (K == 0)
return 0;
if (cache.containsKey(K))
return cache.get(K);
Queue q = new LinkedList();
boolean[] marked = new boolean[K+1];
i... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)