由买买提看人间百态

topics

全部话题 - 话题: println
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
f****u
发帖数: 12
1
来自主题: Java版 - java String
class Str {
String str;
}
class HelloWorldApp {
public static void main(String[] args) {
Str s=new Str();
Str t=new Str();
s.str="1";
t.str="2";
System.out.println(t.str);
t=s;
System.out.println(t.str);
s.str="3";
System.out.println(t.str);
}
}
The result is:
2
1
3
If u use "=" to assiagn class2 to class1, both 1 and 2 contain the same
reference--point to the same object. This phenomenon is called "aliasing
t******l
发帖数: 6
2
来自主题: Java版 - 两个很基本的JAVA问题
正在准备SCJP 看见2个这样的问题
1.请问在JAVA里可以直接PRINT出二进制数么?
比如想知道: -64>>2 最后结果用二进制如何表示的
可以通过System.out.println 或者类似的语句输出二进制数么?
2. Evaluation Order
比如有这么一段程序:
class orderTest
{
public static void main(String[] args)
{
int[] a = {4,4};
int b = 1;
a[b]=b=0;
System.out.println("a["+b+"] is " + a[b]);
System.out.println("b is "+b);
}
}
最后运行结果是:
a[0] is 4
b is 0
请问Evaluation order到底是怎么样子的,最后为什么会得出这样的结果呢?
Thanks..
c*****n
发帖数: 1
3
来自主题: Java版 - jdbc连接数据库出现的问题
import java.sql.*;
public class MakingTheConnection {
public static void main(String[] args) {
try {
Class.forName("COM.cloudscape.core.JDBCDriver");
String sourceURL = "jdbc:cloudscape:C:/Music";
Connection databaseConnection =
DriverManager.getConnection(sourceURL);
System.out.println("Connection established successfully!");
databaseConnection.close();
}
catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
}
catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
就这么一个小程序,我在clas
g****n
发帖数: 18
4
Hello everyone,
I have a question about Java thread sync. Following is part of the code that I
am having trouble with,
while (listening)
{
System.out.println("Waiting for a client...");
new MultiThread(group, serverSocket.accept(), h).start();
trackTime = h.getTime();
System.out.println("Track time " + trackTime);
System.out.println("Load: " + group.activeCount());
}
In it, h is a structure with public function getTime(). The MultiThread will
m
g****n
发帖数: 18
5
来自主题: Java版 - Follow up Re: Simp question on
At first, thanks to everyone who helped me out on the previous question.
Now I encounter a new problem. The code is listed below for the convenience
while (listening)
{
System.out.println("Waiting for a client...");
MultiThread mthread = new MultiThread(group, serverSocket.accept(), h);
mthread.start;
mthread.join();
trackTime = h.getTime();
System.out.println("Track time " + trackTime);
System.out.println("Load: " + group.activeCount
N*D
发帖数: 3641
6
来自主题: Java版 - scala - I 服了 U
这个也可以
def main(args:Array[String]) = println("hello")

and these both compile fine:
def main(args:Array[String]) {
println("hello")
}
def main(args:Array[String]) = {
println("hello")
}
h*****0
发帖数: 4889
7
no, inner class is different.
try:
class A {
static {
System.lout.println("initializing A");
}
static class InnerA {
static {
System.out.println("initializing InnerA");
}
}
}
public class B {
public static void main(String[] args) {
System.out.println("start main");
A a = new A();
A.InnerA ia = new A.InnerA();
}
}
q***s
发帖数: 2243
8
代码见下面,为什么?
多谢各位!
public class TwoThreads {
static Thread laurel, hardy;
static final Object[] locker = new Object[0];
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
q*********u
发帖数: 280
9
【 以下文字转载自 JobHunting 讨论区 】
发信人: yinyueyouge (隐约有歌), 信区: JobHunting
标 题: Re: java enclosure是什么-今天被hm问倒了
发信站: BBS 未名空间站 (Fri Oct 22 09:27:57 2010, 美东)
感觉对方是在问 Closure。
这个是 Java 对 Lambda 表达式的实现。Java 7 已经确定在语法上支持这个。
Java 6或者以前的版本只能靠 interface + anonymous class 来实现。
若是做过 functional programming(比如haskell),应该对 Lamdba 表达
式比较熟悉。
从C++的角度来看,就是 function pointer,但是它是 Strongly Typed。
举例代码来说明。假设要对二叉树遍历,代码很好写,比如:
void inOrder(Tree tree) {
if (tree != null) {
inOrder(tree.getLeft());
System.out.p... 阅读全帖
b******g
发帖数: 669
10
来自主题: Java版 - 问HashSet的问题?
All set cares uniqueness and equals() determine whether two object are
identical.
但是我test 了下
import java.util.HashSet;
public class testT {
private String name;
public testT(String name){
this.name=name;
}
public boolean equals(Object o){
if(!(o instanceof testT))return false;
testT person=(testT)o;
return person.name.equals(this.name);
}
public static void main(String[] args){
HashSet hs=new HashSet();
testT p1= new testT("jing");
... 阅读全帖
d******y
发帖数: 244
11
I learn from a tutorial. I try run the example. errors happen.
something about session and hibernate.cfg.xml.
Here are files.
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
// This ste... 阅读全帖
j*a
发帖数: 14423
12
来自主题: Java版 - java这个是什么逻辑?
:~$ javac -version
javac 1.6.0_32
:~$ cat a.java
import java.lang.Integer;
class a {
public static void main(String args[]) {
int a=1000, b=1000;
System.out.println(a==b);
Integer c=1000, d=1000;
System.out.println(c==d);
Integer e=100, f=100;
System.out.println(e==f);
}
}
:~$ javac a.java
:~$ java -version
java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Ja... 阅读全帖
r********r
发帖数: 208
13
来自主题: Java版 - use >>>= with short
在下面的程序中,对short类型s,无符号右移并赋值(见最后引用,这个操作中间经历
int, right shift, truncation 3个过程)。难道结果不应该还是short类型吗?可是
结果-1是int,32位.没想明白,很不爽。谁能解惑,指点迷津?
//: operators/URShift.java
// Test of unsigned right shift.
//import static net.mindview.util.Print.*;
public class URShift {
public static void main(String[] args) {
short s = -1;
System.out.println(Integer.toBinaryString(s));
System.out.println(Integer.toBinaryString(s>>>10) + ": s>>>10");
s >>>= 10;
System.out.println(Integer.toBinaryString(s... 阅读全帖
y****i
发帖数: 12114
14
来自主题: Java版 - 编程新人求助—死循环了
步骤:
1、用户输入文件名和路径;
2、程序检查是否已有该文件存在,
2 .1、如果存在,要求用户输入新文件名和路径,回到2;
2.2 、如果新文件名不存在,则生成该文件,运行下一段程序。
我的代码如下。我知道按照我的代码,只要输入新文件名,那个新文件立刻就存在了,
然后就又得输入新文件名,然后文件又存在了,进入死循环了。
该怎么写才能实现上述步骤?
谢谢指点。
Scanner keyboard=new Scanner(System.in);

//get file name
System.out.println("Please enter file name:");
String fileName=keyboard.nextLine();
//get file path
System.out.println("Please enter path:");
String filePath=keyboard.nextLine();
//creat... 阅读全帖
s****s
发帖数: 628
15
public void withdraw(int amount) {
56 lock.lock();// Acquire the lock
57 try {
58 while (balance < amount) {
59 System.out.println("\t\t\tWait for a deposit")
60 newDeposit.await();
61 }
62
63 balance -= amount;
64 System.out.println("\t\t\tWithdraw " + amount +
65 "\t\t" + getBalance());
66 }
67 catch (InterruptedException ex) {
68 ex.printStackTrace();
69 }
70 finally {
71 // Release the lock
72 }
73 }
书上说:
What will happen if you replace the while loop in lines 59–60 with the
following if
sta... 阅读全帖
n****e
发帖数: 678
16
来自主题: Java版 - 请教如何学习Java
他的意思是说:如果用strategy pattern, codes就是这个样子的:
=====================================================
interface Strategy {
int execute(int a, int b);
}
/** Implements the algorithm using the strategy interface */
class Add implements Strategy {
public int execute(int a, int b) {
System.out.println("Called Add's execute()");
return a + b; // Do an addition with a and b
}
}
class Subtract implements Strategy {
public int execute(int a, int b) {
System.out.println... 阅读全帖
b*********a
发帖数: 723
17
来自主题: Java版 - 今天碰到一个面试题
package com.bruce.test;
public class TestApp {
public static void main(String[] args) {
int[] arr = { 2, 1, 3, 4, 11, 2, 6, 1, 12, 4, 11, 2, 12, 11, 11 };
System.out.println("O(n) space");
OnFindDuplicate(arr);
System.out
.println("\nO(1) space, but duplicated output sometimes like
11");
OoneFindDuplicate(arr);
System.out.println("\nO(1) space, no duplicated output ");
OoneFindDuplicate2(arr);
}
// need a new arr... 阅读全帖
z*******3
发帖数: 13709
18
i really dont think so
public class Test {
public static void main(String[] args) {
Map m = new HashMap();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(t1.hashCode());
System.out.println(t2.hashCode());
System.out.println(t3.hashCode());
... 阅读全帖
c********p
发帖数: 1969
19
leetcode permutation sequence
先谢谢各位大牛!
我知道这个问题有更好的解法,但我想知道:
我想把第k个permutation存在result中, 无论我result是否设为全局的,它都存不下。
当时读到的时候,有存进去,可是接着run之后就没有了。我要怎么保存它呢?
int count;
StringBuffer result = new StringBuffer();
public String getPermutation(int n, int k) {
// Start typing your Java solution below
// DO NOT write main() function
count = 0;
if(n == 0 || k == 0){
return "";
}

StringBuffer sol = new StringBuffer();
permute... 阅读全帖
T***B
发帖数: 137
20
来自主题: Java版 - 工作中遇到的并行处理问题
照着mectite,goodbug二位的思路写了一下,代码如下. 试着跑了一下,运行结果和预
期吻合。我有一个问题:我在PredictRequest.call()里面把current thread cast成
PredictorThread从而拿到predictor object. 还有更好的办法把predictor (inside
the thread) 和callable联系起来吗?
Predictor.java
public class Predictor {
private String name;

public Predictor(String name) {
// heavy lifting stuff.
this.name = name;
System.out.println("Created predictor " + name);
}
public synchronized String predict(String input) throws
InterruptedExcept... 阅读全帖
i**w
发帖数: 883
21
Pattern p = Pattern.compile("
>(.+?)
");
Matcher m = p.matcher(input);

if (m.matches()) {
int cnt = m.groupCount();
System.out.println(cnt);

String g1 = m.group(1);
System.out.println(g1);

String g2 = m.group(2);
System.out.println(g2);
}
p*********s
发帖数: 1952
22
来自主题: Java版 - 新手问问这个错在哪儿?
//刚学java, 下面代码出错,请问下错在哪儿里,怎么改?
//RefDemo04.java:65: error: cannot find symbol
// System.out.println(bk.getPerson().getName());
^
// symbol: method getName()
// location: class Person
// 1 error
class Person {
private String name;
private int age;
private Book book;

public Person(String n, int a) {
this.setName(n);
this.setAge(a);
}
public void setBook(Book b) {
book = b;
}
public vo... 阅读全帖
x******m
发帖数: 736
23
来自主题: Java版 - Spring菜鸟求救
跟着网上的tutorial学习,一模一样的code,总是出错。
第一个class
package com.springinaction.springidol;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}

}
第二个class
package com.springinaction.springidol;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
... 阅读全帖
h**********c
发帖数: 4120
24
来自主题: Linux版 - Report a bug not sure Kernel or JVM
Centos distro,
Compile the following Java program, the program freezes before "slept".
"top" shows very high CPU utilization.
Ctrl-C not work. kill -9 pid in another console.
Not sure JVM problem or kernel problem, both Java 6 and 7 have this problem.
Ask for blind sweep.
Other servers have similar problem. Reboot server, the problem gone.
Suspect it is because of leap second. Run "top", check your servers!
public static void main (String [] args) {
System.out.println("before sleep");
... 阅读全帖
N***m
发帖数: 4460
25
来自主题: Programming版 - 编程题一道
看到c/c++笔试问题一道,估计是经典问题了。
=======================================
编程输出以下格式的数据。(趣味题)
a) When i=0
1
b) When i=1
7 8 9
6 1 2
5 4 3
c) When i=2
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
=============================
我用java写了一段程序,试了前几个i=1,2,3,4,能工作,但是感觉写的很罗嗦。
有没有更好的Idea?
======================
附:我的臃肿的java code.
[Main.java]
public class Main {
public stat... 阅读全帖
N***m
发帖数: 4460
26
how about in java? tested a few cases and it seems to work.
Map map = new HashMap();
map.put("dog",1);
map.put("sheep",5);
map.put("cat",3);

String s = "dog-cat+dog-sheep";
List list = new LinkedList();
StringTokenizer st = new StringTokenizer(s, "+-");
while(st.hasMoreTokens()) {
St... 阅读全帖
b*********a
发帖数: 723
27
来自主题: Programming版 - 一道多线程的面试题 (转载)
上代码,三个类。可以直接运行
package com.bruce.concurrent;
public class MainApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "I am from China and I was born in Jiangxi province";
Paragraph para = new Paragraph(str);
Thread pt = null;
int threadCount=5;
for (int i = 0; i < threadCount; i++) {
pt = new Thread(new PrinterThread(i, para, threadCount));
pt.start();
}
}
}
package com.bruce.concurrent;
public class PrinterThread implements Runnable {
private ... 阅读全帖
t**r
发帖数: 3428
28
来自主题: Programming版 - use clojure data collection in java..
在java里用clojure的immutable data collection, 比如persistentHashMap.
这样用好处是immutable?具体来说immutable能解决多线程的问题?
compound operation还是得自己保护吧?
package persistent;
import clojure.lang.IPersistentMap;
import clojure.lang.IPersistentSet;
import clojure.lang.IPersistentVector;
import clojure.lang.PersistentHashMap;
import clojure.lang.PersistentHashSet;
import clojure.lang.PersistentVector;
public class PersistentTest {
public static void main(String[] args) {
IPersistentMap m = PersistentHashMap.create("... 阅读全帖
c*****t
发帖数: 1879
29
来自主题: Programming版 - 两个我永远都不想碰的语言
最近无聊,稍微研究了下两个语言,彻底被雷了。
第一是 R 。可以说是世界上最 fucked up 的语言之一(COBOL 是另外一个)。
你看一下这篇文章就明白了:
https://xianblog.wordpress.com/2010/09/13/simply-start-over-and-build-
something-better/
如果你非要写 R 代码。建议你把所有的 variable 都弄个 prefix 。免得你
不小心碰到这种麻烦事。
第二就是 Go 。整个一傻逼语言。
1) 如果该语言有 pointer,但是其速度比 Java 还慢点,谁 TMD 有病才用它。
再不用说,Go 里面需要知道很多很多 low level 的东西,但是搞了半天比 Java
还慢?!!
2) Stupid copies 。好吧,你有 pointer 不用,非得 pass by value (i.e.
struct copy),真是脑袋抽筋了。copy 大部分情况下比 reference 慢。
reference 是可以放在 register 里的,而 struct 一旦比 register... 阅读全帖
s****b
发帖数: 2039
30
来自主题: Military版 - 万能的军版求问个数学问题
你不是问有没有什么好办法吗?跑下面这个程序就可以了,和是1,每个随机数乘0.
001。我
跑了半天还没得到结果。你自己用快的计算机去跑吧。
***
import java.util.Random;
public class Haha {
public static void main(String[] args) {
Random rn = new Random();
int t=0;
String s="";
while (t != 1000) {
t=0;
s="";
for (int i=0; i<10; i++) {
int r = rn.nextInt(1001);
t=t+r;
s=s+r+" ";
}
}
System.out.println("sum = "+t);
... 阅读全帖
s****b
发帖数: 2039
31
来自主题: Military版 - 万能的军版求问个数学问题
用下面这个办法,可以看到每次的10个随机数,以及和是多少。最后到和是1000
停止。
***
import java.util.Random;
public class Ha {
public static void main(String[] args) {
Random rn = new Random();
int t=0;
String s="";
while (t != 1000) {
t=0;
s="";
for (int i=0; i<10; i++) {
int r = rn.nextInt(1001);
t=t+r;
s=s+r+" ";
System.out.println("sum = "+t);
System.out.println("the random numb... 阅读全帖
a******h
发帖数: 19
32
来自主题: JobHunting版 - 问2道面试题
I use dynamic programming to solve Q2. My solution required a sorted
integer array as input. The boolean array is the solution.
public static void main (String [] args) {
int [] intList = {1, 3, 5, 6, 8, 9, 11, 12};
boolean [] boolList = subsetSum(intList, 10);
System.out.println(Arrays.toString(intList));
System.out.println(Arrays.toString(boolList));

}
public static boolean [] subsetSum(int [] intList, int sum) {
if (su
l**********9
发帖数: 537
33
来自主题: JobHunting版 - 问一个Java题
String a="abhay"
String b="deol"
System.out.println(a+b);
======================
String a=new String("abhay");
String b=new String ("deol");
System.out.println(a+b);
========================
Which is more efficient? why?
有2种看法,参看职业杯
http://www.careercup.com/question?id=69271
谢谢了
g**e
发帖数: 6127
34
来自主题: JobHunting版 - 这个很老的Java Trick
System.out.println(ga.i);
System.out.println(((Arabik)ga).i);
member variable in java uses static binding, while method uses dynamic
binding, that's how polymorphism works.
P********l
发帖数: 452
35
来自主题: 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 ... 阅读全帖
l**********n
发帖数: 12
36
来自主题: JobHunting版 - 问道很难的面世题
//solution for Java version
public int nutsLeftRecur(int D, int N, int F, int C){
//T: the times to transport N for horse with C
int T = N / C;
int remain = N % C;
if (remain != 0){
//one more time add to T to transport for horse
T++;
}
//two cases to exit this recursion:
// Case 1:
// destination arrived, return nuts left.
if (D == 0)
return N;
// Case 2:
// if nuts N ... 阅读全帖
S******1
发帖数: 269
37
// Find the longest subarray with equal 1 and 0.
public static int[] longestSub(int[] inputArray) {
int length = inputArray.length;
int[][] record = new int[length][length];
//Initial
for (int i = 0; i < length; i++)
if (inputArray[i] == 1)
record[i][i] = 1;
else
record[i][i] = -1;
for (int i = 0; i < length; i++) {
for (int j = i; j < length; j++) {
if (i < j)
... 阅读全帖
g***s
发帖数: 3811
38
来自主题: JobHunting版 - 生物男的Google面经节略版
It is very quick even for n = Integer.MAX_VALUE;
public static void main(String arg[]){
preprocess();
int n=Integer.MAX_VALUE;
System.out.println("squares sum of " + n + " : ");
for (int num : getSquaresSum(n)){
System.out.print(" " + num);
}
System.out.println("\ntotal callCount= " + callCount);
}
public static Collection getSquaresSum(int n){
Stack cur = new Stack();
Stack阅读全帖
g**e
发帖数: 6127
39
来自主题: JobHunting版 - 愤怒,amazon interviewer 不知KMP 算法
Here is my java version
public static void KMP(String target, String pattern) {
boolean found = false;
int[] overlap = getOverlap(pattern);

int j = 0;
for (int i=0; i while (true) {
if (target.charAt(i) == pattern.charAt(j)) {
j++;
if (j =... 阅读全帖
r******r
发帖数: 700
40
来自主题: JobHunting版 - 问个 Palindrome 的问题
我这个方法很笨。 谁能写个 O(n) 的? 这个好像也是被 facebook 考了的。
// civic
private static boolean isCharPalindrome(String test) {
String stripped = test.toLowerCase().replaceAll("[^0-9a-zA-Z]", "");
for(int i = 0; i < stripped.length() / 2; i++) {
if(stripped.charAt(i) != stripped.charAt(stripped.length() - 1 -
i)) {
return false;
}
}
return true;
}
// ILLINOISURB
public static String longestPrefixPalindrome(String test){
Stri... 阅读全帖
g***s
发帖数: 3811
41
来自主题: JobHunting版 - 问两道数字题
we can preProcess using DP to save time;
time is O(logN×logN)
public class MaxSum {
private static final int MAX_DIGIT = 7;
static int dp[][] = new int[MAX_DIGIT+1][MAX_DIGIT*9+1];
public static int findMax(int x){
int digits = ("" + x).length();
int max = 0;
int t;
for (int sum = 0 ; sum <= 9*digits ; sum++){
max = Math.max(max, t = totalWaysUpto(x, sum));
}
return max;
}
private static int totalWaysUpt... 阅读全帖
b********1
发帖数: 49
42
来自主题: JobHunting版 - G面经
第八题怎么是5?
从3到10 (0 based).
我用的是O(N3), 慢但应该正确呀...
import java.util.*;
public class Water {
int test(int arr[]) {
int N = arr.length;
int max = 0;
for (int i = 0; i < N - 2; i++) {
for (int j = i + 2; j < N; j++) {
int min = Math.min(arr[i], arr[j]);
int sum =0;
for (int k = i + 1; k < j; k++) {
int v = min - arr[k];
if (v > 0) {
sum += v;
... 阅读全帖
b********1
发帖数: 49
43
来自主题: JobHunting版 - G面经
第八题怎么是5?
从3到10 (0 based).
我用的是O(N3), 慢但应该正确呀...
import java.util.*;
public class Water {
int test(int arr[]) {
int N = arr.length;
int max = 0;
for (int i = 0; i < N - 2; i++) {
for (int j = i + 2; j < N; j++) {
int min = Math.min(arr[i], arr[j]);
int sum =0;
for (int k = i + 1; k < j; k++) {
int v = min - arr[k];
if (v > 0) {
sum += v;
... 阅读全帖
g**********y
发帖数: 14569
44
来自主题: JobHunting版 - 求一道 面世题 的解答思路
我写的慢程序:用HashMap实现的Trie. 换成char[], 对于N=7, 速度快一些,但是对N<
7, 速度更慢。可能因为解跟Hash key的位置有关。
public class WordRectangle {
private final static String DIR = "src/test/resources";

private Trie m_trie;
private String[] m_words;
public static void main(String[] args) {
WordRectangle w = new WordRectangle();
long t0 = System.currentTimeMillis();
w.find(6);
long t1 = System.currentTimeMillis();
System.out.println("Time = " + (t... 阅读全帖
c*****m
发帖数: 315
45
来自主题: JobHunting版 - 问个amazon面试题
double play 的算法真的很妙,可以试用几种不同的SHUFFLE, 只需要换那个TARGET
函数即可。我依葫芦画瓢写了两个:
original array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
after odd-even shuffle:
1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 20
after in-shuffle:
1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 20
这个算法看起来有2个LOOP, 但里面那个LOOP 很少RUN, 应该可以证明里面的LOOP 总
数有个UPPER BOUND, 总的时间复杂度很有可能就是O(N)。
public class InShuffle {
public static int target_idx(int idx,int length)
{
int next;
next=2*idx;
if(next... 阅读全帖
g**********y
发帖数: 14569
46
来自主题: JobHunting版 - [算法]打印所有因子乘积组合
不用那么复杂,这种题就是brutal force + DFS
public class Decompose {
private void decompose(int N) {
dfs(N, N, "");
}

private void dfs(int N, int hi, String s) {
if (N == 1) {
if (s.contains("*")) {
System.out.println(s);
}
else {
System.out.println(s + "*1");
}
return;
}

for (int i=hi; i>1; i--) {
if (N%i == 0) {
dfs(N/i, i, s.length(... 阅读全帖
g**********y
发帖数: 14569
47
来自主题: JobHunting版 - 这个facebook puzzle样题怎么做?
这个题不好写的是状态保存和转换。
最多5个peg, 8个disc, 所以可以用long(64-bit)来保存状态,转换需要位操作。
这是我的写法,欢迎改进:
public class KPeg {
public void move(int N, int K, int[] begin, int[] end) {
long s0 = stateToLong(begin);
long sn = stateToLong(end);

ArrayList visited = new ArrayList();
visited.add(new Node(s0, 0, 0, -1));

Deque dq = new ArrayDeque();
dq.add(0);

while (!dq.isEmpty()) {
int current = dq.remov... 阅读全帖
q*****t
发帖数: 3
48
来自主题: JobHunting版 - 问个Amazon面试题
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[... 阅读全帖
q*****t
发帖数: 3
49
来自主题: JobHunting版 - 问个Amazon面试题
import java.util.ArrayList;
import java.util.List;
public class NextLargerNumberWithSameDigits {
public static void main(String[] args) throws Exception {
int a = 1245963;
System.out.println(findnext(a));
a = 698754;
System.out.println(findnext(a));
}
public static int findnext(int a) throws Exception {
List d = new ArrayList();
while (a != 0) {
d.add(a % 10);
a /= 10;
}
Integer[... 阅读全帖
n*******w
发帖数: 687
50
来自主题: JobHunting版 - 请教一道面试题,关于tree的
java写的。可以递归。
public class recurivePre2Post {
final static int MUL = -1;
final static int ADD = -2;
public static void main(String[] args){
int[] expr1 = {MUL, ADD, 1, 2, 3};
int[] expr2 = {ADD, ADD, ADD, ADD, 1, 2, 3, MUL, MUL, 4, 5, 6, 7};
int[] expr3 = {MUL, 2, ADD, 1, MUL, 3, 4};
convert(expr1, 0);
System.out.println();
convert(expr2, 0);
System.out.println();
convert(expr3, 0);
}
public static int convert(int... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)