首页 > 编程语言 >算法--2023.1.13

算法--2023.1.13

时间:2023-01-14 13:34:04浏览次数:42  
标签:13 cur -- int 2023.1 nums1 nums2 HashMap

1.力扣88--合并两个有序数组

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int cur = m+n-1;
        int p = m-1,q = n-1;
        while(p>=0&&q>=0){
            if(nums1[p]>nums2[q]){
                nums1[cur--] = nums1[p--];
            }else{
                nums1[cur--] = nums2[q--];
            }
        }
        while(p>=0){
            nums1[cur--] = nums1[p--];
        }
        while(q>=0){
            nums1[cur--] = nums2[q--];
        }
    }
}

2.力扣2287--重排字符串形成目标字符串

class Solution {
    public int rearrangeCharacters(String s, String target) {
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<Character,Integer> map2 = new HashMap<>();
        int m = s.length(), n = target.length();
        for(int i = 0; i < n; i++){
            map1.put(target.charAt(i),map1.getOrDefault(target.charAt(i),0)+1);
        } 
        for(int i = 0; i<m; i++){
            map2.put(s.charAt(i),map2.getOrDefault(s.charAt(i),0)+1);
        }
        int res = 100;
        for(Character t : map1.keySet()){
            if(map2.containsKey(t)){
                int a = map2.get(t), b = map1.get(t);
                res = Math.min(res,a/b);
            }else{
                return 0;
            }
        }
        return res;
    }
}

 

  

标签:13,cur,--,int,2023.1,nums1,nums2,HashMap
From: https://www.cnblogs.com/lyjps/p/17051624.html

相关文章