朋友分享了一个某大厂笔试题,挺感兴趣的试做了一下
第一题
第二题
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int base[] = {1, 4, 2, 3, 4, 3, 6, 7, 8, 9};
while (true) {
StringBuffer sb = new StringBuffer();
int guest = scanner.nextInt();
int sum = 0;
for (int i = 0; i < base.length; i++) {
List<Integer> tempBox = new ArrayList<>();
sum = base[i];
tempBox.add(base[i]);
if (sum==guest){
sb.append(tempBox.toString());
continue;
}
for (int j = i+1; j < base.length; j++) {
tempBox.add(base[j]);
sum += base[j];
if (sum==guest) {
sb.append(tempBox.toString());
}
}
}
System.out.println(sb.toString());
}
}
}
输出结果
第三题
package com.iamspring.first;
public class Linked<T> {
private Node<T> first;
private Node<T> tail;
public Linked() {
first = new Node<T>();
tail = first;
}
public class Node<T>{
private Node next;
private Node pre;
private T obj;
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
public void setNext(Node next) {
this.next = next;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
}
public void push(T obj){
Node<T> node = new Node();
node.setObj(obj);
node.pre= tail;
tail.next =node;
tail = node;
}
public T pop(){
if (tail.pre ==null){
return null;
}
T obj = tail.getObj();
tail = tail.pre;
tail.next =null;
return obj;
}
}
测试代码
@Test
public void test2(){
Linked<String> linked = new Linked<>();
System.out.print("入栈:");
for (int i =0;i<10;i++){
linked.push(i+"");
System.out.print(i+" ");
}
System.out.println();
System.out.println("出栈");
for(int i = 0 ;i<10;i++){
System.out.println(linked.pop());
}
}