数据结构与算法(二)

75 阅读2分钟

一、概率问题

案例: 有一个f()函数,返回 1-5 等概率的数,求一个 g() 函数,返回 1-7等概率的数

    // 1. 首先将他拆解成 0 - 1 转换器
    //     如果为奇数,例如 5 : 返回 1-2 给 0,返回 4-5 给 1,返回 3 就重做
    //     如果为偶数,和奇数一样处理,不用处理重做
    // 2. 在算一下需要几个二进制位,能够满足。例如 1-7只需要3个二进制位就可以表示
    // 3. 解出 g() 函数
    public static int f1(){
        // 该函数返回 1-5概率的数
        return (Math.roudom() * 5) + 1;
    }
    
    public static int f2() {
        int ans = 0;
        do{
            ans = f1();
        }while(ans == 3);
        return ans < 3 ? 01;
    }
    // 得到 000 - 111  也就是 0-7返回等概率返回
    public static int f3(){
        return (f2() << 2) + (f2() << 1) + f2();
    }
    // 0 - 6 等概率返回
    public static int f4(){
         int ans = 0;
         do{
            ans = f3();
         }while(ans == 7);
         return ans;
    }
    public static int g() {
        return f4() + 1;
    }

二、对数器(测试对不对的例子)

public static int[] lenRandomValueRandom(int maxLen, int maxValue){
    int len = (int)(Math.random() * maxLen);
    int[] ans = new int[len];
    for (int i = 0; i < len; i++) {
        ans[i] = (int) (Math.random() * maxValue);
    }
    return ans;
}

三、二分查找

3.1 基本二分查找

public static boolean BinarySearch(int[]arr, int key){
    if(arr == null || arr.length == 0)
        return false;

    int left = 0;
    int right = arr.length - 1;

    while(left <= right){
        int mid = (left + right) / 2;
        if(arr[mid] == key){
            return true;
        }

        else if(arr[mid] < key) {
            left = mid + 1;
        } else if(arr[mid] > key){
            right = mid - 1;
        }

    }
    return false;
}

3.2 在数组中查找大于等于值的最左边的位置

public static int BinarySearchLeft(int[]arr, int key){
    if(arr == null || arr.length == 0)
        return -1;

    int left = 0;
    int right = arr.length - 1;
    int ans = -1;
    while(left <= right){
        int mid = (left + right) / 2;
        if(arr[mid] >= key){
            ans = mid;
            right = mid - 1;
        }else
            left = mid + 1;

    }
    return ans;
}

四、求第三大的数

class Solution 
{ 
public int thirdMax(int[] nums) { 
if(nums == null || nums.length <=0) 
return -1; 
int k = 0; 
Set<Integer> set = new HashSet(); 
for (int i = 0; i < nums.length; i++) 
{ set.add(nums[k++]); } 
List list = new ArrayList(set); 
Collections.sort(list); 
if(list.size() == 1) 
    return (int)list.get(0); 
if(list.size() == 2)
{ 
    return (int)list.get(0) < (int)list.get(1) ? (int)list.get(1) : (int)list.get(0); 
} 
if(list.size() >= 3){ return (int)list.get(list.size() - 3); } return -1; } }

五、找不同

给定两个字符串 s 和 t ,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例 1:

输入:s = "abcd", t = "abcde" 输出:"e" 解释:'e' 是那个被添加的字母。 示例 2:

输入:s = "", t = "y" 输出:"y"

public char findTheDifference(String s, String t) {
        char[] chars = s.toCharArray();
        char[] chars1 = t.toCharArray();

        Arrays.sort(chars);
        Arrays.sort(chars1);
        boolean flag = false;
        for (int i = 0; i < chars1.length; i++) {
            if(i >=chars.length)
                return chars1[i];
            if(chars[i] != chars1[i]){
                return chars1[i];
            }
        }
        return 'a';
    }

六、两个数的交集

给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。

 

示例 1:

输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2:

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出:[9,4] 解释:[4,9] 也是可通过的

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<Integer>();
        Set<Integer> set2 = new HashSet<Integer>();
        for (int num : nums1) {
            set1.add(num);
        }
        for (int num : nums2) {
            set2.add(num);
        }
        return getIntersection(set1, set2);
    }

    public int[] getIntersection(Set<Integer> set1, Set<Integer> set2) {
        if (set1.size() > set2.size()) {
            return getIntersection(set2, set1);
        }
        Set<Integer> intersectionSet = new HashSet<Integer>();
        for (int num : set1) {
            if (set2.contains(num)) {
                intersectionSet.add(num);
            }
        }
        int[] intersection = new int[intersectionSet.size()];
        int index = 0;
        for (int num : intersectionSet) {
            intersection[index++] = num;
        }
        return intersection;
    }
}