编程面试十大算法总汇

来源:互联网 发布:乌克兰危机知乎 编辑:程序博客网 时间:2024/04/29 22:07

1.字符串
如果IDE没有代码自动补全功能,所以你应该记住下面这些方法。
1、”‘“toCharArray() //获得字符串对应的char数组
2、Arrays.sort() //数组排序
3、Arrays.toString(char[] a) //数组转换成字符串
4、charAt(int x) //获得某个索引处的字符
5、length() //字符串长度
6、length //数组大小
“’”
2、链表
在JAVA中,链表的实现非常简单,每个节点Node都是有一个值val和指向下个节点的链接next.

class Node{    int val;    Node next;    Node(int x){        val=x;        next=null;        }    }

链表两个著名的应用是栈Stack和队列Queue

class Stack{    Node top;    public Node peek(){        if(top!=null){            return top;            }        return null;    }    public Node pop(){        if(top==null){            return null;        }else{            Node temp=new Node(top.val);            top=top.next;            return temp;        }    }    public void push(Node n){        if(n!=null){            n.next=top;            top=n;        }    }}

队列

class Queue{    Node first,last;    public void enqueue(Node n){        if(first==null){            first=n;            last=first;        }else{            last.next=n;            last=n;        }    }    public void Node dequeue(){        if(first==null){            return null;        }else{            Node temp=new Node(first.val);            first=first.next;            if(last==temp)last=first;            return temp;        }    }}

3.树
这里的树通常是指二叉树,每个节点都包含一个左孩子节点和右孩子节点,像下面这样:

class TreeNode{    int value;    TreeNode left;    TreeNode right;

下面是树的一些相关概念:
1.平衡VS非平衡:平衡二叉树中每个节点的左右子树深度相差至多为一。
2.满二叉树:除叶子节点以为的每个节点都有两个孩子。
3.

0 0