最近在找工作,接触到一些笔试题目,虽然网上也有会各种实现,但还是觉得自己写出来能理解的更深刻一点。
当时三道题只有一个小时的时间。平时这部分内容练的不多,只完成了一道,剩下两道题都是后面做的,一共花了2个半小时。。。。。
1.实现两个线程,使之交替打印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("线程结束:" + 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.实现函数,给定一个字符串数组,求该数组的连续非空子集
思想是按游标分隔成左右两段,左段整体和右段每个数据进行连接
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();
}
}
旧版实现
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.文件系统中按逗号分割保存了1亿个正整数(一行10个数,1000万行),找出其中最大的100个数
实测结果使用ArrayList 38秒;而使用LinkList需要86秒。然而我也没搞明白为啥有这么大的区别
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("数组超长");
}
}
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();
}
}