由买买提看人间百态

topics

全部话题 - 话题: integer
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
a**********0
发帖数: 422
1
来自主题: JobHunting版 - LRU cache 问题
自己的代码老通不过 不知道为什么
public class LRUCache {

int cap;

//hashmap is for Cache's storage
// arraylist is for item's recency
HashMap myHashMap = new HashMap();
// key-value
ArrayList myArrayList = new ArrayList();// key


public LRUCache(int capacity) {

this.cap = capacity;

}

public int get(int key) {

if(this.myHashMap.get(key) == null)
ret... 阅读全帖
t**r
发帖数: 3428
2
来自主题: JobHunting版 - 出道题。perfectPermutation
我的代码:
很糟糕,大数据过不去
应该可以用bitmap 优化一下 今天懒得弄了
package topcoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
//B[0] = 0
//B[i] = A[B[i-1]], for every i between 1 and N-1, inclusive.
/*
* Permutation Child array
{0, 1, 2} {0, 0, 0}
{0, 2, 1} {0, 0, 0}
{1, 0, 2} {0, 1, 0}
{1, 2, 0} {0, 1, 2}
{2, 0, 1} {0, 2, 1}
{2, 1, 0} {0, 2, 0}
* */
public... 阅读全帖
s*******e
发帖数: 35
3
来自主题: 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
4
来自主题: 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<... 阅读全帖
r******r
发帖数: 700
5
来自主题: JobHunting版 - Apple Siri 组 Java 测试题
/**
* Using {@link AppleExercise.Problem1.SampleCache} as a starting point,
answer questions 1-3 by
* creating 3 new inner classes within {@link AppleExercise.Problem1}
below.
*/
public static class Problem1 {
public interface Cache {
public void put(Object key, Object value);
public Object get(Object key);
}
public static class SampleCache implements Cache {
private Map map;
public SampleCache() {
... 阅读全帖
c********p
发帖数: 1969
6
来自主题: JobHunting版 - 求个4sum的算法
这个是以前写的。。这个当时是可以过大oj的。
和我这次写的,意思是一样的。。。
我就是按照以前能过的写的。。。结果都过不了了
import java.util.*;
public class Solution {
public ArrayList> fourSum(int[] num, int target) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> result = new ArrayList Integer>>();
HashSet> set = new HashSet>();

if(num == null || num.length < 4){
retur... 阅读全帖
l*******A
发帖数: 209
7
来自主题: JobHunting版 - 一道面试题和解法(求指点).
题目:
Write a method in Java to:
Find all set of permutations from N number of ArrayLists. Each ArrayList has
a different length.
Each permutation is formed by picking one item from each input ArrayList.
You have to exhaust ALL permutations and can't return duplicate permuations.
Each permutation is a Set, so the order of the items does not matter. For
example [a1,b1,c1] is the same permutation as [c1,b1,a1].
Example:
Input: N number of array lists with different length
[a1,a2,a3....]
[b1,b2....... 阅读全帖
C*******n
发帖数: 24
8
来自主题: JobHunting版 - 一道L题
标记个start,然后一步一步开始做
java code,貌似我童鞋也被考这个题了。
个人做了下,蛮有思路,耗时有点长,但是差不多能handle
不过真的算是个难的phone题啊,我这个小弱是这么觉得的。。。。
public void solution(int num){
int i = 1;
while(i * i <= num){
if(i == 1){
System.out.println(num + " * 1");
}else if( num % i == 0){
ArrayList> tmp = helper(num / i, i);
for (ArrayList r : tmp){
for ( Integer no : r){
System.out.print(no + " * ");
}
System.out.println(i);
... 阅读全帖
S********s
发帖数: 29
9
来自主题: JobHunting版 - G家题讨论: harry potter 走矩阵
example code of blaze's solution:
import java.util.Arrays;
import java.util.Random;
/*
* http://www.mitbbs.com/article_t1/JobHunting/32611137_0_1.html
*/
public class HarryPotMatrix {
Integer[][] w;
Integer m, n;
Integer[][] f;
public void initialize(int i, int j) {
Random r = new Random();
m = i;
n = j;
w = new Integer[m][n];
for (int l = 0; l < m; l++) {
for (int h = 0; h < n; h++) {
w[l][h] = -1 * r.nextInt(20... 阅读全帖
f******h
发帖数: 45
10
也找工作了一段时间了,从版上学了很多,上周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)... 阅读全帖
a*********4
发帖数: 7
11
来自主题: JobHunting版 - 一道面试题
a java solution:
import java.util.*;
public class FindNonDupeSubTree {
private class Node{
int val;
Node left;
Node right;
Node (int val){
this.val = val;
left = null;
right = null;
}
}

public boolean FindSubTree(Node root, Set nodeSet, ArrayList<
Integer> validTreeRoots){
if (root == null)
return true;
Set sleft = new HashSet();
Set ... 阅读全帖
a*********4
发帖数: 7
12
来自主题: JobHunting版 - 一道面试题
a java solution:
import java.util.*;
public class FindNonDupeSubTree {
private class Node{
int val;
Node left;
Node right;
Node (int val){
this.val = val;
left = null;
right = null;
}
}

public boolean FindSubTree(Node root, Set nodeSet, ArrayList<
Integer> validTreeRoots){
if (root == null)
return true;
Set sleft = new HashSet();
Set ... 阅读全帖
p*y
发帖数: 108
13
来自主题: 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{
... 阅读全帖
h****3
发帖数: 89
14
来自主题: JobHunting版 - F电面
多谢楼组分享,自己写了一个java版本的
public static void printVertical(TreeNode root){
HashMap> map = new HashMap Integer>>();
printVerticalHelper(root,0,map);
SortedSet keys = new TreeSet(map.keySet());
for(int key:keys){
List temp = map.get(key);
for(int i=0;i System.out.print(temp.get(i)+" ");
}
System.out.println();
}
... 阅读全帖
w*****h
发帖数: 423
15
有人不愿意进去看的话,我贴这
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阅读全帖
y*******5
发帖数: 887
16
来自主题: JobHunting版 - 求指点一道G家Iterator的题目
用composition pattern:
1:---
package NestedIterator;
public interface NestedNodeList extends Iterable {
}
2:---
package NestedIterator;
import java.util.Iterator;
public class Node implements NestedNodeList {
private E val;
public Node(E val) {
this.val = val;
}
@Override
public Iterator iterator() {
return new NodeIterator();
}
private class NodeIterator implements Iterator {
private boolean iterated = false;
@Override... 阅读全帖
x****d
发帖数: 1766
17
LinkedList list = new LinkedList();
Integer inte = new Integer(3);
list.add(new Integer(3));
list.add(new Integer(3));
list.add(new Integer(5));
list.add(new Integer(3));
list.add(new Integer(5));

list.remove(inte);

// list.remove(new Integer(5));
list.removeLastOccurrence(new Integer(5));
System.out.println(list);

System.out.println(list.indexOf(3));
S... 阅读全帖
s******e
发帖数: 108
18
来自主题: JobHunting版 - 攒人品,twitter二面面经
可以用 hashset 来替代 vector ,
public class RandomHash {
HashMap > hashmap = new HashMap LinkedHashSet>();
Vector vec = new Vector();
public void insert(K key) {
vec.add(key);
if( ! hashmap.containsKey(key) ) {
LinkedHashSet hashSet = new
LinkedHashSet();
hashSet.add(new Integer(vec.size()-1));
hashmap.put(key,hashSet);
} else {
h... 阅读全帖
P********l
发帖数: 452
19
来自主题: JobHunting版 - 求教一个算法面试问题,被难住了
受益匪浅。
非常佩服han6,redcloud的数学推导。
这是按照han6的思路写的代码。
http://code.google.com/p/sureinterview/source/browse/test/test1/Matrix1.java#22
======
void generate(int n, int m) {
Integer[] segEnd = new Integer[n - 1];
for (int i = 0; i < n - 1; i++) {
segEnd[i] = i;
}
CombinationImpl combn = new CombinationImpl(Lists
.newArrayList(segEnd), m - 1);
for (List lastPos : combn) {
Integer[] codeSeed = new ... 阅读全帖
g**********y
发帖数: 14569
20
来自主题: JobHunting版 - F的puzzle - Liar Liar
换了种结构,ListArray + HashMap, code还是ugly, 稍
好点。
public class LiarLiar {
public int[] divide(String[] line) {
ArrayList list = new ArrayList();
HashMap pairs = new HashMap();

int N = Integer.parseInt(line[0].trim());
for (int i=0, j=1; i String[] s = line[j].split(" ");
String name = s[0].trim();
int n = Integer.parseInt(s[1].trim(... 阅读全帖
A**u
发帖数: 2458
21
来自主题: JobHunting版 - 请教一个题目
career cup 上的题目
Write a method that returns all subsets of a set.
想请教一下,递归的怎么写呢
这是career cup的java写法,我不懂java,完全看不懂, 请大家给个算法思路
ArrayList> getSubsets(ArrayList set,
2 int index) {
3 ArrayList> allsubsets;
4 if (set.size() == index) {
5 allsubsets = new ArrayList>();
6 allsubsets.add(new ArrayList()); // Empty set
7 } else {
8 allsubsets = getSubsets(set, index + 1);
9 int item = set.get(index);
10 ArrayList阅读全帖
c*****r
发帖数: 108
22
来自主题: JobHunting版 - 又有leetcode题目来请教了
题目是path sum||
过了小测试。但是没有过大测试。 fail的testcase 是一个堆非常复杂的+/-1 的树。
下面是我的code。 麻烦大家来抠一下吧。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList> pathSum(TreeNode root, int sum) {
ArrayList> paths = new ArrayList Integer>>();
pathSum_r(root, sum, new ArrayList(), paths);
ret... 阅读全帖
Y**3
发帖数: 21
23
来自主题: JobHunting版 - 又有leetcode题目来请教了
我看你的代码逻辑和我的一样,本想改成像C++一样简洁的版本,后发现对java不熟
arraylist和vector还不一样,就放弃了,直接给去了几个没必要的if就过了large
case,八百多毫秒,看来java太慢啊。。。
public class Solution {
public ArrayList> pathSum(TreeNode root, int sum) {
ArrayList> paths = new ArrayList Integer>>();
pathSum_r(root, sum, new ArrayList(), paths);
return paths;
}
private void pathSum_r(TreeNode root, int sum, ArrayList path,
ArrayList> paths... 阅读全帖
r**h
发帖数: 1288
24
来自主题: JobHunting版 - 一点码工求职经验总结,回报本版
个人总结的一些C/C++问题:
C Tutorial:
http://www.cprogramming.com/tutorial.html
C问题集:
http://www.indiabix.com/technical/c/
http://www.parashift.com/c++-faq-lite/index.html
1) 为啥要用c++;
2) encapsulate, polymorphism ,inheritance 的概念;
encapsulate: Hide details of implementation
polymorphism : has multiple forms
inheritance: is-a relationship
3)class的概念
type of object
4)default constructor是啥样的;
without parameters
5) 写copy  constructor要注意啥
parameter must be reference : Node(const... 阅读全帖
l********7
发帖数: 40
25
来自主题: JobHunting版 - 一道面试题和解法(求指点).
public static ArrayList> getPermutation(ArrayList<
ArrayList> lists){
ArrayList> result = new ArrayList Integer>>();
if(lists != null && lists.size() != 0){
permutation(lists, result, new ArrayList(), 0);
}

return result;
}

public static void permutation(ArrayList> lists,
ArrayList> result, ArrayList current, int ind... 阅读全帖
e***l
发帖数: 710
26
来自主题: JobHunting版 - LRU cache 问题
import java.util.ArrayList;
import java.util.HashMap;
// TODO: rename this class to "LURCahch" for Leetcode submission
public class LRUCache2 {
private ArrayList list; // list of keys
private HashMap valueMap; // key-value map
private HashMap countMap; // appearance of each keys
in the key list
private int maxSize;
public LRUCache2(int capacity) {
maxSize = capacity;
list = new ArrayList();
valueMap... 阅读全帖
y***i
发帖数: 414
27
public static List> getPerms(int[] num) {
if (num == null || num.length == 0) {
return null;
}
List> res = new ArrayList>();
List first = new ArrayList();
res.add(first);
for (int i = 0; i < num.length; i++) {
List> newRes = new ArrayList>();
for (int j = 0; j < res.size(); j++) {
List current = res.ge... 阅读全帖
g*******e
发帖数: 107
28
来自主题: JobHunting版 - 请问这道题如何做?Zero-one multiple
public zero_one_multiple(int N){
HashMap> map=new HashMap Integer>> ();
int jj=0;
ArrayList remain=new ArrayList();
remain.add(0);
long resD=1;
while (resD int residue=(int) (resD%N);
if (residue==0) return resD;
jj++;
int size=remain.size();
for (int i=0; i int tmp=remain.get(i)+residue;
if ((tmp%N)==0) {
ArrayList res... 阅读全帖
y*****e
发帖数: 712
29
public static Iterable mergeKSortedIterators(List
> Iters){

Queue minHeap = new PriorityQueue();
List result = new ArrayList();
for(Iterator iter : Iters){
if(iter.hasNext()){
minHeap.add(new newIter(iter.next(), iter));
}
}

while(!minHeap.isEmpty()){
newIter newiter = minHeap.poll();
result.add(newiter.get... 阅读全帖
I*******g
发帖数: 7600
30
来自主题: JobHunting版 - 求指点一个G家题
using dynamic programming (cache):
public class Operations
{
static class OperationFormula
{
public int value;
public String formula;
public OperationFormula(int x, String y){
value = x;
formula = y;
}
}
static Map, HashSet> map = new
HashMap,HashSet>();

public static void main( String[] args )
{
int target = 48;
Integer[] item ... 阅读全帖
j*****g
发帖数: 223
31
总结一下面试的准备活动,希望有帮助.
==================== AREAS/TOPICS to study/prep/review ====================
复习的东西还挺多的。比不过刚毕业的呀 :), 脑子不好使了,东西也差不多忘光了...
嘿嘿....
• Sorting
o Bubble/select/insertion/counting/qsort/heap/merge/bst
o Time/space complexity analysis
• Caching design
o Replacement policy (LRU, LFU, NRU, etc…)
o Efficiency/complexity/performance
o Distributed cache
o Hashing
• Multi-thread
o Locking/mutex/semaphore/critical sec... 阅读全帖
j*****g
发帖数: 223
32
总结一下面试的准备活动,希望有帮助.
==================== AREAS/TOPICS to study/prep/review ====================
复习的东西还挺多的。比不过刚毕业的呀 :), 脑子不好使了,东西也差不多忘光了...
嘿嘿....
• Sorting
o Bubble/select/insertion/counting/qsort/heap/merge/bst
o Time/space complexity analysis
• Caching design
o Replacement policy (LRU, LFU, NRU, etc…)
o Efficiency/complexity/performance
o Distributed cache
o Hashing
• Multi-thread
o Locking/mutex/semaphore/critical sec... 阅读全帖
g**********y
发帖数: 14569
33
来自主题: JobHunting版 - 问一个facebook的电面
我只能写到这样了。
public String multiply(String a, String b) {
ArrayList la = convert(a);
ArrayList lb = convert(b);

ArrayList lc = new ArrayList();
for (int i=0; i
int remainder = 0;
for (int i=0; i for (int j=0; j int s = lc.get(i+j) + la.get(i)*lb.get(j) + remainder;
lc.set(i+j, s%1000);... 阅读全帖
g**********y
发帖数: 14569
34
来自主题: JobHunting版 - 问一个facebook的电面
我只能写到这样了。
public String multiply(String a, String b) {
ArrayList la = convert(a);
ArrayList lb = convert(b);
ArrayList lc = new ArrayList();
for (int i=0; i for (int i=0; i int remainder = 0;
for (int j=0; j int s = lc.get(i+j) + la.get(i)*lb.get(j) + remainder;
lc.set(i+j, s%1000);
remainder = s/1000;
}
if (remainder > 0) lc.set(i+lb.size(), remainder);
}
StringBuilder sb = new StringBui... 阅读全帖
s******c
发帖数: 99
35
来自主题: JobHunting版 - 又有leetcode题目来请教了
问题在于recursive call的时候,你每次都重新生成left_path和right_path,然后拷
贝整个path到里面,这样就会慢,不需要这两个额外的arraylist,就用path一个就可,
backtrack就是你走完left child和 right child 之后都回到parent就可,其实就是把
path最后一个element去掉就行。
public class Solution {
public ArrayList> pathSum(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> results=new ArrayList Integer>>();
ArrayList path=new ArrayList<... 阅读全帖
P***t
发帖数: 1006
36
来自主题: JobHunting版 - 有些面试题是够扯蛋的
正好写了个Program比较了一下,再加上你的HASHSET:
Sort used time (ms): 1182
HashMap Used time (ms): 2959
HashSet Used time (ms): 3583
import java.io.Console;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class LongestConsecSequence {
public int longestConsecutive(int[] a) {
return v1(a);
}
public int v1(int[] in) {
int[] a = new int[in.length];
System.arraycopy(in, 0, a, 0, in.length);
if (a.length == 0)
return 0;
Arrays... 阅读全帖
c********p
发帖数: 1969
37
来自主题: JobHunting版 - 求个4sum的算法
这个是今天的。。。
import java.util.*;
public class Solution {
public ArrayList> fourSum(int[] num, int target) {
// Start typing your Java solution below
// DO NOT write main() function
HashSet> set = new HashSet>();

if(num == null || num.length <= 3){
return new ArrayList>(set);
}

Arrays.sort(num);
Hashtable> hash = new Has... 阅读全帖
m*******1
发帖数: 168
38
来自主题: JobHunting版 - 问个递归的问题
不好意思,我基础太差了,在leetcode上遇到好几道类似的题目,每次在同一个地方总
是弄不明白,希望同学们能帮我讲解下,帮助和我一样有困惑的人,先谢谢大家啦!
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],
]
//solution
public ArrayList> combine(int n, int k)
{
ArrayList> res= new ArrayList
>();
ArrayList temp= new ArrayList ();
... 阅读全帖
l****o
发帖数: 315
39
来自主题: JobHunting版 - LeetCode 新题 graph clone
DFS 和 BSF 的都写了,已过OJ
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<
UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if (node == null) return null;
Ha... 阅读全帖
l*n
发帖数: 529
40
来自主题: JobHunting版 - 一道面试题和解法(求指点).
从你这个dfs解法倒是学到了点东西:n重循环可以通过dfs来做。:D
解法当中唯一的问题就是set判重错了,ArrayList是没有重定义hash()跟
equals()方法的,所以判重就只看是否是同一个reference。
List> arrayCombinate(List arrs) {
List> ol = new ArrayList>();
if (arrs.size() == 0)
return ol;
dfs(arrs, 0, new ArrayList(), ol, new HashSet());
return ol;
}
void dfs(List arrs, int i, List curr,
List> ol, Set memo) {
if (i == arrs.siz... 阅读全帖
l*****n
发帖数: 52
41
来自主题: JobHunting版 - 请教一下subset I 输出子集顺序问题
leetcode subset 我用eclipse调试
import java.util.*;
public class subSets {
public static HashSet> subsets(int[] s){
HashSet> result = new HashSet
>();
if(s.length == 0|| s== null) return null;
//Arrays.sort(s);
ArrayList list = new ArrayList();
recursion(result,list,s,0);
return result;

}
public static void recursion(HashSet> result,
Array... 阅读全帖
a***m
发帖数: 5037
42
来自主题: JobHunting版 - amazon 新鲜面筋
phone coding questions :
1 - Write a function that takes an integer N and returns the Nth number of a
Fibonacci suite.
2 - Given a list of Integer. Write a function that takes an integer and
returns all the pair of integers included in the given list that sum up with
this integer.
Face to face coding questions :
1 - Write a function that takes an integer and return a string of this
integer in Roman number format.
2 - Go game. Write a function that takes a position (x,y) in a go game graph
and re... 阅读全帖
b*****i
发帖数: 130
43
我是按照这位大侠的解法来做leetcode subsets这题的。
http://blog.csdn.net/u011095253/article/details/9158397
01.public class Solution {
02. public ArrayList> subsets(int[] S) {
03. ArrayList> res = new ArrayList Integer>>();
04. ArrayList tmp = new ArrayList();
05. Arrays.sort(S);
06. res.add(tmp);
07. dfs(res,tmp,S,0);
08. return res;
09. }
10.
11. public void dfs(ArrayList阅读全帖
a**********0
发帖数: 422
44
来自主题: JobHunting版 - LRU cache 超时
代码本身很简单 但是超时 而且test case 不容易复制 因为是cap =2048 然后一长串
操作的
import java.util.*;
public class LRUCache {

int cap;
//hashmap is for Cache's storage
// arraylist is for items' recencies
HashMap myHashMap; // key-value
ArrayList myArrayList; // key


public LRUCache(int capacity) { // this is the constructor

this.cap = capacity;
myHashMap = new HashMap(); // key-value
myArrayList = new ArrayL... 阅读全帖
y**********a
发帖数: 824
45
遍历所有的可能性,计算。
public int maxResult(int[]A) {
int[]rel=new int[1];rel[0]=Integer.MIN_VALUE;
dfs(A, new int[A.length-1], 0, rel);
return rel[0];
}
void dfs(int[]A, int[]op, int i, int[] max) {
if (i==op.length) {
StringBuilder s=new StringBuilder();
s.append(Integer.toString(A[0]));
char[] opc=new char[4];
opc[0]='+';opc[1]='-';opc[2]='*';opc[3]='/';
for (int j=0;j ... 阅读全帖
p**p
发帖数: 742
46
来自主题: JobHunting版 - 请教 print factors 这个题
暴力递归解法:
public List> printFactors(int n) {
List> result = new ArrayList>();
if(n <= 0) {
return result;
}
List trace = new ArrayList();
printFactorsHelper(n, trace, result);
return result;
}

private void printFactorsHelper(int n, List trace, List Integer>> result) {
List list = new ArrayList(trace);
if(list.isEmpt... 阅读全帖
C*******a
发帖数: 448
47
来自主题: JobHunting版 - java的基本问题
有个java的基本问题,一直没搞清楚,问题很简单:
以下是Pascal's triangle,正确的代码。
在第3行,我之前是这么写的:
List> t = new ArrayList>();
第5行是:ArrayList row = new ArrayList();
结果总是报错‘incompatible type’,这是为什么?
多谢!!!
public class Solution {
public List> generate(int numRows) {
List> t = new ArrayList>();
for (int i = 0; i < numRows; i++) {
List row = new ArrayList();
for (int j = 0; j ... 阅读全帖
h**********c
发帖数: 4120
48
来自主题: 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... 阅读全帖
k****r
发帖数: 807
49
来自主题: JobHunting版 - 一道FB的followup 问题
recursive,
用list《List《Integer》》 res纪录结果,
用一个mostleft记录目前最左边的index,
如果目前level小于mostleft,or超出res大小,插最前面,或最后面新的list:
static int mostLeft = 0;
public static void printTreeInVerticalOrder(TreeNode root) {
List> res = new ArrayList<>();
//res.add(new ArrayList());
printHelper(root, 0, res);
printTree(res);
}
public static void printHelper(TreeNode root, int curr, List Integer>> res) {
if (root == null) return;
els... 阅读全帖
k****r
发帖数: 807
50
来自主题: JobHunting版 - 一道FB的followup 问题
recursive,
用list《List《Integer》》 res纪录结果,
用一个mostleft记录目前最左边的index,
如果目前level小于mostleft,or超出res大小,插最前面,或最后面新的list:
static int mostLeft = 0;
public static void printTreeInVerticalOrder(TreeNode root) {
List> res = new ArrayList<>();
//res.add(new ArrayList());
printHelper(root, 0, res);
printTree(res);
}
public static void printHelper(TreeNode root, int curr, List Integer>> res) {
if (root == null) return;
els... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)