• 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));
        }
    }
  • 相关阅读:
    Expression Blend4经验分享:自适应布局浅析
    Expression Blend4经验分享:制作一个简单的图片按钮样式
    Expression Blend4经验分享:制作一个简单的文字按钮样式
    24.Unity手势操作(转自宣雨松博客)一
    1.关于Unity -Vuforia -Android 开发 ,平台的搭建(极品菜鸟完整版)
    26. Kinect + Unity 体感及增强现实开发历程一
    25.Unity3D手机中Input类touch详解-Unity触屏事件解析到底(Twisted Fate)
    23.Unity+Metaio SDK 开发笔记
    徐CC , Unity +C# 自学笔记2
    unity3d中的http通信
  • 原文地址:https://www.cnblogs.com/juniorMa/p/15879665.html
Copyright © 2020-2023  润新知