NC_31_FirstNotRepeatingChar NC_32_SQRT NC_33_MERGE_LINKLIST NC_34_UNIQUE_PATH

package org.example.interview.practice;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

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

public class NC_31_FirstNotRepeatingChar {
    public static int FirstNotRepeatingChar(String str) {

        TreeMap<Integer, Integer> map = new TreeMap<>();
        char[] array = str.toCharArray();
        int length = array.length;
        Map<Integer, Integer> charMap = new HashMap<>();
        for (int i = 0;i<length; i++) {
            char ch = array[i];
            if (!charMap.containsKey(Integer.valueOf(ch))) {
                charMap.put(Integer.valueOf(ch), 1);
            } else {
                charMap.put(Integer.valueOf(ch), charMap.get(Integer.valueOf(ch)) + 1);
            }

            map.put(Integer.valueOf(i), Integer.valueOf(ch));

        }

        Set<Map.Entry<Integer, Integer>> set = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : set) {
            Integer value = entry.getValue();
            if (charMap.get(value) == 1) {
                return entry.getKey();
            }
        }

        return -1;
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/7/21
 */

public class NC_32_SQRT {
    public int sqrt(int x) {
        // write code here
        if (x <= 0) {
            return 0;
        }

        int left = 1, right = x;
        while (true) {
            int middle = (left + right) >> 1;
            if (middle <= x / middle && (middle + 1) > x / (middle + 1)) {
                return (int) middle;
            } else if (middle < x / middle) {
                left = middle + 1;
            } else {
                right = middle - 1;
            }
        }

    }
}
package org.example.interview.practice;

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

public class NC_33_MERGE_LINKLIST {

    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = null;
        while (list1 != null && list2 != null) {
            if (list1.val<list2.val) {
                if (cur == null) {
                    cur = list1;
                    dummy.next = cur;
                } else {
                    cur.next = list1;
                    cur = cur.next;
                }

                list1 = list1.next;

            } else {
                if (cur == null) {
                    cur = list2;
                    dummy.next = cur;
                } else {
                    cur.next = list2;
                    cur = cur.next;
                }
                list2 = list2.next;
            }

        }

        if (list1 != null) {
            cur.next = list1;
        }
        if (list2 != null) {
            cur.next = list2;
        }

        return dummy.next;
    }

    public ListNode Merge2(ListNode list1,ListNode list2) {

        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }

        if (list1.val < list2.val) {
            list1.next = Merge2(list1.next,list2);
            return list1;
        } else {
            list2.next = Merge2(list1,list2.next);
            return list2;
        }
    }

    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }

    public static void main (String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node3 = new ListNode(3);
        ListNode node5 = new ListNode(5);
        node1.next = node3;
        node3.next = node5;

        ListNode node2 = new ListNode(2);
        ListNode node4 = new ListNode(4);
        ListNode node6 = new ListNode(6);
        node2.next = node4;
        node4.next = node6;
    }
}
package org.example.interview.practice;

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

public class NC_34_UNIQUE_PATH {
    public static int uniquePaths (int m, int n) {
        // write code here
        int[][] dp = new int[m][n];

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                // 当 i = 0:dp[0][j] = dp[0][j-1]
                if(i == 0){
                    dp[i][j] = 1; // 都是1是因为dp[0][j] = dp[0][j-1],所以干脆全部赋值为1
                    continue;
                }
                // 当 j = 0:dp[i][0] = dp[i-1][0]
                if(j == 0){
                    dp[i][j] = 1;
                    continue;
                }
                // 当 i > 1 && j > 1 :  dp[i][j] = dp[i][j-1] + dp[i-1][j]
                dp[i][j] = dp[i-1][j]+dp[i][j-1];
            }
        }
        return dp[m-1][n-1]; // 返回到达终点的所有可行路径
    }

    public static int move(int m, int n) {

        if (m < 0 || n < 0)
            return 0;
        if (m == 0 && n == 0) {
            return 1;
        }
        if (m == 1 && n == 0) {
            return 1;
        }
        if (m == 0 && n == 1) {
            return 1;
        }

        return move(m-1,n) + move(m,n-1);

    }

    public static void main (String[] args) {
        System.out.println(uniquePaths(2,1));
    }
}

 

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

导航