220. Contains Duplicate III
发布日期:2021-06-24 18:22:41 浏览次数:2 分类:技术文章

本文共 2269 字,大约阅读时间需要 7 分钟。

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3, t = 0Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1, t = 2Output: true

Example 3:

Input: nums = [1,5,9,1,5,9], k = 2, t = 3Output: false

难度:medium

题目:给定一整数数组,找出是否存在两个不同的索引i, j使其索引差的绝对值小于等于k, 值的差的绝对值小于等于t.

思路:

  1. 暴力破解,
  2. 使用滑动窗口和TreeSet是为了使得滑动窗口有序,TreeSet底层是二叉搜索树, 如果暴力破解时间复杂度为O(kn), 改用TreeSet使得搜索时间复杂度为O(log K), 故总的时间复杂度为O(nlog K)。

Runtime: 22 ms, faster than 70.38% of Java online submissions for Contains Duplicate III.

Memory Usage: 40.4 MB, less than 5.58% of Java online submissions for Contains Duplicate III.

class Solution {    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {        if(nums == null || nums.length < 2 || k < 1 || t < 0){            return false;        }        TreeSet
treeSet = new TreeSet<>(); for (int i = 0; i < nums.length; i++) { if (!treeSet.subSet((long) nums[i] - t, true, (long) nums[i] + t, true).isEmpty()) { return true; } if (i >= k) { treeSet.remove((long) nums[i - k]); } treeSet.add((long) nums[i]); } return false; }}

Runtime: 987 ms, faster than 5.06% of Java online submissions for Contains Duplicate III.

Memory Usage: 38.8 MB, less than 56.75% of Java online submissions for Contains Duplicate III.

class Solution {    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {        for (int i = 0; i < nums.length; i++) {            for (int j = i + 1; j < Math.min(nums.length, i + k + 1); j++) {                if (nums[i] > 0 && nums[j] < 0 || nums[i] < 0 && nums[j] > 0) {                    if (Math.abs(nums[i]) - t > 0                         || Math.abs(nums[i]) - t + Math.abs(nums[j]) > 0) {                        continue;                    }                }                                if (Math.abs(nums[i] - nums[j]) <= t) {                    return true;                }            }        }                            return false;    }}

转载地址:https://blog.csdn.net/weixin_34248258/article/details/88600424 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:vue+node全栈移动商城【8】-vant新建注册页面
下一篇:行内元素有哪些?块级元素有哪些? 空(void)元素有那些?

发表评论

最新留言

很好
[***.229.124.182]2024年04月08日 15时35分37秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

JAVA——[MySQLNonTransientConnectionException:Could not create connection to database server.]解决方案 2019-04-28
Eclipse——Maven项目工程无法编译但可以运行解决方案 2019-04-28
RSA加密算法 2019-04-28
JAVA——RSA加密【X509EncodedKeySpec、PKCS8EncodedKeySpec、RSAPublicKeySpec、RSAPrivateKeySpec】 2019-04-28
JAVA——基于HttpClient的正方教务系统[1999-2020]模拟登录基本解决方案 2019-04-28
Java Web——文件下载getResourceAsStream()返回NULL解决方案 2019-04-28
Java Web——文件下载时中文文件名乱码问题解决方案 2019-04-28
Spring Boot——[Unable to start LiveReload server]解决方案 2019-04-28
Spring Boot——[java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration]解决方案 2019-04-28
Spring Boot——[JPA 无法注入 JpaRepository 子接口问题]解决方案 2019-04-28
Vue——组件化开发DEMO 2019-04-28
Spring Boot——基于OkHTTP的GitHub第三方登录DEMO 2019-04-28
Spring Boot——MyBatis配置带下划线命名的字段自动转换驼峰命名解决方案 2019-04-28
Spring Boot——读取.properties配置文件解决方案 2019-04-28
Thymeleaf——访问静态资源(static)解决方案 2019-04-28
Bootstrap4——字体大小根据屏幕改变解决方案 2019-04-28
Alibaba Cloud Toolkit——简介 2019-04-28
Spring Boot——[Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]解决方案 2019-04-28
JAVA——System.in作为控制台输入时结束输入(输入EOF)解决方案 2019-04-28
Visual Studio 2019 + Visual C++——创建Visual C++ Hello World! 程序 2019-04-28