<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)220:存在重復(fù)元素 III

          共 2786字,需瀏覽 6分鐘

           ·

          2021-03-25 14:07

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 存在重復(fù)元素 III,我們先來看題面:
          https://leetcode-cn.com/problems/contains-duplicate-iii/

          Given an integer array nums and two integers k and t, return true if there are two distinct indices i and j in the array such that abs(nums[i] - nums[j]) <= t and abs(i - j) <= k.

          在整數(shù)數(shù)組 nums 中,是否存在兩個下標 i 和 j,使得 nums [i] 和 nums [j] 的差的絕對值小于等于 t ,且滿足 i 和 j 的差的絕對值也小于等于 ? 。

          如果存在則返回 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


          解題


          注意:整數(shù)溢出的情況
          利用set的lower_bound函數(shù)返回>= key的元素,不存在時返回end()
          it = s.lower_bound(nums[i] - long(t));
          if(it != s.end() && (*it - nums[i]) <= t) {
          return true;
          }
          i和j之間差的絕對值最大為k的元素保存到set容器中去
          if(s.size() > k) {
          s.erase(nums[i - k]);
          }


          class Solution {
          public:
              bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
                  set<long> s;
                  for(int i = 0; i < nums.size(); i++) {
                      auto it = s.lower_bound(nums[i] - long(t));
                      if(it != s.end() && *it - nums[i] <= t) {
                          return true;
                      }
                      s.insert(nums[i]);
                      if(s.size() > k) {
                          s.erase(nums[i - k]);
                      }
                  }
                  
                  return false;
              }
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-200題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)201:數(shù)字范圍按位與

          LeetCode刷題實戰(zhàn)202:快樂數(shù)

          LeetCode刷題實戰(zhàn)203:移除鏈表元素

          LeetCode刷題實戰(zhàn)204:計數(shù)質(zhì)數(shù)

          LeetCode刷題實戰(zhàn)205:同構(gòu)字符串

          LeetCode刷題實戰(zhàn)206:反轉(zhuǎn)鏈表

          LeetCode刷題實戰(zhàn)207:課程表

          LeetCode刷題實戰(zhàn)208:實現(xiàn) Trie (前綴樹)

          LeetCode刷題實戰(zhàn)209:長度最小的子數(shù)組

          LeetCode刷題實戰(zhàn)210:課程表 II

          LeetCode刷題實戰(zhàn)211:添加與搜索單詞

          LeetCode刷題實戰(zhàn)212:單詞搜索 II

          LeetCode刷題實戰(zhàn)213:打家劫舍 II

          LeetCode刷題實戰(zhàn)214:最短回文串

          LeetCode刷題實戰(zhàn)215:數(shù)組中的第K個最大元素

          LeetCode刷題實戰(zhàn)216:組合總和 III

          LeetCode刷題實戰(zhàn)217:存在重復(fù)元素

          LeetCode刷題實戰(zhàn)218:天際線問題


          瀏覽 49
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  在线观看黄色av 欧美国产精品一二三产品在哪买 | 欧美日韩A级 | 第四色大香蕉 | 在线看一区二区三区四区 | 成人蜜桃网 |