笔试题解答

简介: /** * @author shishusheng * @date 2018/8/22 23:35 */import java.util.
img_7af7375e3743cf8b14f7a5f450987d16.png
/**
 * @author shishusheng
 * @date 2018/8/22 23:35
 */
import java.util.ArrayList;

public class ReverseList {

    private class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    ArrayList<Integer> arrayList = new ArrayList<>();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode != null){
            this.printListFromTailToHead(listNode.next);
            arrayList.add(listNode.val);
        }
        return arrayList;
    }
}
img_8cf3dd974c2b2e21b48053199b436520.png

显然是斐波那契数列

/**
 * @author shishusheng
 * @date 2018/8/22 23:35
 */
import java.util.ArrayList;

public class JumpFloor {

    public int JumpFloorII(int N) {
        int[] stepArr = new int[N];
        stepArr[0] = 1;
        for (int i = 1; i < N; i++) {
            for (int j = 0; j < i; j++) {
                stepArr[i] += stepArr[j];
            }
            stepArr[i]+=1;
        }
        return stepArr[N - 1];
    }
}
img_437ec420bff3a85992f0f010d813b37e.png

img_74d2c35ee3609e7ebbf46eda617f7b6a.png
/**
 * @author shishusheng
 * @date 2018/8/22 23:35
 */

public class JudgeTriAngle {

    public String judgeTriAngle(int a, int b, int c) {
        if (a + b > c && b + c > a && c + a > b) {
            return "成立";
        }
        return "不成立";
    }

}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author shishusheng
 * @date 2018/8/22 23:35
 */

public class HourseRun {

    private class Point {
        private int x;
        private int y;
        private int shortest;

        public Point() {
        }

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getShortest() {
            return shortest;
        }

        public void addShortest(Point point) {
            this.shortest = point.getShortest() + 1;
        }
    }

    private static final int MAX_SIZE = 8;
    private static final int MIN_SIZE = 1;
    private Point start;
    private Point end;

    private List<Point> direction = new ArrayList<>();
    private LinkedList<Point> unVisitedPoint = new LinkedList<>();
    private int[][] markVisited = new int[9][9];

    public HourseRun(Point start, Point end) {
        this.start = start;
        this.end = end;
        init();
    }

    public void init() {
        direction.add(new Point(2, 1)); //right up horizontal
        direction.add(new Point(2, -1)); //right down horizontal
        direction.add(new Point(1, -2)); //right down vertical
        direction.add(new Point(-1, -2)); //left down vertical
        direction.add(new Point(-2, -1)); //left down horizontal
        direction.add(new Point(-2, 1)); //left up horizontal
        direction.add(new Point(-1, 2)); //left up vertical
        direction.add(new Point(1, 2)); //right up vertical
    }

    private int bfs() {
        Point current = new Point();
        while (!unVisitedPoint.isEmpty()) {
            current = unVisitedPoint.poll();
            markVisited[current.getX()][current.getY()] = 1;

            if (current.getX() == end.getX() && current.getY() == end.getY()) {
                break;
            }

            for (Point aDirection : direction) {
                Point next = new Point(current.getX() + aDirection.getX(), current.getY() + aDirection.getY());
                next.addShortest(current);

                if (next.getX() < MIN_SIZE || next.getX() > MAX_SIZE || next.getY() < MIN_SIZE || next.getY() > MAX_SIZE) {
                    continue;
                }

                if (markVisited[next.getX()][next.getY()] == 0) {
                    unVisitedPoint.add(next);
                }
            }

        }
        return current.getShortest();
    }

    public int find() {
        this.unVisitedPoint.add(start);
        return bfs();
    }


}
目录
相关文章
|
网络协议 Java 调度
笔试题总结
32位机器上,以下结构的sizeof(P)为 struct A { int a; char b; int c; char d; } struct P { struct A w[2]; short b; struct A* p; } /*考察结构体对齐和填充: 结构体每个成员相对于结构体首地址的偏移量都是成员大小的整数倍,如果不是,编译器会自动在成员间填充。
948 0
|
算法 机器学习/深度学习 BI
|
机器学习/深度学习 搜索推荐 索引
|
机器学习/深度学习