leetcode1

来源:互联网 发布:linux 拆卸软件 编辑:程序博客网 时间:2024/06/10 15:08

1.二叉树最下深度(用层次遍历)

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */import java.util.Queue;import java.util.LinkedList;public class Solution {    public int run(TreeNode root) {         if(root ==null)           return 0;         if(root.left==null && root.right==null)             return 1;       Queue<TreeNode> qu = new LinkedList<TreeNode>();       int depth =1;       int start =0;       int end =0;       qu.add(root);       end = qu.size();       while(!qu.isEmpty()){           TreeNode temp = qu.remove();           start++;           if(temp.left!=null)               qu.add(temp.left);           if(temp.right!=null)               qu.add(temp.right);           if(temp.left==null && temp.right==null){          break;           }            if(start == end){           depth++;               end = qu.size();               start =0;           }                          }return depth;    }}
2.计算逆波兰表达式的值

import java.util.Stack;public class Solution {    public int evalRPN(String[] tokens) {        Stack<Integer> num = new Stack<Integer>();               for(int i=0;i<tokens.length;i++){           if(tokens[i].equals("+")|| tokens[i].equals("-")|| tokens[i].equals("*")||tokens[i].equals("/")){              int num2 = num.pop();              int num1= num.pop();              num.push(calculate(tokens[i],num1,num2));           }else{              int number = Integer.parseInt(tokens[i]);              num.push(number);           }        }        return num.pop();    }    private int calculate(String op,int f1,int f2){      if(op.equals("+"))         return f1+f2;      if(op.equals("-"))         return f1-f2;       if(op.equals("*"))       return f1*f2;       if(op.equals("/"))       return f1/f2;      return 0;    }}

3.对链表排序

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode sortList(ListNode head) {                if (head == null || head.next == null)            return head;        ListNode slow = head;        ListNode fast = head.next;        while (fast != null && fast.next != null) {            slow = slow.next;            fast = fast.next.next;        }        ListNode right = sortList(slow.next);        slow.next = null;        ListNode left = sortList(head);        // /待左右两边各自有序,进行归并即可        ListNode temp_head = new ListNode(0);        ListNode temp_node = temp_head;        while (left != null && right != null) {            if (left.val < right.val) {                temp_node.next = left;                left = left.next;            } else {                temp_node.next = right;                right = right.next;            }            temp_node = temp_node.next;        }        if (left != null)            temp_node.next = left;        if (right != null)            temp_node.next = right;        return temp_head.next;    }}
4.链表插入排序

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode insertionSortList(ListNode head) {        if(head==null)            return null;        ListNode dumy = new ListNode(Integer.MIN_VALUE);        ListNode pre = dumy;        ListNode cur = head;        while(cur!=null){            ListNode next = cur.next;            pre = dumy;            while(pre.next!=null && cur.val>pre.next.val){                pre = pre.next;            }            cur.next = pre.next;            pre.next= cur;            cur = next;        }        return dumy.next;    }}
5.二叉树后续遍历

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */import java.util.ArrayList;import java.util.Stack;public class Solution {    public ArrayList<Integer> postorderTraversal(TreeNode root) {        Stack<TreeNode> s = new Stack<TreeNode>();        ArrayList<Integer> list = new ArrayList<Integer>();        if(root ==null)            return list;        TreeNode p = root,r=null;               while(p!=null || !s.isEmpty()){            if(p!=null){                s.push(p);                p =p.left;            }else{                p=s.peek();                p =p.right;                if(p!=null && p!=r){                    s.push(p);                    p=p.left;                }else{                    p=s.pop();                    list.add(p.val);                    r=p;                    p=null;                }            }        }        return list;    }}


6.二叉树前序遍历

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */import java.util.ArrayList;import java.util.Stack;public class Solution {    public ArrayList<Integer> preorderTraversal(TreeNode root) {        ArrayList<Integer> list = new ArrayList<Integer>();        Stack<TreeNode> q = new Stack<TreeNode>();             if(root ==null)            return list;        q.push(root);        while(!q.isEmpty()){            TreeNode temp = q.pop();            list.add(temp.val);            if(temp.right!=null)                q.push(temp.right);            if(temp.left!=null)                q.push(temp.left);                                              }              return list;    }}

7.判断链表是否有环,返回环开始的节点

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode detectCycle(ListNode head) {        if(head ==null || head.next==null)            return null;        ListNode fast = head;        ListNode slow = head;        while(fast!=null && fast.next!=null){            fast = fast.next.next;            slow = slow.next;                            if(fast==slow){              ListNode slow2 = head;              while(slow != slow2){              slow =slow.next;              slow2 = slow2.next;              }            return slow;           }        }        return null;    }}

8.判断链表是否有环

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public boolean hasCycle(ListNode head) {        if(head==null || head.next==null)            return false;        ListNode fast = head;        ListNode slow = head;        while(fast!=null && fast.next!=null){            fast = fast.next.next;            slow = slow.next;            if(fast == slow)                return true;        }        return false;    }}

9.带有随机指针的链表复制

/** * Definition for singly-linked list with a random pointer. * class RandomListNode { *     int label; *     RandomListNode next, random; *     RandomListNode(int x) { this.label = x; } * }; */import java.util.*;public class Solution {    public RandomListNode copyRandomList(RandomListNode head) {        if(head ==null)            return null;        RandomListNode pNode = head,copyHead = null,copyNode = null;        Map<RandomListNode,RandomListNode> map = new HashMap<>();        while(pNode!=null){            RandomListNode node = new RandomListNode(pNode.label);            node.next = null;            node.random = null;            if(pNode == head){                copyHead = copyNode =node;            }else{                copyNode.next = node;                copyNode = copyNode.next;            }            map.put(pNode,copyNode);            pNode = pNode.next;        }        for(Map.Entry<RandomListNode,RandomListNode> m:map.entrySet()){            m.getValue().random =map.get(m.getKey().random);        }        return copyHead;    }}

10.找出一个数组只出现一次的数

public class Solution {    public int singleNumber(int[] A) {        int one = 0;        int two =0;        int three =0;        for(int i=0;i<A.length;i++){            int t = A[i];            two |=one&t;            one ^=t;            three = two&one;            one &=~three;            two &=~three;        }        return one;    }}







原创粉丝点击