算法题学习记录(一)

题目

  • 双指针、哈希、字符串
import java.util.*;

import java.util.*;
public class Solution {
    /**
     * 
     * @param S string字符串 
     * @param T string字符串 
     * @return string字符串
     */
    public String minWindow (String S, String T) {
        // write code here
        int[] map = new int[128];
        for(int i=0;i<T.length();i++)
            map[T.charAt(i)]++;
        int begin=0, end=0,count=T.length(),d=Integer.MAX_VALUE,head=0;
        while(end<S.length()){
            if(map[S.charAt(end++)]-- >0 )
                count--;
            while(count==0){
                if(end-begin<d)
                    d = end-(head=begin);
                if(map[S.charAt(begin++)]++==0)
                    count++;
            }
        }
        return  d==Integer.MAX_VALUE?"":S.substring(head,head+d);
    }
}

思路

心得

前序中序,求层次

package com.amb.stock_pre.zijie;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    class Node{
        char val;
        Node left;
        Node right;

        Node(){

        }
        Node(char x){
            this.val = x;
            this.left = null;
            this.right = null;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    private Node root;

    public Main(){
        root = build(0,inorder.length-1,0,preorder.length-1);
    }


    private char[] inorder = {'A','C','Q','V','L','C','O','J','Y','P','R','K','S','X','G'};

    private char[] preorder = {'Y','L','C','A','Q','V','J','C','O','X','P','K','R','S','G'};

    ArrayList<Integer> ans = new ArrayList<>();

    public Node build(int ix,int iy,int px,int py){
//        if(px >= preorder.length || ix >= inorder.length)return null;
        if(px > py || ix>iy){
            return null;
        }

        char root = preorder[px];//根节点
        Node node = new Node(root);
        int mid = ix;
        for(;mid<=iy;mid++){
            if(inorder[mid] ==  root){
                node.left = build(ix,mid-1,px+1,px+mid-ix);
                node.right = build(mid+1,iy,px+mid-ix+1,py);
            }
        }


        return node;
    }

    public void getCheng(){
        if(root == null)return;
        Queue<Node> que = new LinkedList<>();

        que.offer(root);
        while (!que.isEmpty()){
            int len = que.size();
            for(int i=0;i<len;i++){
                Node node = que.poll();
                System.out.print(node.val);
                if(node.left!=null)que.offer(node.left);
                if(node.right != null)que.offer(node.right);
            }
            System.out.println("");

        }

    }

    public void getAfterorder(Node node){
        if(node!=null){
            getAfterorder(node.left);
            getAfterorder(node.right);
            System.out.print(node.val);
        }
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.getAfterorder(m.root);
        System.out.println("");
        m.getCheng();
    }


}

你可能感兴趣的:(后端相关,刷题笔记,字符串,算法,leetcode)