《leetcode 3.7》

1. 搜索旋转排序数组

题目描述:
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。
编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。

代码:

package leetcode.week9;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-03-07 19:11
 * @desc
 */
public class t81 {
    public static boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = (end - start) / 2 + start;
            if (nums[mid] == target) {
                return true;
            }

            if (nums[start] == nums[mid]) {
                start++;
                continue;
            }

            // 前半部分有序
            if (nums[start] < nums[mid]) {
                if (target < nums[mid] && target >= nums[start]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            } else {
                if (target > nums[mid] && target <= nums[end]) {
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }
        }

        return false;
    }

    public static void main(String[] args) {

        int[] nums = {1, 3, 5};
        System.out.println(search(nums, 1));
    }
}

2. 删除排序链表中的重复元素 II

题目描述:
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5

代码:

package leetcode.week9;


/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-03-07 19:26
 * @desc
 */
public class t82 {
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode tmp = new ListNode(0);
        tmp.next = head;
        ListNode tt = tmp;
        while (tmp.next != null) {
            int val = tmp.next.val;
            if (tmp.next.next != null && (tmp.next.next.val == val)) {
                ListNode ttt = tmp.next.next;
                while (ttt != null && ttt.val == val) {
                    ttt = ttt.next;
                }
                tmp.next = ttt;
            } else {

                tmp = tmp.next;
            }
        }
        return tt.next;

    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(2);
        ListNode listNode4 = new ListNode(3);
        ListNode listNode5 = new ListNode(3);
        ListNode listNode6 = new ListNode(4);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        listNode5.next = listNode6;

        ListNode res = deleteDuplicates(listNode1);
        while (res != null) {
            System.out.println(res.val);
            res = res.next;
        }
    }
}

3. 最短无序连续子数组

题目描述:
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
你找到的子数组应是最短的,请输出它的长度。
示例 1:
输入: [2, 6, 4, 8, 10, 9, 15]
输出: 5
解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。

代码:

package leetcode.week9;

import java.util.Arrays;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-03-06 22:07
 * @desc
 */
public class t581 {
    public static int findUnsortedSubarray(int[] nums) {
        int[] numSorted = nums.clone();
        Arrays.sort(nums);
        int start = nums.length;
        int end = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != numSorted[i]) {
                start = Math.min(start, i);
                end = Math.max(end, i);
            }
        }
        return (end - start) >= 0 ? end - start + 1 : 0;

    }

    public static void main(String[] args) {
        int[] nums = {2, 6, 4, 8, 10, 9, 15};
        System.out.println(findUnsortedSubarray(nums));
    }
}

4. 错误的集合

题目描述:
集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复。
给定一个数组 nums 代表了集合 S 发生错误后的结果。你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
示例 1:
输入: nums = [1,2,2,4]
输出: [2,3]

代码:

package leetcode.week9;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-03-07 18:47
 * @desc
 */
public class t645 {
    public static int[] findErrorNums(int[] nums) {
        int[] bucket = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            bucket[nums[i] - 1]++;
        }
        int[] res = new int[2];
        for (int i = 0; i < bucket.length; i++) {
            if (bucket[i] > 1) {
                res[0] = i + 1;
            } else if (bucket[i] == 0) {
                res[1] = i + 1;
            }
        }
        return res;
    }

    public static void main(String[] args) {

        int num[] = {1, 2, 2, 4};

        int[] res = findErrorNums(num);
        for (int i : res) {
            System.out.println(i);
        }
    }
}

5. 前K个高频单词

题目描述:
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
示例 1:
输入: [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2
输出: [“i”, “love”]
解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 “i” 在 “love” 之前。

代码:

package leetcode.week9;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2020-03-06 20:37
 * @desc
 */
public class t692 {

    public static List<String> topKFrequent(String[] words, int k) {
        Map<String, Integer> map = new HashMap<>();
        for (String str : words) {
            map.put(str, map.getOrDefault(str, 0) + 1);
        }

        return map.entrySet().stream()
                .sorted(new Comparator<Map.Entry<String, Integer>>() {
                    @Override
                    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                        if (o1.getValue().equals(o2.getValue())) {
                            return o1.getKey().compareTo(o2.getKey());
                        }
                        return o2.getValue().compareTo(o1.getValue());
                    }
                })
                .limit(k)
                .map(Map.Entry<String, Integer>::getKey)
                .collect(Collectors.toList());

    }

    public static void main(String[] args) {
        String[] strs = {"i", "love", "leetcode", "i", "love", "coding"};//{"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};

        List<String> res = topKFrequent(strs, 4);
        for (String str : res) {
            System.out.println(str);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值