最近在找工作,接觸到一些筆試題目,雖然網(wǎng)上也有會各種實現(xiàn),但還是覺得自己寫出來能理解的更深刻一點。
當(dāng)時三道題只有一個小時的時間。平時這部分內(nèi)容練的不多,只完成了一道,剩下兩道題都是后面做的,一共花了2個半小時。。。。。
1.實現(xiàn)兩個線程,使之交替打印1-100;
public class First {
static class NumThread implements Runnable {
private int i = 1;
@Override
public void run() {
while (i <= 100) {
synchronized (this) {
notify();
System.out.println(Thread.currentThread().getName() + ":\t" + i++);
try {
if (i <= 100) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("線程結(jié)束:" + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
NumThread numThread = new NumThread();
Thread thread1 = new Thread(numThread);
Thread thread2 = new Thread(numThread);
thread1.start();
thread2.start();
}
}
2.實現(xiàn)函數(shù),給定一個字符串?dāng)?shù)組,求該數(shù)組的連續(xù)非空子集
思想是按游標(biāo)分隔成左右兩段,左段整體和右段每個數(shù)據(jù)進(jìn)行連接
public class ArrayCollected {
public static void main(String[] args) {
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
collected(0, 1, list);
}
public static void collected(int cutIndexBegin, int cutIndexEnd, List list) {
if (cutIndexEnd > list.size()) {
return;
}
List subList = list.subList(cutIndexBegin, cutIndexEnd);
String subStr = dataFormat(subList);
if (cutIndexEnd - cutIndexBegin == 1) {
System.out.println(subStr);
}
for (int i = cutIndexEnd; i < list.size(); i++) {
System.out.println(subStr + list.get(i).toString());
}
if (cutIndexEnd == list.size()) {
cutIndexEnd = ++cutIndexBegin;
}
collected(cutIndexBegin, cutIndexEnd + 1, list);
}
public static String dataFormat(List list) {
StringBuffer sb = new StringBuffer();
list.forEach(e -> sb.append(e));
return sb.toString();
}
}
舊版實現(xiàn)
public class Second {
static String exampleData = "abcdeftgy";
public static void main(String[] args) {
char[] chars = exampleData.toCharArray();
ArrayList<Character> characters = new ArrayList<>();
for (char c : chars) {
characters.add(c);
}
for (int i = 0; i < characters.size(); i++) {
joint(0, characters.subList(i, characters.size()));
}
}
public static void joint(int cutIndex, List<Character> characters) {
if (cutIndex > characters.size()) {
return;
}
if (cutIndex == 0) {
System.out.println(characters.get(cutIndex));
} else {
StringBuffer stringBuffer = new StringBuffer();
characters.subList(0, cutIndex).stream().forEach(character -> stringBuffer.append(character));
String leftStr = stringBuffer.toString();
for (int i = cutIndex; i < characters.size(); i++) {
System.out.println(leftStr + characters.get(i));
}
}
joint(cutIndex + 1, characters);
}
}
3.文件系統(tǒng)中按逗號分割保存了1億個正整數(shù)(一行10個數(shù),1000萬行),找出其中最大的100個數(shù)
實測結(jié)果使用ArrayList 38秒;而使用LinkList需要86秒。然而我也沒搞明白為啥有這么大的區(qū)別
public class BigFile {
static class DataArrayStructure {
int max;
List<Integer> list;
public DataArrayStructure(boolean useArrayList) {
if (useArrayList) {
list = new ArrayList<>(128);
} else {
list = new LinkedList<>();
}
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public List<Integer> getList() {
return list;
}
public void addElement(Integer i) {
if (i > max) {
max = i;
}
this.getList().add(i);
if (this.list.size() > 100) {
removeMin();
// removeLinkMin();
}
if (this.list.size() > 100) {
throw new RuntimeException("數(shù)組超長");
}
}
public void removeMin() {
int min = this.list.get(0);
int index = 0;
for (int i = 1; i < this.list.size(); i++) {
int j = this.list.get(i).intValue();
if (j < min) {
min = j;
index = i;
}
}
this.list.remove(index);
}
public void removeLinkMin() {
Iterator<Integer> iterator = this.list.iterator();
Integer min = iterator.next();
while (iterator.hasNext()) {
Integer t = iterator.next();
if (t < min) {
min = t;
}
}
this.list.remove(min);
}
}
public static void main(String[] args) throws Exception {
long startTime = System.currentTimeMillis();
readData(new File("/Users/zhangfan/logs/data.txt"));
// writeData();
System.out.println("花費了:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
public static void readData(File file) throws IOException {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
DataArrayStructure data = new DataArrayStructure(true);
String str;
while ((str = bufferedReader.readLine()) != null) {
String[] strings = str.split(",");
for (int j = 0; j < strings.length; j++) {
data.addElement(Integer.valueOf(strings[j]).intValue());
}
}
bufferedReader.close();
System.out.println(JSONObject.toJSONString(data.getList()));
}
public static void writeData() throws IOException {
File file = new File("/Users/zhangfan/logs/data.txt");
if (file.exists()) {
file.createNewFile();
}
Random random = new Random();
FileOutputStream fileOutputStream = new FileOutputStream(file);
for (int i = 0; i < 10000000; i++) {
StringBuffer stringBuffer = new StringBuffer();
for (int j = 0; j < 10; j++) {
stringBuffer.append(random.nextInt());
stringBuffer.append(",");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
stringBuffer.append("\n");
fileOutputStream.write(stringBuffer.toString().getBytes(), 0, stringBuffer.toString().getBytes().length);
}
fileOutputStream.close();
}
}