LeedCode代码记录

1、树  Minimum Depth of Binary Tree

public class Solution {

    public int run(TreeNode root) {
         if(root!=null)
         {
            if(root.left==null) return run(root.right)+1;
            if(root.right==null) return run(root.left)+1;
             int right=run(root.right);
             int left=run(root.left);
             if(right<left) 
                 return right+1;
             else 
                 return left+1;
         }
         else return 0;
  } 
}

 

2、栈  evaluate-reverse-polish-notation

import java.util.Stack;
public class Solution {
   public int evalRPN(String[] tokens) {
        Stack stack=new Stack();
        int temp=0;
       for(int i=0;i<tokens.length;i++){
           if(tokens.length==1){
               temp=Integer.parseInt(tokens[0]);
           }
           if (tokens[i].equals( "+" )|| tokens[i] .equals( "-" )|| tokens[i] .equals( "*") || tokens[i].equals("/")) {
               int a= (Integer) stack.pop();
               int b=(Integer) stack.pop();
               if(tokens[i].equals("+")){
                   temp=a+b;
               } else if (tokens[i].equals("-")) {
                   temp=b-a;
               }else if(tokens[i].equals("*")){
                   temp=a*b;
               }else if(tokens[i].equals("/")){
                   temp=b/a;
               }
               stack.push(temp);
           }else{
              stack.push(Integer.parseInt(tokens[i]));
           }
       }
       return temp;
    }
}

 

3、穷举   max-points-on-a-line

import java.util.HashMap;
import java.util.Map;

import static java.lang.Math.abs;

public class Solution {
       public static int maxPoints(Point[] points) {
        int MAX=0;
        int length=points.length;
        for(int i=0;i<length;i++){
            if(points.length==1){
                MAX=1;
                continue;
            }
            float temp=0;
            boolean flag=false;
            int num=0;
            Map<Object,Integer> map=new HashMap<Object, Integer>();
            map.put("op",1);
            map.put("po",1);
            if(MAX>(length-i)){break;}
            for(int j=i+1;j<length;j++){
                if((points[i].x==points[j].x)&&(points[i].y == points[j].y)){
                    flag=true;
                    num++;
                }else if(points[i].x==points[j].x){
                    map.put("op", map.get("op") + 1);
                }else if ((points[i].y - points[j].y)==0){
                    map.put("po", map.get("po") + 1);
                }else {
                    temp = (float) ((points[i].y - points[j].y)*1.0/(points[i].x - points[j].x));
                    if (map.containsKey(temp)) {
                        map.put(temp, map.get(temp) + 1);
                       // System.out.println(map.get(temp));
                    } else {
                        map.put(temp, 2);
                    }
                }
            }
         for(Integer v:map.values()){
                 if(flag==true){
                     v+=num;
                 }
                if(v>MAX){
                    MAX=v;
                }
          }
        }
        return MAX;
    }
}

 

4、链表 sort-list

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {
    public ListNode sortList(ListNode head) {
         ListNode heah=head;
        ListNode heat=head;
        List<Integer> list=new ArrayList<Integer>();
        boolean flag=false;
        while (heah!=null){
            flag=true;
            list.add(heah.val);
            heah = heah.next;

        }
        if(flag==true){
            Collections.sort(list);
            for(int i=0;i<list.size();i++) {
                heat.val=list.get(i);
                if(heat.next!=null) {
                    heat = heat.next;
                }
            }
        }
        return head;
    }
}

5、树  binary-tree-postorder-traversal

import java.util.ArrayList;
public class Solution {
    ArrayList<Integer> list=new ArrayList<Integer>();
    public  ArrayList<Integer> postorderTraversal(TreeNode root) {
        if(root!=null){
            postorderTraversal(root.left);
            postorderTraversal(root.right);
            list.add(root.val);
        }
        return list;
    }
}

 

6、树  binary-tree-preorder-traversal

import java.util.ArrayList;
public class Solution {
    ArrayList<Integer> list=new ArrayList<Integer>();
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        if(root!=null){
            list.add(root.val);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
        return list;
    }
}

 

7、链表 reorder-list

public class Solution {
         public void reorderList(ListNode head) {
         if(head == null || head.next == null)
            return;        
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode after = slow.next;
        slow.next = null;
        ListNode pre = null;
        while(after != null){
            ListNode temp = after.next;
            after.next = pre;
            pre = after;
            after = temp;
        }
        ListNode first = head;
        after = pre;
        while(first != null && after != null){
            ListNode ftemp = first.next;
            ListNode aftemp = after.next;
            first.next = after;
            first = ftemp;
            after.next = first;        
            after = aftemp;        
        }
    }
}

8、linked-list-cycle

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode a=head;
        ListNode b=head;
        if(head==null) return false;
        while (b!=null){
            a=a.next;
            b=b.next;
            if(b!=null&&b.next!=null){
                b=b.next;
            }else{
                return false;
            }
            if(a==b){
                return true;
            }
        }
      return false;
    }
}

9、word-break-ii (卒。。)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class solution3 {
    public static ArrayList<String> wordBreak(String s, Set<String> dict) {
        ArrayList<String> arrayList=new ArrayList<>();
        int length=s.length();
        int first=0;
        if(dict==null){
            return arrayList;
        }else if(length == 1 && dict.contains(s)){
            arrayList.add(s);
            return arrayList;
        }else if(length>1){
            for (int j = 0; j < length; j++) {
                int flag = 0;
                boolean bool = true;
                StringBuffer str = new StringBuffer();
                for (int i = first; i < length; i++) {
                    String temp = s.substring(flag, i+1);
                    if (dict.contains(temp)) {
                        str.append(temp + " ");
                        if (bool == true) {
                            first = i+1;
                            bool=false;
                        }
                        flag = i + 1;
                    }
                }
                if(str.toString().replaceAll(" ","").length()==s.length())
                    arrayList.add(str.toString().trim());
            }
            Collections.reverse(arrayList);
        }
        return arrayList;
    }
}


 

10、动态规划 word-break

import java.util.HashSet;
import java.util.Set;
public class Solution {
    public boolean wordBreak(String s, Set<String> dict) {
        int n = s.length();
        boolean[] memo = new boolean[n + 1];
        memo[0] = true;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                if (memo[j] && dict.contains(s.substring(j, i))) {
                    memo[i] = true;
                    break;
                }
            }
        }
        return memo[n];
    }
}

11、链表 copy-list-with-random-pointer

import java.util.HashMap;
import java.util.Map;
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        Map<RandomListNode,RandomListNode> map = new HashMap<>();
        RandomListNode p = head;
        while(p != null){
            RandomListNode q = new RandomListNode(p.label);
            map.put(p,q);
            p = p.next;
        }
        p=head;
        while(p != null){
            RandomListNode q = map.get(p);
            q.next = map.get(p.next);
            q.random = map.get(p.random);
            p = p.next;
        }
        return map.remove(head);
    }
}

12、single-number

public class Solution {
    public int singleNumber(int[] A) {
        int num = 0;
        for(int i=0;i<A.length;i++){
           num^=A[i]; //两个相同的数异或为零
        }
       return num;
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值