NC_26_GenerateParenthesis NC_27_ALL_SUBARRAY NC_28_Min_Window NC_29_FIND_IN_MATRIX

package org.example.interview.practice;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xianzhe.ma
 * @date 2021/8/23
 */

public class NC_26_GenerateParenthesis {

    public static ArrayList<String> generateParenthesis (int n) {
        // write code here
        ArrayList<String> result = new ArrayList<>(10);
        backtrack("", 0, 0, n, result);
        return result;
    }
    private static void backtrack(String string, int open, int close, int n, List<String> result) {
        if (string.length() == n << 1) {
            result.add(string);
            return;
        }
        if (open < n) {
            backtrack(string+"(", open+1, close, n, result);
        }
        if (close < open) {
            backtrack(string+")", open, close+1, n, result);
        }
    }

    public static void main (String[] args) {
        generateParenthesis(3);
    }
}
package org.example.interview.practice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xianzhe.ma
 * @date 2021/8/23
 */

public class NC_27_ALL_SUBARRAY {

    private static ArrayList<ArrayList<Integer>> result = new ArrayList<>();

    public ArrayList<ArrayList<Integer>> subsets(int[] S) {

        if (S == null || S.length == 0) {
            result.add(new ArrayList<>());
            return result;
        }
        result.add(new ArrayList<>());
        int length = S.length;
        List<Integer> inputList = Arrays.stream(S).boxed().collect(Collectors.toList());
        for (int i = 1; i < length; i++) {
            combine(inputList, 0, i, new LinkedList<>());
        }
        result.add((ArrayList<Integer>) inputList);
        return result;
    }

    public static void combine(List<Integer> input, int start, int length, LinkedList<Integer> tempList) {
        if (length == 0) {
            ArrayList<Integer> list = new ArrayList<>();
            for (Integer num : tempList) {
                list.add(num);
            }
            result.add(list);
            return;
        }
        if (start == input.size()) {
            return;
        }

        tempList.add(input.get(start));
        combine(input, start + 1, length - 1, tempList);
        tempList.removeLast();
        combine(input, start + 1, length, tempList);
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/8/15
 */

public class NC_28_Min_Window {
    public static String minWindow (String s, String t) {
        // write code here
        if (s == null || s == "" || t == null || t == "" || s.length() < t.length()) {
            return "";
        }
        //用来统计t中每个字符出现次数
        int[] needs = new int[128];
        //用来统计滑动窗口中每个字符出现次数
        int[] window = new int[128];

        for (int i = 0; i < t.length(); i++) {
            needs[t.charAt(i)]++;
        }

        int left = 0;
        int right = 0;

        String res = "";

        //目前有多少个字符
        int count = 0;

        //用来记录最短需要多少个字符。
        int minLength = s.length() + 1;

        while (right < s.length()) {
            char ch = s.charAt(right);
            window[ch]++;
            if (needs[ch] > 0 && needs[ch] >= window[ch]) {
                count++;
            }

            //移动到不满足条件为止
            while (count == t.length()) {
                ch = s.charAt(left);
                if (needs[ch] > 0 && needs[ch] >= window[ch]) {
                    count--;
                }
                if (right - left + 1 < minLength) {
                    minLength = right - left + 1;
                    res = s.substring(left, right + 1);

                }
                window[ch]--;
                left++;

            }
            right++;

        }
        return res;
    }

    public static void main (String[] args) {
        String s = "XDOYEZODEYXNZ";
        String t = "XYZ";

        minWindow(s, t);
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/11/4
 */

public class NC_29_FIND_IN_MATRIX {

    public boolean Find(int target, int [][] array) {

        // 判断数组是否为空
        int m = array.length;
        if (m == 0) return false;
        int n = array[0].length;
        if (n == 0) return false;
        int r = 0, c = n-1; // 右上角元素
        while (r<m && c>=0) {
            if (target == array[r][c]) {
                return true;
            }
            else if (target > array[r][c]) {
                ++r;
            }
            else {
                --c;
            }
        }
        return false;

    }

    public static void main (String[] args) {

       int[][] array =  {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};

        System.out.println(Find2(15, array));
    }
    public static boolean Find2(int target, int [][] array) {

        // 判断数组是否为空
        int m = array.length;
        if (m == 0) return false;
        int n = array[0].length;
        if (n == 0) return false;
        int r = 0, c = n-1; // 右上角元素
        while (r < m) {
            int left = array[r][0];
            int right = array[r][c];
            if (target >= left && target <= right) {
                boolean result = binarySearch(array[r], 0, c,target);
                if (result) {
                    return true;
                }

            }

            r++;
        }
        return false;

    }

    public static boolean binarySearch(int[] array, int start, int end, int target) {
        if (start > end) {
            return false;
        }
        int mid = (start + end) /2;
        int value = array[mid];
        if (value == target) {
            return true;
        }

        if (target > value) {
            return binarySearch(array,mid+1,end,target);
        } else {
            return binarySearch(array,start, mid-1, target);
        }

    }
}

 

posted on 2022-02-10 16:20  MaXianZhe  阅读(50)  评论(0编辑  收藏  举报

导航