leetcode 220. 存在重復元素 III(排序)

給你一個整數數組 nums 和兩個整數 k 和 t 。請你判斷是否存在 兩個不同下標 i 和 j,使得 abs(nums[i] - nums[j]) <= t ,同時又滿足 abs(i - j) <= k 。

如果存在則返回 true,不存在返回 false。

示例 1:

輸入:nums = [1,2,3,1], k = 3, t = 0
輸出:true
示例 2:

輸入:nums = [1,0,1,1], k = 1, t = 2
輸出:true
示例 3:

輸入:nums = [1,5,9,1,5,9], k = 2, t = 3
輸出:false

解題思路

用二維數組將元素和元素下標進行綁定,按元素大小進行排序,每個元素都在滿足差值范圍內盡可能向后遍歷,檢查判斷后面元素的下標是否滿足范圍要求

代碼

class Solution {public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {int n=nums.length;int[][] ints = new int[n][2];for(int i=0;i<n;i++){ints[i][0]=i;ints[i][1]=nums[i];}Arrays.sort(ints,(o1, o2) -> o1[1]-o2[1]);for(int i=0;i<n;i++){int next=i+1;while (next<n&&Math.abs((long)ints[next][1]-(long)ints[i][1])<=t){if(Math.abs((long)ints[next][0]-(long)ints[i][0])<=k)return true;next++;}}return false;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391401.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391401.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391401.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ON DUPLICATE KEY UPDATE

INSERT INTO ON DUPLICATE KEY UPDATE 與 REPLACE INTO&#xff0c;兩個命令可以處理重復鍵值問題&#xff0c;在實際上它之間有什么區別呢&#xff1f;前提條件是這個表必須有一個唯一索引或主鍵。1、REPLACE發現重復的先刪除再插入&#xff0c;如果記錄有多個字段&#xff0c…

os.path 模塊

os.path.abspath(path) #返回絕對路徑os.path.basename(path) #返回文件名os.path.commonprefix(list) #返回list(多個路徑)中&#xff0c;所有path共有的最長的路徑。os.path.dirname(path) #返回文件路徑os.path.exists(path) #路徑存在則返回True,路徑損壞返回Falseos.path…

探索性數據分析(EDA):Python

什么是探索性數據分析(EDA)&#xff1f; (What is Exploratory Data Analysis(EDA)?) If we want to explain EDA in simple terms, it means trying to understand the given data much better, so that we can make some sense out of it.如果我們想用簡單的術語來解釋EDA&a…

微服務框架---搭建 go-micro環境

1.安裝micro 需要使用GO1.11以上版本 #linux 下 export GO111MODULEon export GOPROXYhttps://goproxy.io # windows下設置如下環境變量 setx GO111MODULE on setx GOPROXY https://goproxy.io # 使用如下指令安裝 go get -u -v github.com/micro/micro go get -u -v github.co…

angular dom_Angular 8 DOM查詢:ViewChild和ViewChildren示例

angular domThe ViewChild and ViewChildren decorators in Angular provide a way to access and manipulate DOM elements, directives and components. In this tutorial, well see an Angular 8 example of how to use the two decorators.Angular中的ViewChild和ViewChild…

浪潮之巔——IT產業的三大定律

http://www.cnblogs.com/ysocean/p/7641540.html轉載于:https://www.cnblogs.com/czlovezmt/p/8325772.html

DStream算子講解(一)

先把目錄列好&#xff0c;方便有條理的進行整理轉載于:https://www.cnblogs.com/leodaxin/p/7507600.html

aws 靜態網站_如何使用AWS托管靜態網站-入門指南

aws 靜態網站When I created my first portfolio last year, I based it on what I had learned from freeCodeCamp (HTML, CSS and a little JavaScript). 去年創建我的第一個投資組合時 &#xff0c;我基于從freeCodeCamp (HTML&#xff0c;CSS和一些JavaScript)中學到的知識…

leetcode 27. 移除元素(雙指針)

給你一個數組 nums 和一個值 val&#xff0c;你需要 原地 移除所有數值等于 val 的元素&#xff0c;并返回移除后數組的新長度。 不要使用額外的數組空間&#xff0c;你必須僅使用 O(1) 額外空間并 原地 修改輸入數組。 元素的順序可以改變。你不需要考慮數組中超出新長度后面…

使用TVP批量插入數據

TVP&#xff08;全稱 :Table-Valued Parameter&#xff09; 叫做表值參數(Table-Valued Parameter)是SQL2008的一個新特性。顧名思義&#xff0c;表值參數表示你可以把一個表類型作為參數傳遞到函數或存儲過程里。 第一步&#xff1a;創建一個Type類型和寫入數據的原始表結構相…

python:找出兩個列表中相同和不同的元素(使用推導式)

#接口返回值 list1 [張三, 李四, 王五, 老二] #數據庫返回值 list2 [張三, 李四, 老二, 王七]a [x for x in list1 if x in list2] #兩個列表表都存在 b [y for y in (list1 list2) if y not in a] #兩個列表中的不同元素print(a的值為:,a) print(b的值為:,b)c [x for x …

springcloud(六):配置中心git示例

隨著線上項目變的日益龐大&#xff0c;每個項目都散落著各種配置文件&#xff0c;如果采用分布式的開發模式&#xff0c;需要的配置文件隨著服務增加而不斷增多。某一個基礎服務信息變更&#xff0c;都會引起一系列的更新和重啟&#xff0c;運維苦不堪言也容易出錯。配置中心便…

寫作工具_4種加快數據科學寫作速度的工具

寫作工具I’ve been writing about data science on Medium for just over two years. Writing, in particular, technical writing can be time-consuming. Not only do you need to come up with an idea, write well, edit your articles for accuracy and flow, and proofr…

leetcode 91. 解碼方法(dp)

解題思路 記憶化搜索&#xff0c;記錄已經計算過的子問題 代碼 func numDecodings(s string) int {temp:make([]int,len(s),len(s))for i : range temp {temp[i]-1}return de(s,0,temp) } func de(s string,cur int,dp []int) int {if curlen(s){return 1}if dp[cur]!-1{re…

python數據結構與算法

2019獨角獸企業重金招聘Python工程師標準>>> http://python.jobbole.com/tag/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/ 轉載于:https://my.oschina.net/u/3572879/blog/1611369

test5

test5 轉載于:https://www.cnblogs.com/Forever77/p/11468284.html

ux和ui_閱讀10個UI / UX設計系統所獲得的經驗教訓

ux和uiAs a way to improve my UI/UX skills I decided to read the guidelines for 10 popular UI/UX design systems. In this article I will give you a concise summary of the most important concepts. 為了提高我的UI / UX技能&#xff0c;我決定閱讀10種流行的UI / UX…

大數據(big data)_如何使用Big Query&Data Studio處理和可視化Google Cloud上的財務數據...

大數據(big data)介紹 (Introduction) This article will show you one of the ways you can process stock price data using Google Cloud Platform’s BigQuery, and build a simple dashboard on the processed data using Google Data Studio.本文將向您展示使用Google Cl…

第1次作業:閱讀優秀博文談感想

摘要&#xff1a;本文介紹第1次作業的詳細內容&#xff0c;包括評分標準。 注&#xff1a;本次作業提交截止時間為UTC8(北京時間)&#xff0c;2017-9-17 22:00&#xff08;星期日&#xff09;&#xff0c;以博客發表日期為準。 1. 作業內容 閱讀一些優秀博文&#xff08;見第二…

ubuntu 16.04常用命令

ip配置&#xff1a; 終端輸入vi /etc/network/interfaces命令編輯配置文件,增加如下內容&#xff1a;         auto enp2s0    iface enp2s0 inet static    address 192.168.1.211    netmask 255.255.255.0    gateway 192.168.1.1 重啟網卡&#xf…