H****S 发帖数: 1359 | 1 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;
} |
|
r******r 发帖数: 700 | 2 那个从数组查找 Majority Element 的问题,好像有更简洁的 O(n) 算法啊。 难道我
错在哪里?
下面我写的一个, 和原始的 Moore’s Voting Algorithm 的 O(n) 算法:
1. 用 hashtable. 这个的 wost case 是 O(n), best case 好像是 O(n/2)
public static Integer findMajorElement(int[] array){
Map number = new HashMap();
int size = array.length;
for(int i=0; i
Integer v = number.get(array[i]);
if( v == null ){
v = 1;
... 阅读全帖 |
|
m***o 发帖数: 41 | 3 A non-empty zero-indexed array A consisting of N integers is given. A pair
of integers (P, Q) is called K-complementary in array A if 0 = P, Q < N and
A[P] + A[Q] = K.
For example, consider array A such that
A[0] = 1 A[1] = 8 A[2]= -3
A[3] = 0 A[4] = 1 A[5]= 3
A[6] = -2 A[7] = 4 A[8]= 5
1,8,-3,0,1,3,-2,4,5
The following pairs are 6-complementary in array A: (0,8), (1,6), (4,8), (5,
5), (6,1), (8,0), (8,4). For instance, the pair (4,8) is 6-complementary
because A[4] + A[8] = 1 + 5 = 6.... 阅读全帖 |
|
d********w 发帖数: 363 | 4 应该是对的,我试了一些test cases;
Vector item1 = new Vector();
item1.add(1);
item1.add(2);
Vector item2 = new Vector();
item2.add(3);
Vector> a = new Vector>();
//a.add(item2);
a.add(new Vector());
//a.add(item2);
Flat f = new Flat(a);
while(f.hasNext())
System.out.println(f.next()); |
|
J***e 发帖数: 50 | 5 Write a special singleton class, the class has one integer field,
make sure only one instance of this class will be instantiated for
the same value of the integer field.
以下是我写的, 但是有个问题,这样instance不是static, getInstance()因为用到
instance也不能是static,可以吗?这样解对吗?另外,当singleton object destruct的时候,应该把 value从hashmap里删除。怎么实现这个呢?用finalize()?
import java.util.HashMap;
public class SingletonInteger {
private Integer value;
private SingletonInteger instance;
private static HashMap阅读全帖 |
|
p*****2 发帖数: 21240 | 6 我写了一个你看看对不对。好久没做题了,感觉有些生疏。
ArrayList> all = new ArrayList>();
void dfs(int[] num, int start, ArrayList al)
{
all.add(new ArrayList(al));
int prev=-1;
for(int i=start;i
{
if(num[i]!=prev)
{
al.add(num[i]);
dfs(num,i+1,al);
al.remove(al.size()-1);
prev=num[i];
}
}
}
... 阅读全帖 |
|
w***o 发帖数: 109 | 7 HashMap> map = new HashMap
Integer>>();
int[] max = new int[n]; // n = total number of colors
for(Vertex v : G.V()) {
for(Vertex w: v.adj()) {
if(w.color != v.color) {
if(map.containsKey(w.color)) {
HashSet set = map.get(w.color);
if(!set.contains(v.color)) {
set.add(v.color);
max[w.color]++;
}
} else {
Ha... 阅读全帖 |
|
c*****a 发帖数: 808 | 8 大牛快来看看
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find
all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending
order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2... 阅读全帖 |
|
s****0 发帖数: 117 | 9 大概是这个意思. 我没理解最后两种情况怎么处理,好像结果不确定。
package myutil;
import java.util.Arrays;
public class FlexSearch {
final Integer[] data;
final boolean asc;
public FlexSearch(Integer[] data) {
if (data == null || data.length < 2)
throw new java.lang.NoSuchFieldError();// whatever.
this.data = data;
asc = data[0] - data[1] < 0;
}
private int getIdx(int val) {
return Arrays.binarySearch(data, val);
}
public int getLTMax(int val) {
int... 阅读全帖 |
|
h********0 发帖数: 74 | 10 public ArrayList> combine_iterative(int n, int k) {
ArrayList> result = new ArrayList>
();
ArrayList list;
int end = n-k+1;
for(int i=1; i<= end; i++){
list = new ArrayList();
list.add(i);
result.add(list);
}
ArrayList listTmp;
for(int i = 1; i< k; i++){
//int len = result.size();
for(int j = result.size(); j>0; j-- ){
list = ... 阅读全帖 |
|
h********0 发帖数: 74 | 11 --- score = 所有出发比自己晚但是到达比自己早的racer数量之和, ---
是那些racer的个数, 不是他们的score 之和, 对不?
my idea is:
1 create a map and listA that makes the start
time ordered ,
2 travel the startTime, from big to small, and create a listB to store the
endTime in order
2.1 to the first Race whose startTime is biggest, insert his endTime in
the listB, the position is 0, so the score is 0
2.2 to every Racer, binary search his endTime in listB, the position is
his score.
code:
public boolean ... 阅读全帖 |
|
w****x 发帖数: 2483 | 12 public class RacerScore
{
static public class Segment
{
int id = -1;
int beg = 0;
int end = 0;
Segment(int i, int b, int e)
{
id = i;
beg = b;
end = e;
}
}
static public class comp implements Comparator
{
public int compare(Segment s1, Segment s2)
{
return s1.beg - s2.beg;
}
}
static public void getScores(Segment[] segs, int ... 阅读全帖 |
|
e***n 发帖数: 42 | 13 来个java的:
public static void twoProduct(int n){
Map> m = new HashMap
String>>();
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if (m.containsKey(i*j)){
m.get(i*j).add(Integer.toString(i) + '*' + Integer.toString(
j));
}
else{
ArrayList tmp = new ArrayList();
tmp.add(Integer.toString(i) + '*' + Integer.toString(j));
... 阅读全帖 |
|
e****e 发帖数: 418 | 14 我的解法,总觉得用Java写还有更好的。
public class LongestConsecutiveSubsequence {
private int longestLCSStartPos = 0;
private int longestLCSCount = 0;
public Vector lcs(Vector input) {
if( input == null || input.size() < 2 )
throw new RuntimeException( "Invalid input." );
lcs( input, 0 );
if ( longestLCSCount < 2 )
throw new RuntimeException( "lcs length is less than 2." );
Vector r = new Vector阅读全帖 |
|
L*******e 发帖数: 114 | 15 综合楼上的解答,我试着写了一个。
/**
* find all the prime up to n(including n)
*/
public static List findPrimes(int n){
List res = new ArrayList();
if (n <= 1) return res;
// create a boolean array to track whether a number
// is a prime or not
boolean[] primeTrack = new boolean[n+1];
for (int i = 2; i <= n; i++){
primeTrack[i] = true;
}
int upper = (int)Math.sqrt(n);
for (int i = 2; i <= upper; i++){
for (int j = i * i; j <= n; j +=... 阅读全帖 |
|
s********x 发帖数: 914 | 16 Time Limit Exceeded了
但感觉已经cache了intermediate result了
是不是这题assume只是26个小写英文字母呢,如果是的话,用array就更快?
贴一下code
class AnagramString {
boolean visited;
String str;
private Map map = null;
AnagramString(String s) {
this.str = s;
}
boolean isAnagram(String s) {
if (this.map == null) {
this.map = new HashMap(this.str.length());
for (int i = 0; i < this.str.length(); i++) {
char c =... 阅读全帖 |
|
j**********m 发帖数: 51 | 17 A non-empty zero-indexed array A consisting of N integers is given.
A pair of integers (P, Q) is calledK-complementary in array A if 0 ≤ P, Q <
N and A[P] + A[Q] = K.
For example, consider array A such that:
A[0] = 1 A[1] = 8 A[2]= -3
A[3] = 0 A[4] = 1 A[5]= 3
A[6] = -2 A[7] = 4 A[8]= 5
The following pairs are 6-complementary in array A: (0,8), (1,6), (4,8), (5,
5), (6,1), (8,0), (8,4).
For instance, the pair (... 阅读全帖 |
|
s**x 发帖数: 7506 | 18 I believe leetcode provide a simple solution which may overflow, but
actually it is correct.
You can simply use unsigned int. I would think even an int would work as
well.
bool isPalindrome(int num) {
if(num < 0) return false;
unsigned int oldNum = num;
unsigned int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev == oldNum);
}
The following is from gnu comments.
13.2.1 Basics of Integer Overflow
In languages like C, unsigned integer overflow ... 阅读全帖 |
|
a**********0 发帖数: 422 | 19 如果有duplicates 用下面这个算法
import java.util.*;
public class PermutationsII {
public ArrayList> permuteUnique(int[] num) {
Arrays.sort(num);
ArrayList> myResult = new ArrayList
Integer>>();
ArrayList temp1 = new ArrayList();
for(int i = 0; i<= num.length-1; i++)
temp1.add(num[i]);
myResult.add(temp1);
while(nextPermutation(num)){
... 阅读全帖 |
|
S******1 发帖数: 216 | 20 来自主题: JobHunting版 - FB 面筋
Minimum
写一个考虑顺序的inverted indexing做法
//7:10
//给一个string,比如UAXXBAUB,给一个pattern,比如AB,返回包含pattern的最短
substring,结果是AUB
int findMinWindow(String s, String p) {
if (s == null || p == null || s.length() < p.length())
return -1;
if (p.isEmpty())
return 0;
Map> indexMap = new HashMap
Integer>>();
for (int i = 0; i < s.length(); i++) {
if (!indexMap.containsKey(s.charAt(i)))
indexMap.put(s.charAt(i), new Arra... 阅读全帖 |
|
a**********0 发帖数: 422 | 21 java代码
import java.util.*;
public class PermutationsII {
public ArrayList> permuteUnique(int[] num) {
Arrays.sort(num);
ArrayList> myResult = new ArrayList
Integer>>();
ArrayList temp1 = new ArrayList();
for(int i = 0; i<= num.length-1; i++)
temp1.add(num[i]);
myResult.add(temp1);
while(nextPermutation(num)){
ArrayList<... 阅读全帖 |
|
c********6 发帖数: 33 | 22 List> factorCombination(int n) {
List> result = new ArrayList>();
dfs(n, 2, result, new ArrayList());
return result;
}
void dfs(int n, int start, List> result, List sub
) {
if(n == 1) {
result.add(new ArrayList(sub));
return;
}
for(int i = start; i <= n; i++) {
if(n % i != 0) continue;
if(i == n && sub.i... 阅读全帖 |
|
c********6 发帖数: 33 | 23 List> factorCombination(int n) {
List> result = new ArrayList>();
dfs(n, 2, result, new ArrayList());
return result;
}
void dfs(int n, int start, List> result, List sub
) {
if(n == 1) {
result.add(new ArrayList(sub));
return;
}
for(int i = start; i <= n; i++) {
if(n % i != 0) continue;
if(i == n && sub.i... 阅读全帖 |
|
r*******e 发帖数: 971 | 24 楼主你挂得不冤。我是面试官也会挂掉你的。
1)数组是否排序要提前问面试官,自己想当然属于lacking communication
然后面试官在你声明low 与high 两个变量时候没有提醒你么?还是说你没有提前跟面
试官说你的方法??
2)速度不够快。否则第二题即使只有15分钟也能写个大概的。
3)如果楼主不服,我们现在来说代码
1:if(nums==null || nums.length<2) return false;
这里可能是需要抛出异常的 throw new NullPointerException();
2:if( (nums[low]+nums[high]) == sum ){ return true;}。。。
更好点的办法是在循环外面声明一个变量temp,temp = nums[low]+nums[high]
否则每次循环都得多算一次。
3.HashMap map = new HashMap();
应该写成Map map = new HashMap<>();... 阅读全帖 |
|
l*****a 发帖数: 14598 | 25
发信人: reclapple (加菲鲸), 信区: JobHunting
标 题: Re: 最新L家面经
发信站: BBS 未名空间站 (Mon Nov 10 19:41:25 2014, 美东)
楼主你挂得不冤。我是面试官也会挂掉你的。
1)数组是否排序要提前问面试官,自己想当然属于lacking communication
然后面试官在你声明low 与high 两个变量时候没有提醒你么?还是说你没有提前跟面
试官说你的方法??
2)速度不够快。否则第二题即使只有15分钟也能写个大概的。
3)如果楼主不服,我们现在来说代码
1:if(nums==null || nums.length<2) return false;
这里可能是需要抛出异常的 throw new NullPointerException();
==> 这个真的需要吗?我认为不需要吧。
3.HashMap map = new HashMap();
应该写成Map map = new HashMap<>()... 阅读全帖 |
|
y**********a 发帖数: 824 | 26 class MinDist{
Map dist;
String separator;
int minDist(String s1, String s2, Map>map) {
dist=new HashMap<>();
Listl1,l2;
if ((l1=map.get(s1))==null||(l2=map.get(s2))==null) return -1;
int rel=Integer.MAX_VALUE;
for(int i1=0,i2=0;i1
int v1=l1.get(i1),v2=l2.get(i2),dif=v1-v2;
rel=Math.min(rel,Math.abs(dif));
if (rel==1) return 0;
el... 阅读全帖 |
|
I*******g 发帖数: 7600 | 27 第一题:
public static String shortestString(String s, String p){
String result = null;
if (s== null || p == null || p.length() >s.length()) return null;
if (s.length() ==0 || p.length() ==0) return "";
Map indexMap = new HashMap();
List indexList = new ArrayList();
int min = s.length() +1;
for(int i =0 ;i < s.length(); i++){
for(Integer x: indexList){
... 阅读全帖 |
|
I*******g 发帖数: 7600 | 28 第一题:
public static String shortestString(String s, String p){
String result = null;
if (s== null || p == null || p.length() >s.length()) return null;
if (s.length() ==0 || p.length() ==0) return "";
Map indexMap = new HashMap();
List indexList = new ArrayList();
int min = s.length() +1;
for(int i =0 ;i < s.length(); i++){
for(Integer x: indexList){
... 阅读全帖 |
|
r*******e 发帖数: 971 | 29 看你们贴,那我也贴一个。
public class Solution {
public List findSubstring(String S, String[] L) {
if (S==null||S.length()==0) return Collections.emptyList();
Map map = new HashMap();
Map map_full;
List result= new ArrayList();
for (String s:L){
if (map.containsKey(s)) map.put(s,map.get(s)+1);
else map.put(s,1);
}
int len= L[0]... 阅读全帖 |
|
z***b 发帖数: 127 | 30 Given a nested list of integers, returns the sum of all integers in the list
weighted by their depth
For example, given the list {{1,1},2,{1,1}} the function should return 10 (
four 1's at depth 2, one *2 at depth 1)
Given the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1,
one 4 at depth 2, and *one 6 at depth 3)
public int depthSum (List input)
{
//Implement this function
}
/**
* This is the interface that allows for creating nested lists. You should
not i... 阅读全帖 |
|
z***b 发帖数: 127 | 31 Given a nested list of integers, returns the sum of all integers in the list
weighted by their depth
For example, given the list {{1,1},2,{1,1}} the function should return 10 (
four 1's at depth 2, one *2 at depth 1)
Given the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1,
one 4 at depth 2, and *one 6 at depth 3)
public int depthSum (List input)
{
//Implement this function
}
/**
* This is the interface that allows for creating nested lists. You should
not i... 阅读全帖 |
|
x*****0 发帖数: 452 | 32 下面这道题是在版上看到的一道题。请问大家有什么想法吗?
设计一个Map,满足下面的时间复杂度。
add: O(1) deletion: O(1) lookup: O(1) clear:O(1) iterate: O(number of
elements)。
提示:
如果我们用randomly accessed array,复杂度如下:
add: O(1) deletion: O(1) lookup: O(1) clear: O(size of array) iterate:
O(size of array)
如果我么用sequential array, 复杂度如下:
add: O(1) deletion: O(number of elements) lookup:O(number of elements)
clear: O(1) iterate:O(number of elements)
所以我们需要把这两个方法整合起来。
对于这题的提示,sequential array指的就是link list? 把每一对阅读全帖 |
|
a******f 发帖数: 9 | 33 #include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class NestedInteger {
public:
NestedInteger(int i) : integer(i), isInt(true) {}
NestedInteger(vector l) : nestedList(l), isInt(false) {}
// Return true if this NestedInteger holds a single integer, rather than
a nested list.
bool isInteger() {
return isInt;
}
// R... 阅读全帖 |
|
c*****u 发帖数: 867 | 34 https://leetcode.com/problems/substring-with-concatenation-of-all-words/
这道题我是用的brute force,所以有时候超时。请问有更好的算法吗?
我的code有几次通过了,耗时200ms,我看最终结果里有很多几十毫秒的。请问那是怎
样的算法呢?
public class Solution {
public List findSubstring(String s, String[] words) {
ArrayList result = new ArrayList();
HashMap wordMap = new HashMap();
HashMap wordCover = new HashMap();
int wordsLen = words.lengt... 阅读全帖 |
|
a*o 发帖数: 25262 | 35 帮你找个证明吧, n = 2.
For any positive integers n and x in (0,1) we can define
2n
x^n (1 - x)
^n 1 --- j
f(x) = ---------------- = ---- ) c_j x
n! n! ---
j=n
Here, c_j are integers. For x in (0,1) we have,
0 < f(x) < 1/n!
Now, suppose that positive integers a and b exist such that
2 a
pi = ---
b
Define
n / 2n 2n-2 ... 阅读全帖 |
|
T*****e 发帖数: 361 | 36 该怎么样cast 一个generic的对象啊?
比方说:
SortedSet s0 = new TreeSet();
Object obj o = s0;
...
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
但是这样cast会导致warning:
Type safety: The cast from Object to SortedSet is actually checking
against the erased type SortedSet.
如果:
SortedSet s1 = (SortedSet) o;
s1.add( new Integer(1) );
则在add的时候会有如下的warning:
Type safety: The method add(Object) belongs to the raw type Set. References to
generic type Set should be par |
|
a**e 发帖数: 5794 | 37 我用一个List alMatrix包含另一个List alRow,后者
是一个Integer List。
List alMatrix = new ArrayList(n);
List alRow = new ArrayList(n);
往里面加数据都没问题
alRow.add(i);
alMatrix.add(alRow);
但是取出数据时遇到一个警告:unchecked cast
List alRow1 = alMatrix.get(i);
说右边是List,和左边的List不匹配。
我怎样告诉它取出的是一个List呢?
加个(List)不行。 |
|
N***m 发帖数: 4460 | 38 一小段code。我在学java concurrent,不知道多核电脑上运行会快多少?
是不是和核的数目基本线性的,如果thread_num % #cpu_core==0?
多核会不会自己优化?
==================================================
这段程序寻找给定范围内素数的个数。
[Main.java]
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Main {
public static void main(String[] args) {
List> tasks = new ArrayList
... 阅读全帖 |
|
Y**G 发帖数: 1089 | 39 这个问题问的有点违反人性。实际使用Java的人是不会问这种问题的。
如果要硬说,Java编译器把"Integer i = 100"翻成"i = Integer.valueOf(100)",就
是一个简单的autoboxing的过程。
编译器才不管Integer.valueOf是怎么实现的。
下面是JDK 6中Integer.valueOf的实现:
=================
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
=================
所以Integer.valueOf(n)的返回值,对于n是否是8位或者一下和以上的行为是不同的。 |
|
w*********n 发帖数: 439 | 40 我初始化一个ArraList ,然后向这个ArrayList里面加入2个Integer,当我试
图改变其中一个Integer的值的时候,
居然改不掉。请问怎么回事?
ArrayList list = new ArrayList<>();
Integer age1 = 20;
Integer age2 = 20;
list.add(age1);
list.add(age2);
//change age2 int value to 30
age2 = 30;
System.out.println(list.get(0));
System.out.println(list.get(1));
————————————————————————————
Output:
20
20
请问为什么改不掉age2这个Integer的值。 |
|
b***i 发帖数: 3043 | 41 public static ThreadLocal instance;
public static void setHDC(Integer i){
instance=new ThreadLocal();
instance.set(i);
System.out.println("Program "+"in "+ GThreadLib.get()+" setting ="+i
);
}
public static Integer getHDC(){
Integer h=instance.get();
System.out.println("Program "+" in "+GThreadLib.get()+" getting ="+h
);
}
还是必须 public static final ThreadLocal instance=new
ThreadLocal();
我好像在前一种实... 阅读全帖 |
|
C******t 发帖数: 72 | 42 来自主题: Statistics版 - 问一下证明 E(X+Y)=integ[integ[(X+Y)f(x,y)dxdy]]=integ[integ(x)f(x,y)dxdy]+inte[integ(y)
f(x,y)dxdy]=integ[xf(x)dx]+integ[yf(y)dy]=E(x)+E(y) |
|
h********l 发帖数: 67 | 43 1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
2 . 写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的一个。
3. 预处理器标识#error的目的是什么?
4. 嵌入式系统中经常要用到无限循环,你怎么样用C编写死循环呢?
5. 用变量a给出下面的定义 a) 一个整型数(An integer) b)一个指向整型数的指针
( A pointer to an integer) c)一个指向指针的的指针,它指向的指针是指向一个
整型数( A pointer to a pointer to an intege)r d)一个有10个整型数的数组(
An array of 10 integers) e) 一个有10个指针的数组,该指针是指向一个整型数的
。(An array of 10 pointers to integers) f) 一个指向有10个整型数数组的指针
( A pointer to an array of 10 integers) g) 一个指向函数的指针,该函数有一
个整型参数并返回一个整型数(A pointer to a fun |
|
a*u 发帖数: 97 | 44 我的想法和pathfinding比较像,use priority queue with counts and position.
If (len-1)/(count-1)>=d is true, proceed to generate the new string.
引用roufoo的例子,初始化priority queue是
key / count / position
C 3 Integer.MIN_VALUE
B 2 Integer.MIN_VALUE
D 2 Integer.MIN_VALUE
E 2 Integer.MIN_VALUE
A 1 Integer.MIN_VALUE
E 1 Integer.MIN_VALUE
Maintain a cur reference, every time find max(count) with cur-position>=d (
customize comparable function). After printing a key, decrement its count (
remove if count reaches zero |
|
j**l 发帖数: 2911 | 45 写成sizeof(integers)/sizeof(int)还是错的。函数has_num的第一个参数是int *
integers,是把它当指针而不是数组名处理的,这样sizeof(integers)的结果总是4。
如果这样写
int integers[100];
那么sizeof(integers)的结果就是400了
如果是我,就把数组大小用参数传入
bool has_num(int integers[], int n, int target) |
|
P********l 发帖数: 452 | 46 来自主题: JobHunting版 - 请教个题目 这是那个“数组里两个数和为给定值”问题的扩展版。
觉得枚举每个组合就挺好。比如,52张牌选三张的组合是
1, 1, 1
1, 1, 2
1, 1, 3
。。。
1, 1, 13
1, 2, 2
1, 2, 3
。。。
11, 13, 13
12, 12, 12
12, 12, 13
12, 13, 13
13, 13, 13
如果要选4张牌,使其和为28,就可以通过前三张算出第四张,然后检查是否符合要求。
代码:
http://code.google.com/p/sureinterview/source/browse/test/test1/CombinationTest.java#120
public void testNumComb2() {
// list all combinations of c(7,3)
int suit = 4;
int rank = 13;
int takeN = 3; //4-1=3。
int totalNum = 28;
List ... 阅读全帖 |
|
g***s 发帖数: 3811 | 47 i dont know why Hashmap doesn't work. maybe i mis-
understand the question?
void findPairs(int[] array, int sum) {
HashMap map = new HashMap();
for (int num : array){
if (map.containsKey(sum - num)){
for (int i = 0; i< map.get(sum-num); i++){
System.out.println(num + "," + (sum-num));
}
}
map.put(num, map.containsKey(num)? map.get(num)+1 : 1);
}
... 阅读全帖 |
|
b**********r 发帖数: 91 | 48 Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖 |
|
b**********r 发帖数: 91 | 49 Run the ultimate example around 400 ms
here is my code
4, 13, 5, 4, 13, 6, 8, 12, 16, 9, 13, 13, 13, 16, 9, 2, 6, 5,
0, 4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167,
4, 17, 22, 26, 39, 45, 53, 65, 81, 90, 103, 116, 129, 145, 154, 156,
162, 167, 13, 18, 22, 35, 41, 49, 61, 77, 86, 99, 112, 125, 141, 150,
152, 158, 163, 5, 9, 22, 28, 36, 48, 64, 73, 86, 99, 112, 128, 137, 139,
145, 150, 4, 17, 23, 31, 43, 59, 68, 81, 94, 107, 123, 132, 134, 140,
145, 13, 19, 27, 39... 阅读全帖 |
|
b*******h 发帖数: 53 | 50 int compare(int a, int b)
{
String x= Integer.toString(a)
String y = Integer.toString(b);
if (Integer.parseInt(x+y)< Integer.parseInt(x+y))
return -1;
else if(Integer.parseInt(x+y)= Integer.parseInt(x+y))
return 0;
else return 1;
}
void Quick_Sort(int[] a, int p, int r)
{
if(p
{
int q = partition(a,p,r);
Quick_Sort(a,p,q-1);
Quick_Sort(a,q+1,r);
}
}
int Partition(int[] a, int p, int r)
{
int key = a[r];
int i = p-1;
i... 阅读全帖 |
|