由买买提看人间百态

topics

全部话题 - 话题: integate
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
l*****o
发帖数: 214
1
public class NumberPickGame {

public NumberPickGame() {}

public static boolean canIForceWin(int maxNumber, int total) {
if ((maxNumber + 1) * maxNumber /2 < total) {
return false;
}
Set numbersAvailable = new HashSet();
for (int i = 1; i <= maxNumber; i++) {
numbersAvailable.add(i);
}
int[] picks = new int[maxNumber];
boolean canWin = canFirstPlayerWin(numbersAvailable, total, 0, pic... 阅读全帖
T*****n
发帖数: 82
2
String to Integer (atoi) 这一题
通过的code是:
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1)
return 0;
// trim white spaces
str = str.trim();
char flag = '+';
// check negative or positive
int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
// use double to store result
double result = 0;
// calculate value
while... 阅读全帖
b*********n
发帖数: 26
3
来自主题: JobHunting版 - 用java面试真吃亏
用ArrayList是可以直接remove 了, 我面试时就是这么干的。
但是又有一个问题:
写test case的时候,如果用int[][] , initialize 只需要:
int[][] t = {{1, 1, 1, 1}, {0, 1, 6}, {1}};
但是用ArrayList> , 我就得:
ArrayList> t2 = new ArrayList>();
ArrayList r0 = new ArrayList(Arrays.asList(1,1,1,1));
ArrayList r1 = new ArrayList(Arrays.asList(0,1,6));
ArrayList r2 = new ArrayList(Arrays.asList(1));
t2.add(r0);
t2.add... 阅读全帖
k****r
发帖数: 807
4
我写了一个,不知道可用不,先用DP找到最长subsequence的长度,complexity is O(
nlongn);
再用recursive找到所有可行解。
public static List> AllLongestIncreasingSubsequence(int[] nums
) {
int[] m = new int[nums.length + 1];
int L = 0;
for (int i = 0; i < nums.length; i++) {
int lo = 1;
int hi = L;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (nums[m[mid]] < nums[i]) lo = mid + 1;
else hi = mid - 1;
}... 阅读全帖
a***u
发帖数: 383
5
来自主题: JobHunting版 - 贡献一个 一个L家的店面题目
import java.util.*;
public class DesignDataStructure {
Map> map = new HashMap>();
List list = new ArrayList();

public void add(int num){
list.add(num);
int index=list.size()-1;
if(map.containsKey(num)){
map.get(num).add(index);
}else{
Set set =new HashSet();
set.add(index);
map.put(num, set);
}
}
public voi... 阅读全帖
a***u
发帖数: 383
6
来自主题: JobHunting版 - 贡献一个 一个L家的店面题目
import java.util.*;
public class DesignDataStructure {
Map> map = new HashMap>();
List list = new ArrayList();

public void add(int num){
list.add(num);
int index=list.size()-1;
if(map.containsKey(num)){
map.get(num).add(index);
}else{
Set set =new HashSet();
set.add(index);
map.put(num, set);
}
}
public voi... 阅读全帖
a**d
发帖数: 64
7
来自主题: JobHunting版 - 问两道fb题
第一题的java答案抄了一个,运行通过的。
https://github.com/nagajyothi/InterviewBit/blob/master/DynamicProgramming/
AvgSet.java
各路大神看看还有没有更优解。
// Sum_of_Set1 / size_of_set1 = total_sum / total_size, and so, Sum_of_Set1
= size_of_set1 * total_sum / total_size.
// Now we can just iterate on size_of_set1, and we would know the required
Sum_of_Set1.
static List res = new ArrayList();
static boolean[][][] dp;
public static List> partitionAverage(int[] num) {
List阅读全帖
o*q
发帖数: 630
8
来自主题: JobHunting版 - 请教leetcode高频题是哪些题
# Title Editorial Acceptance Difficulty Frequency
1
Two Sum 28.3% Easy
292
Nim Game 54.4% Easy
344
Reverse String 57.3% Easy
136
Single Number 52.2% Easy
2
Add Two Numbers 25.6% Medium
371
Sum of Two Integers 51.6% Easy
4
Median of Two Sorted Arrays
20.4% Hard
6
ZigZag Conversion 25.6% Easy
13
Roman to Integer 42.7% Easy
237
... 阅读全帖

发帖数: 1
9
下面是单线程,而且图是adjacency list
public class Solution {
//5763 ms, method: bfs, 时间O(N + E),空间O(N)。
public List> connectedSet(ArrayList
nodes) {
List> res = new ArrayList<>();
List path = new ArrayList<>();
Set visited = new HashSet<>();
for (UndirectedGraphNode node : nodes) {
if (!visited.contains(node)) {
path.clear();
Queue阅读全帖
z****e
发帖数: 54598
10
来自主题: Java版 - 关于==和equals
今天做leetcode突然遇到的一个问题
在数据量比较小的时候,不会有问题
但是数据量一旦变大,马上就出问题了
不多说废话,上代码,代码很简单
Map m1 = new HashMap();
Map m2 = new HashMap();
然后
m1.put('a',1);
m2.put('a',new Integer(1));
然后
m1.get('a') == m2.get('a'),这个autoboxing理论上是可以的
但是实际上,在数据量陡然变大了之后,这个会出现false的结果
不是很明白为什么
不过让我想起一个往事就是
enum类型的判断,同样可以用==来替换equals
但是,这个情况在rmi的时候会出问题
所以说到底,还是尽量避免使用==
否则会出现很多很subtle的问题
以下是代码正文
test case什么都写好了
可以直接debug和运行main函数
package test;
import java.... 阅读全帖
h******o
发帖数: 334
11
A .txt file. Use the below java code to read the file and use a 2-D array (
one dimension for the users and other dimension for the products) to store
the order#. Also, use a dictionary to map each users to its corresponding
array row. How to replace the missing value with 0 in the 2D array?
Users, Products, order#:
name1 p1 5
name1 p2
name1 p3 2
name2 p1 3
name2 p2 1
name2 p3
name3 p1 5
name3 p2
name3 p3 2
name4 p1 3
name4... 阅读全帖
P********l
发帖数: 452
12
来自主题: JobHunting版 - Facebook Interview Questions
O(n) algorithm:
public Integer[] getMaxInSlideWindow(Integer[] A, Integer w) {
// invalid input
if (A == null || w <= 0 || A.length - w + 1 <= 0)
return null;
Integer[] B = new Integer[A.length - w + 1];
// auxiliary queue that is sorted in descending order
List q = new LinkedList();
for (int i = 0; i < A.length; i++) {
// enqueue. Remove those smaller values
int data = A[i];
whi... 阅读全帖
g**********y
发帖数: 14569
13
来自主题: JobHunting版 - 问道amazon的面试题
写了个简化版的:
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
14
来自主题: JobHunting版 - 问道amazon的面试题
写了个简化版的:
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 ... 阅读全帖
y******n
发帖数: 47
15
来自主题: JobHunting版 - 面经+一点个人体会
周五面完最后一个onsite, 累的惨兮兮的, 好容易爬回家. 不管结果如何, 这段时间找
工作算是告一段落了.
下面把这段时间面试中被问到的题目整理一下, 供大家参考. 我也就不说具体是那些公
司了, 都是很典型的面试题, 到哪里都有可能会被问到.
* implement memcpy? How to improve? how to determine if a system is
32bit or 64bit?
* how is static keyword used in Java?
* a list of intervals, no overlapping and sorted, write a function to
insert an interval into the list and still keep the list sorted and no
overlapping.
* given a function on sorted dictionary to retrieve word by index,
string getWord(int i), how to i... 阅读全帖
g**********y
发帖数: 14569
16
来自主题: JobHunting版 - subset
I am not sure what's your question. But as example, here is a simplified
Java implementation, assume sets no more than 32. If more than that, you can
use BigInteger or write your own class.
public class Subset {
public Set[] merge(Set[] sets) {
HashMap cardinal = new HashMap();
HashMap map = new HashMap();
HashSet remove = new HashSet();

for (Set set : sets) {
i... 阅读全帖
S*******0
发帖数: 198
17
来自主题: JobHunting版 - 为什么做了400道算法题还是那么菜
正数和负数的overflow判断不一样,
public static int atoi(String str) throws Exception{
String expression = str.trim();
if(expression==null||expression.equals("")){
throw new Exception("expression is empty");
}
int sign = 1;
int index = 0;
if(expression.charAt(0) == '-'){
sign = -1;
}
if(expression.charAt(0) == '-' || expression.charAt(0) == '+'){
index++;
if(expression.length()==1){
... 阅读全帖
S*******0
发帖数: 198
18
来自主题: JobHunting版 - atoi很不好写,头都大了...
这个版上讨论过多次了,这是我的code
//atoi
public static int atoi(String str) throws Exception{
String expression = str.trim();
if(expression==null||expression.equals("")){
throw new Exception("expression is empty");
}
int sign = 1;
int index = 0;
if(expression.charAt(0) == '-'){
sign = -1;
}
if(expression.charAt(0) == '-' || expression.charAt(0) == '+'){
index++;
if(expression.length()==1){
... 阅读全帖
S**I
发帖数: 15689
19
来自主题: JobHunting版 - [合集] 面经+一点个人体会
☆─────────────────────────────────────☆
yuhanlin (Yuhan) 于 (Mon Aug 29 00:18:17 2011, 美东) 提到:
周五面完最后一个onsite, 累的惨兮兮的, 好容易爬回家. 不管结果如何, 这段时间找
工作算是告一段落了.
下面把这段时间面试中被问到的题目整理一下, 供大家参考. 我也就不说具体是那些公
司了, 都是很典型的面试题, 到哪里都有可能会被问到.
* implement memcpy? How to improve? how to determine if a system is
32bit or 64bit?
* how is static keyword used in Java?
* a list of intervals, no overlapping and sorted, write a function to
insert an interval into the list and still keep the list sorted and no
overlapping.... 阅读全帖
s********r
发帖数: 137
20
来自主题: JobHunting版 - 问道amazon 面试题
/**
* Took FightingXu's algorithm and implemented in JAVA:
*/
public static boolean isSameBST(int[] arrA, int aOffset, int aMin, int aMax,
int[] arrB, int bOffset, int bMin, int bMax)
{
if (arrA == null || arrB == null)
return false;
if (aOffset > -1 && aOffset == 0 && bOffset > -1 && bOffset == 0)
return true;
if (arrA.length == aOffset || arrB.length == bOffset)
return false;
if (aOffset > -1 && bOffset > -1 && arrA[aOffset] != arrB[bOffset])
return false;
int i, aLeftOffset = 0, bLeftOffset =... 阅读全帖
S******1
发帖数: 269
21
Basically the same solution with repeatable candidates. Recursion, time
complexity should be O(n!)?
import java.util.*;
public class Solution {
public ArrayList> combinationSum2(int[] num, int
target) {
ArrayList> result= new ArrayList Integer>> ();
ArrayList item=new ArrayList ();
Arrays.sort(num);
if(num!=null || num.length>0)
combinations(num, target, result, item, 0);

... 阅读全帖
j******s
发帖数: 48
22
来自主题: JobHunting版 - 请教leetcode Permutations II 解法和code
public ArrayList> permuteUnique(int[] num) {
Arrays.sort(num);
ArrayList> output
= new ArrayList>();
ArrayList list = new ArrayList();
boolean col[] = new boolean[num.length];
permute(output,list,col,num,0);
return output;
}

public void permute(ArrayList> output,
ArrayList list,boolean col[]... 阅读全帖
w***o
发帖数: 109
23
来自主题: JobHunting版 - 关于leetcode的combinationSum题
这个能过OJ。
public class Solution {
public ArrayList> combinationSum(int[] candidates,
int target) {
// Start typing your Java solution below
// DO NOT write main() function
if(candidates == null || candidates.length == 0)
return new ArrayList>();
Arrays.sort(candidates);

ArrayList> ret = new ArrayList
>();
getCombination(candidates, 0, 0, target, ne... 阅读全帖
t*******e
发帖数: 274
24
来自主题: JobHunting版 - combination sum II
有没有java solution么?下面的代码是combination sum的,怎么在这个基础上改呢?
public class Solution {
public ArrayList> combinationSum(int[] candidates,
int target) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList> results = new ArrayList Integer>>();
Arrays.sort(candidates);
addUp(candidates, 0, target, new ArrayList(), results);
return results;
}
private void addUp(i... 阅读全帖
f*******t
发帖数: 7549
25
来自主题: JobHunting版 - 问一道题
Java版本比较方便,可以用ArrayList自带的iterator。C++如果只需要实现类似于Java
iterator的两个接口,倒是不难。
public class DoubleLevelArrayListIterator {
private Iterator> itLvl1;
private Iterator itLvl2;

public DoubleLevelArrayListIterator(ArrayList> a) {
itLvl1 = a.iterator();
itLvl2 = null;
}

public boolean hasNext() {
if (itLvl2 != null && itLvl2.hasNext()) {
return true;
} else {
while ((itLvl2 == ... 阅读全帖
g**G
发帖数: 767
26
来自主题: JobHunting版 - 请教LeetCode的3Sum
Did you consider to skip duplicate element?
I pasted my solution as follows, hope it helps
public ArrayList> threeSum(int[] num) {
ArrayList> threeSum = new ArrayList Integer>>();
if (num == null || num.length == 0)
return threeSum;
Arrays.sort(num);
int len = num.length;
int prev1 = Integer.MIN_VALUE;
int prev2 = Integer.MIN_VALUE;
int prev3 = Integer.MIN_VALUE;
for (... 阅读全帖
s********x
发帖数: 914
27
切磋一下

public static ArrayList multiplyBigInterger(List num1,
List num2) {
if (num1 == null || num1.isEmpty() || num2 == null || num2.isEmpty()
) {
throw new IllegalArgumentException("Invalid input");
}
if (num1.size() < num2.size()) {
return multiplyBigInterger(num2, num1);
}
// num1.size() >= num2.size()
ArrayList result = new ArrayList(num1.size()
... 阅读全帖
t**********r
发帖数: 2153
28
来自主题: JobHunting版 - 请问如何去除结果里面的重复
这个是过了OJ的code.为了不超时,加了点tweak.看起来没有以前那么好看了.
public static ArrayList> threeSum(int[] inputs) {
if (inputs == null || inputs.length <= 2) {
// throw new IllegalArgumentException("Invalid inputs");
return new ArrayList>();
}
Arrays.sort(inputs);
Set> matchedTriplets = new HashSet Integer>>();
for (int i = 0; i < inputs.length - 2; i++) {
if (inputs[i] > 0... 阅读全帖
l*n
发帖数: 529
29
来自主题: 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>();
... 阅读全帖
S******1
发帖数: 216
30
来自主题: JobHunting版 - G家面经(已被HC挂,求分析)
//6:24 6:40
String divide(int a, int b) {
String res = "";
if (b == 0)
return res;
if (a == 0)
return "0";

if (a < 0 && b > 0 || a > 0 && b < 0)
res += "-";

a = a < 0 ? -a : a;
b = b < 0 ? -b : b;

res = res + Integer.toString(a/b) + ".";
a -= (a/b)*b;

Map posMap = new HashMap();
List arr = new ArrayList();
while (!posMap.containsKey(a)) {
posMap.pu... 阅读全帖
y**********a
发帖数: 824
31
除非具体知道奇数的次数,否则没法常数空间。
空间时间 : O(n) O(n)
static List timeEfficient(int[]A) {
Map m=new HashMap<>();
for (int a:A)
if (!m.containsKey(a)) m.put(a, 1);
else m.put(a, m.get(a)+1);
List rel=new ArrayList<>();
for (Map.Entry e:m.entrySet())
if (e.getValue()%2==0)
{rel.add(e.getKey());break;}
return rel;
}
空间时间: O(1) O(nlogn)
static List阅读全帖
T******g
发帖数: 790
32
来自主题: JobHunting版 - Java里自带的LinkedList类能用吗?
不自己写Node->LinkedList了,直接import java.util.*;?
可以不可以?比如这个切割链表的代码:package chapter2;
import java.util.*;
public class CC2_4{
public static LinkedList partition(LinkedList list,int k){
if(list==null)
return null;
LinkedList less=new LinkedList();
LinkedList noLess=new LinkedList();
for(int i=0;i if(list.get(i) less.add(list.get(i));
}else{
noLess.add(list.get(i));
}
}
less.addAll(noLess);
return less;
}
public static void mai... 阅读全帖
i*********7
发帖数: 348
33
这一题大部分人应该都知道是用BFS解。
我只是想自己试验一下DFS的解。
DFS解如果要避免TLE,重点在于需要截枝和截枝后的答案更新。
这就是我自己新建一个class和对应的HashMap去记录进行截枝。
我的观念是这个样子的,在遇到重复出现过的节点单词的时候,首先考虑的是这个节点
往下遍历过后是否出现过解,如果没有的话只有两种情况:1,这个节点往下走是没有
解的。(在不变回去的情况下)2.变回去了。 这种情况下都当做无效访问往上一层走。
如果有的话,就比较该节点之前有解的情况下它所居的递归层数是否比当前重复访问的
时候深,如果否,则不更新,如果是,则根据层数差来修正结果。这相当于把之前遍历
过的结果默认放在这一层下面了。
好吧,问题来了。。这个解只能过leetcode 80%的cases。在一个字典很大的case中比
Expected answer多了1. 有没有人能告诉我听我的代码或者逻辑问题出在哪儿了?=。=
class DataSet{
int level, res;
DataSet(){
level = 0;
... 阅读全帖
S*******C
发帖数: 822
34
来自主题: JobHunting版 - lintcode subarray sum 怎么做?
Subarray Sum
20%
Accepted
Given an integer array, find a subarray where the sum of numbers is zero.
Your code should return the index of the first number and the index of the
last number.
Example
Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].
http://www.lintcode.com/en/problem/subarray-sum/
下面的解法总是在第16个test case Memory Limit Exceeded
public ArrayList subarraySum(int[] a) {
ArrayList res = new ArrayList();
//a map between sum and index
Has... 阅读全帖
h*****y
发帖数: 298
35
来自主题: JobHunting版 - 发一个Startup的面经 - Affirm
recursion比较robust但是短时间内不好写. 因为你没有可以用的语法树,所以要花力
气来tokenize. 只有用Stack才有希望在规定时间里写完,虽然这样的程序完全没有用。
public class EvalExpression {
private Stack results = new Stack();
private Stack operators = new Stack();
private Stack> vars = new Stack >>();
private static final String ADD = "add";
private static final String MULT = "mult";
private static final String LET = "let";
private static final String LPAREN... 阅读全帖
S***w
发帖数: 1014
36
来自主题: JobHunting版 - 问个fb onsite题目
请 斧正 感觉有点over kill
public List> findSum(int[] candidates, int k, int target) {
List> ret = new ArrayList>();
if (candidates.length == 0) return ret;
Arrays.sort(candidates);
List path = new ArrayList();
dfs(candidates, 0, k, target, path, ret);
return ret;
}
private void dfs(int[] candidates, int depth, int k, int target, List<
Integer> path, List> ret) {
... 阅读全帖
s******3
发帖数: 61
37
来自主题: JobHunting版 - 问道leetcode的题:Combination Sum II
相对于I来说只是一个很小的修改就可以了,
不过发现现在过不了OJ,总是提示TLE,
大家帮忙看看是什么问题?
谢谢!
public List> combinationSum2(int[] candidates, int target) {
List list = new ArrayList();
List> res = new ArrayList>();
Arrays.sort(candidates);
dfs(candidates, target, 0, list, res);
return res;
}

public void dfs(int[] candidates, int target, int start,
List list, List> res) {

... 阅读全帖
n*******e
发帖数: 37
38
来自主题: JobHunting版 - 求问一题G家的面经
给一个directed graph,要求打印出所有的环。
不知道我的Java code可行吗? 我用DFS traverse, 同时记住现在的path, 当侦测到
back edge时, 就打印出path中的cycle部分。
public void printCyclesInDirectedGraph(int n, int[] edges) {
List> graph = new ArrayList>(n);
for (int i = 0; i < n; i++)
graph.add(new LinkedList());
for (int [] e : edges)
graph.get(e[0]).add(e[1]);
int[] visited = new int[n];
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
List阅读全帖
l***i
发帖数: 1309
39
来自主题: JobHunting版 - T家在线测试面经,感觉好难啊
Here is an idea and a proof sketch.
Solution: (c*a, c*b) for cubic numbers a and b and integer c >= 1. It is
easy to see those are solutions, but takes a proof to show that they are the
only solutions.
Observation: if (a,b) is a solution, then (k*a, k*b) is also a solution, for
integer k >= 1.
Claim: If (a, b) is a solution, then both a and b must be cubic numbers.
Now let's prove that claim. W.l.o.g. we may assume gcd(a,b) = 1, and we may
also assume that a is not a cubic number. Let p be a pri... 阅读全帖

发帖数: 1
40
来自主题: JobHunting版 - 请教一个关于java comparator的问题
打算用treeset实现一个最小堆。可是用了自己写的comparator以后,大数(如10000)
被认为是不同的数,因此不能去重复。请问为什么呢?
public class Test {
public static void main(String[] args) {
Set treeset = new TreeSet<>(new MyComparator());
Integer[] array = new Integer[args.length];
for (int i = 0 ; i < args.length ; i ++ ) {
array[i] = Integer.valueOf(args[i]);
treeset.add(array[i]);
}
for (Integer i : treeset) {
Syst... 阅读全帖

发帖数: 1
41
来自主题: JobHunting版 - 贡献一个最近电面题目
根据楼上的思想写了两个版本,不知道对不对
单调栈:
List findMinUnsortedSubArray(int[] nums) {
List res = new ArrayList<>(2);
if (nums == null || nums.length == 0) return res;
int n = nums.length;
Stack s = new Stack<>();
int max = Integer.MIN_VALUE;
s.push(-1);
for (int i=0; i while (!s.isEmpty() && nums[i] < max && nums[i] < nums[s.peek()]
) {
s.pop();
}
if (nums[i]... 阅读全帖

发帖数: 1
42
来自主题: JobHunting版 - 来做道题!
636. Exclusive Time of Functions
Input:
n = 2
logs =
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3, 4]
这道题目,很有意思。
单线程求每个函数执行的总时间。
一共有
start end start end
start start end end
两种情况。
需要注意,开始时间是 第i秒的开始,结束时间是第i秒的最后。
class Solution {
public int[] exclusiveTime(int n, List logs) {
Stack stack = new Stack<>();
int[] res = new int[n];
String[] sa = logs.get(0).split(":");
int prev = Integer.parseInt(sa[2]);
stack.push(Integer.parse... 阅读全帖
w*****n
发帖数: 353
43
来自主题: Java版 - 最近碰到的笔试题
1.
A binary gap within a positive integer N is any maximal sequence of
consecutive zeros that is surrounded by ones at both ends in the binary
representation of N.
For example, number 9 has binary representation 1001 and contains a binary
gap of length 2. The number 529 has binary representation 1000010001) and
contains two binary gaps: one of length 4 and one of length 3. The number 20
has binary representation 10100 and contains one binary gap of length 1.
The number 15 has binary representati... 阅读全帖
k****i
发帖数: 101
44
来自主题: Java版 - 今天碰到一个面试题

public static int solveDuplicateElement(Integer[] ar) {
int pos = 0, len = ar.length, bigO = 0;
while (pos < len) {
int slot = ar[pos];
if (slot == pos || slot == len) {
// being in place or invalidated
++pos;
} else {
int next = ar[slot];
if (next == slot) {... 阅读全帖
f**********3
发帖数: 295
45
在做leetcode LRU,自己写个LinkedList没问题,但是想用java的实现,遇到困难了...
比如
HashMap map = new HashMap();
Integer key = new Integer(3);
map.put(key, 10);
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(key); //同样的key object,加到linked list里面
ll.add(10);
// the list should be 1->3->10 now
int keyToLookUp = 3;
然后怎样可以在map里拿到原来的key object reference然后把ll里面对应的node删除
呢? 就是HashMap里需要给key的value,找key的object reference。 LinkedList里需
要给node object reference,然后从list里删掉。我知道大... 阅读全帖
N***m
发帖数: 4460
46
来自主题: Programming版 - 问题一枚
上帝造我的时候肯定是让我脑袋先着地的。
两个callable,一个返回100,一个返回200,
还有一个runnable3,用于cyclicbarrier的action.
程序运行到runnable3的System.out.println("barrier action executed!"),
似乎就悬在那里了。似乎在等什么,或者死循环?
[Main.java]
public class Main {
static List> tasks = new ArrayList Integer>>();
public static void main(String[] args) throws InterruptedException,
ExecutionException {

CyclicBarrier barrier = new CyclicBarrier(2, new Runnable3()
);
Callabl... 阅读全帖
l*******G
发帖数: 1191
47
来自主题: Computation版 - fortran90 奇怪的格式问题
对于自己定义的数据(结构)的成员打印格式居然出问题。有兴趣的看一下为何主程序
最后两行打印结果不一样?gfortran and ifort give same results on linux.
=====save to test.f90, and then "ifort test.f90" and then "./a.out" to run=====
module numz
integer, parameter:: b8 = selected_real_kind(14)
integer,allocatable :: a_gene(:),many_genes(:,:)
end module
module galapagos
use numz
... 阅读全帖
l*******G
发帖数: 1191
48
来自主题: Computation版 - fortran90 奇怪的格式问题
对于自己定义的数据(结构)的成员打印格式居然出问题。有兴趣的看一下为何主程序
最后两行打印结果不一样?gfortran and ifort give same results on linux.
=====save to test.f90, and then "ifort test.f90" and then "./a.out" to run=====
module numz
integer, parameter:: b8 = selected_real_kind(14)
integer,allocatable :: a_gene(:),many_genes(:,:)
end module
module galapagos
use numz
... 阅读全帖
i****g
发帖数: 3896
49
http://blog.sina.com.cn/s/blog_c24597bf0101b871.html
致谢:I would like to thank Prof. Shing-Tung Yau for suggesting the title of

this article, Prof. William Dunham for information on the history of the
Twin Prime Conjecture, Prof. Liming Ge for biographic information about
Yitang Zhang, Prof. Shiu-Yuen Cheng for pointing out the paper of
Soundararajan cited in this article, Prof. Lo Yang for information about
Chengbiao Pan quoted below, and Prof. Yuan Wang for detailed information on
result... 阅读全帖
H****S
发帖数: 1359
50
来自主题: JobHunting版 - Facebook Phone Inteview + 流程请教
import java.util.ArrayList;
public ArrayList> uniqueSet(int[] a, int i) {
ArrayList retList = new ArrayList>();
if (i < 0) {
retList.add(new ArrayList);
return retList;
}
for (ArrayList list : uniqueSet(a, i - 1)) {
retList.add((ArrayList)(list.clone())); //ugly
list.add(new Integer(a[i]));
retList.add((ArrayList)(list.clone())); //ugly
}
return retList;
}
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)