l**b 发帖数: 457 | 1 二爷不用DP,奇怪啊!第3题用Trie的话memory吃不消吧?用DP是不是好点?
贴个简单的代码,helper是recursive的,helper2是DP的,2爷帮忙看看,是不是又出
错了:
public boolean isSubSequence(String s, String t) {
assert(s != null && t != null);
if (t.length() == 0) return true;
//return helper(s, t, 0, 0);
return helper2(s, t);
}
private boolean helper(String s, String t, int si, int ti) {
if (ti == t.length()) return true;
if (si >= s.length()) return false;
if (s.charAt(si) == t... 阅读全帖 |
|
l**b 发帖数: 457 | 2 然后想了一下,DP的空间好像可以优化到O(|Si|):
private boolean helper3(String s, String t) {
boolean[] dp = new boolean[s.length() + 1];
for (int i = 0; i <= s.length(); i++) {
dp[i] = true;
}
for (int i = 1; i <= t.length(); i++) {
boolean[] newDp = new boolean[s.length() + 1];
for (int j = 1; j <= s.length(); j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) {
newDp[j] = dp[j - 1];
} else {
... 阅读全帖 |
|
l**b 发帖数: 457 | 3 二爷不用DP,奇怪啊!第3题用Trie的话memory吃不消吧?用DP是不是好点?
贴个简单的代码,helper是recursive的,helper2是DP的,2爷帮忙看看,是不是又出
错了:
public boolean isSubSequence(String s, String t) {
assert(s != null && t != null);
if (t.length() == 0) return true;
//return helper(s, t, 0, 0);
return helper2(s, t);
}
private boolean helper(String s, String t, int si, int ti) {
if (ti == t.length()) return true;
if (si >= s.length()) return false;
if (s.charAt(si) == t... 阅读全帖 |
|
l**b 发帖数: 457 | 4 然后想了一下,DP的空间好像可以优化到O(|Si|):
private boolean helper3(String s, String t) {
boolean[] dp = new boolean[s.length() + 1];
for (int i = 0; i <= s.length(); i++) {
dp[i] = true;
}
for (int i = 1; i <= t.length(); i++) {
boolean[] newDp = new boolean[s.length() + 1];
for (int j = 1; j <= s.length(); j++) {
if (t.charAt(i - 1) == s.charAt(j - 1)) {
newDp[j] = dp[j - 1];
} else {
... 阅读全帖 |
|
c******a 发帖数: 789 | 5 大小测试都过了
public boolean exist(char[][] board, String word) {
if (board==null || board.length==0 || board[0].length==0) return
false;
for (int i=0; i
for (int j=0; j
if (board[i][j]==word.charAt(0)) {
boolean[][] used = new boolean[board.length][board[0].
length];
used[i][j] = true;
if (walk(board, i, j, word, 1, used)) return true;
... 阅读全帖 |
|
w******j 发帖数: 185 | 6 来个java的吧,带parent pointer和不带的,preorder和inorder的
import java.util.NoSuchElementException;
import java.util.Stack;
public class BST {
private Node root;
private static class Node {
int key;
Node left, right, parent;
Node(int key) {
this.key = key;
}
}
public BST(){
}
public BSTIterator inorderIterator() {
return new InorderIterator();
}
public BSTIterator preorderIterator() {
return new PreorderI... 阅读全帖 |
|
z*******3 发帖数: 13709 | 7 五个步骤
0)预处理,把输入的string给trim一下,这个适用于任何string作为输入的问题
1)写出is unsigned integer的方法
2)写出is integer的方法,借用步骤1写好的方法,加一个判断是不是+-号开头
3)写出is unsigned double的方法,借用步骤1写好的方法
这里有一个很特殊的corner case,就是.单独出现的情况,其实整个流程就这一个
corner case
4)写出is double的方法,借用步骤3写好的方法
5)然后valid number根据e做切割
e后面必需是integer,用步骤2的方法
e前面必需是double,用步骤4的方法
搞定
看起来很长,其实没啥东西,甚至没有脑筋急转弯的地方,就一个corner case记住就
好了
如果可以自由选择java类库的话,double.parseDouble(String)和integer.parseInt(
String)
加上try catch exception block,简简单单搞定一大串内容
废话说完咯,以下是代码
public class Solution ... 阅读全帖 |
|
w******j 发帖数: 185 | 8 来个java的吧,带parent pointer和不带的,preorder和inorder的
import java.util.NoSuchElementException;
import java.util.Stack;
public class BST {
private Node root;
private static class Node {
int key;
Node left, right, parent;
Node(int key) {
this.key = key;
}
}
public BST(){
}
public BSTIterator inorderIterator() {
return new InorderIterator();
}
public BSTIterator preorderIterator() {
return new PreorderI... 阅读全帖 |
|
m********l 发帖数: 791 | 9 了解这题DFS的话代码简洁而且大测试不超时。
就是想拿这题练习一下BFS的解法,自己吭哧吭哧写的代码超时了,不知道代码中的哪
一步太耗时?大家帮忙看一下,谢谢~
或者其他可以改进的地方大家也不妨指出~
代码如下:
public class Solution {
public static Queue queue = new LinkedList();
public boolean exist(char[][] board, String word) {
if (word.equals("") || word == null)
return true;
if (board == null || board.length == 0)
return false;
int row = board.length;
int col = board[0].length;
String tmp = word; // save c... 阅读全帖 | | |
|
c*********a 发帖数: 8 | 10 Code for G question:
public class Solution {
public static class Pair implements Comparable{
int val;
int count;
public Pair(int val, int count) {
this.val = val;
this.count = count;
}
@Override
public int hashCode() {
return val + count * 3;
}
@Override
public boolean equals(Object obj) {
if ( obj == null ) { return false;}
if ( obj.getClass() != this.get... 阅读全帖 |
|
y*****e 发帖数: 712 | 11 public class BQueue {
private Queue q = new LinkedList();
private int limit;
public BQueue(int limit) {
this.limit = limit;
}
public synchronized void put (T t) throws InterruptedException {
while (isFull()) {
wait();
}
boolean e = isEmpty();
q.add(t);
if (e)
notifyAll();
}
public synchronized T get () throws InterruptedException {
while (isEmpty()) {
wait();
}... 阅读全帖 |
|
p****e 发帖数: 4 | 12 My solution:
class FileCompare {
public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b) {
while (a.hasNext() && b.hasNext()) {
int curA = a.next();
int curB = b.next();
if (curA != curB) {
int prevA = curA;
int prevB = curB;
if (a.hasNext() && b.hasNext()) {
curA = a.next();
curB = b.next();
if (curA != prevB && curB != prevA) {
// This means neither prevA nor prevB is the extra item added... 阅读全帖 |
|
j********l 发帖数: 325 | 13 enum AttackResponse {
HIT, MISS, SUNK, END
}
class Ship {
private List parts;
GameBoard board;
void addPart(Coordination coor) {
parts.add(coor);
}
void removePart(Coordination coor) {
parts.remove(coor);
}
boolean hasSunk() {
return parts == null || parts.size() == 0;
}
}
class Coordination {
private int x;
private int y;
}
class Playe... 阅读全帖 |
|
发帖数: 1 | 14 简化版只输出一个答案可以用counter做。
如果是301原题输出所有解那就要dfs了,dfs比bfs省空间,并且不需要set来去重,看
起来更elegant。DFS之前要先count一下需要删除多少个。时间复杂度是O(N^k), k是需
要删除的括号数。当然是需要删除的越多,解就越多,复杂度就越高。上code:
class Solution {
public List removeInvalidParentheses(String s) {
int leftRemove = 0;
int rightRemove = 0;
int open = 0;
for (int i = 0; i < s.length(); ++i) {
char cur = s.charAt(i);
if (cur == '(') {
open++;
} else if (cur == ')') {
... 阅读全帖 |
|
w***n 发帖数: 4358 | 15 ☆─────────────────────────────────────☆
Boolean (Soso) 于 (Wed Sep 8 06:11:47 2010, 美东) 提到:
湖北 要签证的话,是去北京还是去上海?
我觉得是去北京
☆─────────────────────────────────────☆
zoun (zoun) 于 (Wed Sep 8 10:30:26 2010, 美东) 提到:
本来说不分区了,可是前段时间说上海人多,不让去。
☆─────────────────────────────────────☆
yk7209 (gene) 于 (Wed Sep 8 10:47:58 2010, 美东) 提到:
北京。
☆─────────────────────────────────────☆
Boolean (Soso) 于 (Wed Sep 8 11:06:17 2010, 美东) 提到:
那 按规定 是上海 ?
☆─────────────────────────────────────☆
Boo... 阅读全帖 |
|
w***n 发帖数: 4358 | 16 ☆─────────────────────────────────────☆
wowowowo (life is a journey) 于 (Tue Mar 29 00:16:01 2011, 美东) 提到:
你说标题党能不能上十大你说标题党能不能上十大你说标题党能不能上十大你说标题党
能不能上十大你说标题党能不能上十大你说标题党能不能上十大你说标题党能不能上十
大你说标题党能不能上十大你说标题党能不能上十大你说标题党能不能上十大你说标题
党能不能上十大你说标题党能不能上十大你说标题党能不能上十大
还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力还有暴力
☆─────────────────────────────────────☆
yellowchild (yellowchild) 于 (Tue Mar 29 00:21:50 2011, 美东) 提到:
标题党和十大什么区... 阅读全帖 |
|
a****l 发帖数: 8211 | 17 it seems you only have two threads, one main, one background. Then it's very
simple, no need for "advanced" things like mutex. Just add one global
boolean, when background has a new value and boolean is 0, write the value,
set boolean to 1; the main thread keeps checking boolean, if 1, read value,
finish work, then set boolean to 0.
gdata.
main
main(
) |
|
t**r 发帖数: 3428 | 18 /**
* Returns whether all bounded integers within `s` satisfy `p`.
*/
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (contains(s, a) && !p(a)) false
else iter(a + 1)
}
iter(-bound)
}
/**
* Returns whether there exists a bounded integer within `s`
* that satisfies `p`.
*/
def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, (x => !p(x)))
/**
* Returns a set transformed ... 阅读全帖 |
|
|
P********l 发帖数: 452 | 20 Fixed the issue ihasleetcode mentioned. Thanks.
Added more test cases like:
string = "ho"
pattern = "**ho", "ho**"
string = "a", pattern = "aa"
string = "aa", pattern = "a"
Code:
/**
* check if patt matches the string str. Same as {@link match} but
* one-dimension array is used.
*
* @param str
* the string to match
* @param patt
* the pattern matching str. '*' is for 0 or more characters
and
* '.' is for exactly one cha... 阅读全帖 |
|
g**********y 发帖数: 14569 | 21 写了个简化版的:
public class Gloomyturkey implements IMilestone {
private int N;
private int M;
private int[] sequence;
private int[] answer;
private HashMap map;
private boolean[] used;
private int[] sum;
private boolean found;
public int[] findMilestones(int[] segs) {
M = segs.length;
sum = segs;
calcN();
initMap(segs);
used = new boolean[M];
found = false;
sequence = new int[N];
answer = new int[N];
assign(0, M-1);
assign(1, M-2);
dfs(2);
return ... 阅读全帖 |
|
g**********y 发帖数: 14569 | 22 写了个简化版的:
public class Gloomyturkey implements IMilestone {
private int N;
private int M;
private int[] sequence;
private int[] answer;
private HashMap map;
private boolean[] used;
private int[] sum;
private boolean found;
public int[] findMilestones(int[] segs) {
M = segs.length;
sum = segs;
calcN();
initMap(segs);
used = new boolean[M];
found = false;
sequence = new int[N];
answer = new int[N];
assign(0, M-1);
assign(1, M-2);
dfs(2);
return ... 阅读全帖 |
|
l*****n 发帖数: 577 | 23 来自主题: JobHunting版 - 面经(L) 说实话,LinkedIn 是我面试过的相当拖拉的公司。看到不少人因电话面试3,4天就感觉
没戏了,我都忍不住地想,太着急了。以下是我的经历,相当拖拉。我写得也挺拖拉,
但希望对面试他家的朋友有所帮助。
1.初接触
在去年6月,我通过版上的一朋友推荐,到现在大概8个月了,才算结束。当然这其中也
有我的因素。
我给那朋友发了email包括我的简历,他并没有回,所以我一直不知道是有人推荐,也不
知道推荐人名字。直到后来recuiter问(可能是为了confirm reference bonus),才想
起来查以前发出的email。
recuiter到7月中旬才联系我。我还以为他在网上找到的我呢。我只在linkedin放了简
单的简历,也没有对外公开。记得但是对被发现感到比较惊奇。但是,我看了一下job
description,不很match.我就把面试推了。但是表示了对公司的强烈兴趣,并同意看看
他家其他的工作,有合适的联系他。
到十月中旬,recruiter又联系我。但是,我此时在中国,正好度一个一个月的长假。
只好在十一月再面试。面试SET position.
2.初次phone(rec... 阅读全帖 |
|
p*****2 发帖数: 21240 | 24
这是我的测试程序。你测的什么数据?我试了几个P都还行。
import java.util.*;
public class test {
static boolean Prob()
{
Random rand=new Random();
return rand.nextBoolean();
}
static boolean Prob2(double p, boolean expected)
{
if(p<0.5)
{
expected=!expected;
p=1-p;
}
if(Prob()==expected)
return expected;
else
{
return Prob2((p-0.5)/0.5, expected);
}
}
public static void main(String[] args)
{
int count=0;
... 阅读全帖 |
|
D********g 发帖数: 650 | 25 prob那题,我的code:
static boolean prob() {
Random r = new Random();
if (r.nextDouble() < 0.5) {
return true;
}
return false;
}
static boolean prob(double p) {
double EPS = 1e-8;
if (p >= 1.0) {
return true;
}
if (p <= 0.0) {
return false;
}
int bitPos = 0;
while (p > EPS) {
bitPos ++;
p = p * 2; ... 阅读全帖 |
|
p*****2 发帖数: 21240 | 26
这是我的测试程序。你测的什么数据?我试了几个P都还行。
import java.util.*;
public class test {
static boolean Prob()
{
Random rand=new Random();
return rand.nextBoolean();
}
static boolean Prob2(double p, boolean expected)
{
if(p<0.5)
{
expected=!expected;
p=1-p;
}
if(Prob()==expected)
return expected;
else
{
return Prob2((p-0.5)/0.5, expected);
}
}
public static void main(String[] args)
{
int count=0;
... 阅读全帖 |
|
D********g 发帖数: 650 | 27 prob那题,我的code:
static boolean prob() {
Random r = new Random();
if (r.nextDouble() < 0.5) {
return true;
}
return false;
}
static boolean prob(double p) {
double EPS = 1e-8;
if (p >= 1.0) {
return true;
}
if (p <= 0.0) {
return false;
}
int bitPos = 0;
while (p > EPS) {
bitPos ++;
p = p * 2; ... 阅读全帖 |
|
p*****2 发帖数: 21240 | 28
);
哈哈。我写了一个。跟你几乎一模一样。
HashMap hm = new HashMap();
public boolean isScramble(String s1, String s2)
{
if (s1 == null || s2 == null)
return false;
if (s1.length() != s2.length())
return false;
if (s1.equals(s2))
return true;
String key1 = s1 + ":" + s2;
String key2 = s2 + ":" + s1;
if (hm.containsKey(key1))
return hm.get(key1);
if (hm.containsKey(key... 阅读全帖 |
|
p*****2 发帖数: 21240 | 29
);
哈哈。我写了一个。跟你几乎一模一样。
HashMap hm = new HashMap();
public boolean isScramble(String s1, String s2)
{
if (s1 == null || s2 == null)
return false;
if (s1.length() != s2.length())
return false;
if (s1.equals(s2))
return true;
String key1 = s1 + ":" + s2;
String key2 = s2 + ":" + s1;
if (hm.containsKey(key1))
return hm.get(key1);
if (hm.containsKey(key... 阅读全帖 |
|
z******e 发帖数: 82 | 30 new test cases:
--------------------
char str[] = "\\"; // test \ inside "
char str[] = "\\";
char str[] = '\"'; // test \ inside "
char str[] = '\"';
code:
---------------------------
private static String uncomment(String str) {
boolean slash2 = false;
boolean inStr = false;
boolean slashstar = false;
boolean escape = false;
StringBuilder sb = new StringBuilder();
char lastc = ' ';
char c = ' ';
int deleteStart = -1;
... 阅读全帖 |
|
f*******t 发帖数: 7549 | 31 问过1337哥,他说最大数据是32768*32768,肯定会MLE。
我有个优化的版本,但最后一个test case还是TLE。十分想看谁有能AC的代码!
public boolean isMatch(String s, String p) {
if (s == null || p == null)
return false;
if (s.isEmpty() && p.isEmpty()) { //Both are empty string -> true
return true;
} else if (s.isEmpty()) { // If s is empty, p must not contain
any character other than '*'
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) != '*')
... 阅读全帖 |
|
n*p 发帖数: 298 | 32 in order traversal
要用一个变量来存前一个Node的值。但发现有时候值没有被更新,结果是错的,是因为
Java的passing by value的原因吗?代码如下:
public boolean isValidBST(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if (root == null) return true;
//if (root.left == null && root.right == null) return true;
Integer preVal = Integer.MIN_VALUE;
return inOrder (root, preVal);
}
public boolean inOrder(TreeNode root, Integer preVal... 阅读全帖 |
|
r****m 发帖数: 70 | 33 boolean validate(String expression){
String expr = expression.trim();
boolean expectNum = true;
int numOfParentheses = 0;
int i = 0;
while(i < expr.length()){
if(expr.charAt(i) == '('){
if(!expectNum) return false;
numOfParentheses++;
i++;
} else if (expr.charAt(i) == ')'){
if(expectNum || numOfParentheses < 1) return false;
numOfParentheses--;
... 阅读全帖 |
|
r****m 发帖数: 70 | 34 boolean validate(String expression){
String expr = expression.trim();
boolean expectNum = true;
int numOfParentheses = 0;
int i = 0;
while(i < expr.length()){
if(expr.charAt(i) == '('){
if(!expectNum) return false;
numOfParentheses++;
i++;
} else if (expr.charAt(i) == ')'){
if(expectNum || numOfParentheses < 1) return false;
numOfParentheses--;
... 阅读全帖 |
|
c********r 发帖数: 286 | 35 M现在问题还问的挺新。。
那个六边形的是这个意思不?
public class Unit implements IUnit{
private final int MAX_NEIGHBOR = 6;
private final int MAX_RESOURCE = 10;
private int nNeighbor = 0;
private int nResource = 0;
Unit[] neighbors;
Unit[] resources;
public Unit(){
neighbors = new Unit[MAX_NEIGHBOR];
resources = new Unit[MAX_RESOURCE];
}
@Override
public boolean findRecource(){
if(neighbors == null || nNeighbor == MAX_NEIGHBOR){
return false;
}
boolean added = false;
for(Unit u: neighbors){
if(nNeighbor <= MAX_NEIGHB... 阅读全帖 |
|
l**b 发帖数: 457 | 36 来自主题: JobHunting版 - A家面试题 我当时和你一样,也讨论了100!的方法,说不行,太慢了。还是从string那里入手比
较好。
这个是我当时写的。DP那个方法刚刚写的,实在是很弱,所以如果错了,请轻拍。其他
的基本应该和面试的时候一样。反正写完他就让我把原来for loop是用string的length
做end point的,改成了用symbol最长的length。然后就说没问题了。
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
public class SymbolWord {
// Here is the max length of any symbol
public static final int MAX_SYMBOL_LENGTH = 2;
public String findLongestSymbolWord(List dict, Set
s... 阅读全帖 |
|
e****e 发帖数: 418 | 37 public class PlaylistFlagsParametersWrapper {
private boolean[] accessArray = new boolean[8];
public PlaylistFlagsParametersWrapper(PlaylistFlagsParameters
playlistFlagsParameters) {
accessArray[0] = playlistFlagsParameters.access0;
accessArray[1] = playlistFlagsParameters.access1;
accessArray[2] = playlistFlagsParameters.access2;
accessArray[3] = playlistFlagsParameters.access3;
accessArray[4] = playlistFlagsParameters.access4;
acc... 阅读全帖 |
|
d****n 发帖数: 233 | 38 public class Solution {
public boolean isMatch(String s, String p) {
if (p=="") return s == "";
int m = s.length() + 1;
int n = p.length() + 1;
boolean[][] result = new boolean[n][m];
// Initialization part
result[0][0] = true;
result[1][0] = false;
for( int i = 2; i < n; i++)
result[i][0] = p.charAt(i-1) == '*' ? result[i-2][0] : false;
for( int j = 1; j < m; j++){... 阅读全帖 |
|
d****n 发帖数: 233 | 39 Another one for Wildcard match:
public class Solution {
public boolean isMatch(String s, String p) {
int m = s.length() + 1;
int n = p.length() + 1;
boolean[][] result = new boolean[n][m];
result[0][0] = true;
for( int i = 1; i < n; i++){
result[i][0] = p.charAt(i-1) == '*' ? result[i-1][0] : false;
}
for( int j = 1; j < m; j++){
result[0][j] = false;
}
for( int i = 1; i ... 阅读全帖 |
|
s**********e 发帖数: 326 | 40 来自主题: JobHunting版 - 一个小题目 贴个我的代码:
evenProb have 50/50 probability to return true, genAnyProb can return true
with any prob(first parameter)
main should call genAnyProb like this: genAnyProb(0.6,1,0.00001)
public boolean evenProb() {
return new Random().nextInt(2) == 0;
}
public boolean genAnyProb(double prob, double base, double epsilon) {
if (prob * base < epsilon) {
return true;
}
if (prob == 0.5) {
return evenProb();
} else if (prob > 0.5) {
... 阅读全帖 |
|
f******p 发帖数: 173 | 41 allocate an array
dp[i] = true, if 0 is reachable from position i.
Update dp[] incrementally until no new changes are made to dp[] array.
worst case o(n2) ~=sum 0 to n, when only one element in dp[] is set to true
per scan. eg. 1,1,1,1,1,0
// assume a[] non-negative, and contains at least one 0
//[1,2,1,0,3]
public static boolean sol1 (int[] a, int k) {
int len = a.length;
boolean[] canJump = new boolean[len];
//find indices of 0 first
for (int i=0; ... 阅读全帖 |
|
l*n 发帖数: 529 | 42 来自主题: JobHunting版 - G新鲜面经 仔细考虑了下,1.2这么做比较简单:就是把数组遍历一遍,但是每次要调头,如果调
头的时候没有候选了就fail.
写了个java的code
[1,2,4,5,6]给出的所有结果是
[1, 4, 2, 6, 5]
[1, 5, 4, 6, 2]
[1, 5, 2, 6, 4]
[1, 6, 4, 5, 2]
[1, 6, 2, 5, 4]
[2, 4, 1, 6, 5]
[2, 5, 4, 6, 1]
[2, 5, 1, 6, 4]
[2, 6, 4, 5, 1]
[2, 6, 1, 5, 4]
[4, 5, 2, 6, 1]
[4, 5, 1, 6, 2]
[4, 6, 2, 5, 1]
[4, 6, 1, 5, 2]
[5, 6, 2, 4, 1]
[5, 6, 1, 4, 2]
List> reorder(int[] arr) {
assert (arr != null);
Arrays.sort(arr);
List> ol = new ArrayList>();
... 阅读全帖 |
|
B*****7 发帖数: 137 | 43 这是问题的链接:
http://oj.leetcode.com/problems/word-break/
我的solution是:
public class Solution {
private HashMap cache = new HashMap(
);
public boolean wordBreak(String s, Set dict) {
if( s.length() == 0 ) return false;
if( cache.containsKey( s ) ) return cache.get( s );
Boolean result = false;
for( int i = 0; i < s.length(); i++ ) {
String head = s.substring( 0, i+1 );
String rest = (i < s.length... 阅读全帖 |
|
r****s 发帖数: 42 | 44 补充点知识,大家共勉,呵呵:
上面有童靴说,java的boolean存储是jvm dependent的,常见的是 1byte。
我去考古了下,确实Java的虚拟机不给力吖,处理boolean数组,元素是当成1 byte处
理。但理论上应该是1 bit的。因此,在遇到要用boolean数组时,应该用java.util.
BitSet 来替换boolean数组。BitSet is a vector of bits, JVM对BitSet 确实是按 1
bit处理的。这个工作时尤其处理后台大数据,用BitSet能省很多资源。
参考链接: http://chrononsystems.com/blog/hidden-evils-of-javas-byte-array-byte |
|
n******7 发帖数: 99 | 45 rt,还是说用ternary operator会降低可读性,所以尽量少用,换成if else语句。举
个例子,Check a binary tree is a binary search tree.
public boolean isBST(Node root){
return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
}
public boolean isBST(Node root, int min, int max){
return (root==null)?true:((root.getValue()>=min)&&(root.getValue()<
max)&&isBST(root.left(),min,root.getValue())
&&isBST(root.right(),root.getValue(),max));
}
还是写成
public boolean isBST(Node root, int min, int max){
... 阅读全帖 |
|
f******h 发帖数: 45 | 46 也找工作了一段时间了,从版上学了很多,上周G家面完了,求个bless。
之前的一些都挂了,还在继续找其他的。等定下来之后一定发面经回报本版。
谢谢大家啦!!
1. http://www.mitbbs.com/article_t/JobHunting/32005597.html
1) Implement a simple calculator (+,-,*,/);
2) Implement "+1" for a large integer;
3) How to match Ads to users;
4) How to extract useful information from a forum webpage (list all
kinds of useful signal you can think of)
5) How to detect the duplicate HTML pages (large scale);
6) Find all the paths between two places on Google map;
7)... 阅读全帖 |
|
w*****j 发帖数: 226 | 47 用自动机写的
求批判
个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
corner case看得也比较清楚
enum State
{
ErrorState
{
@Override
public boolean isValid() { return false; }
},
EndState,
StartState
{
@Override
public State readDigit() { return DigitState; }
@Override
public State readPlus() { return PlusState; }
@Override
public State readMinus() { return MinusState; }
@Override
public State readPoin... 阅读全帖 |
|
w*****j 发帖数: 226 | 48 用自动机写的
求批判
个人觉得自动机的好处是可视化,只要图画对,代码不容易写错
corner case看得也比较清楚
enum State
{
ErrorState
{
@Override
public boolean isValid() { return false; }
},
EndState,
StartState
{
@Override
public State readDigit() { return DigitState; }
@Override
public State readPlus() { return PlusState; }
@Override
public State readMinus() { return MinusState; }
@Override
public State readPoin... 阅读全帖 |
|
x*******6 发帖数: 262 | 49 贡献一个code,由于没有乘除,只需要记录括号内是否要根据加减改变数字的符号,不
需要使用reverse polish notation解法。
public static int eval(String s, int x) {
String[] exp = s.split("=");
int[] left = helper(exp[0],x);
int[] right = helper(exp[1],x);
return (left[0] - right[0])/(right[1]-right[0]);
}
private static int[] helper(String s, int x) {
boolean positive = true;
Stack re = new Stack();
re.push(false);
int num = 0;
int y = 0;
... 阅读全帖 |
|
l*****n 发帖数: 246 | 50 第2题不是应该是leetcode上的permutation II吗?还是我题意理解错了?
尝试着写了个第二题和第三题的代码:
//给定一个字典,给定一个字符串,返回所有可能的组合。
public List words(Set dict, String str) {
char[] arr = str.toCharArray();
List result = new ArrayList();
Arrays.sort(arr);
helper(dict, arr, result, new boolean[arr.length], new ArrayList<
Character>());
return result;
}
private void helper(Set dict, char[] arr, List result,
boolean[] used, List item) {
if(item.size()==arr.l... 阅读全帖 |
|