<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)360:有序轉(zhuǎn)化數(shù)組

          共 4932字,需瀏覽 10分鐘

           ·

          2021-08-24 17:27

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

          今天和大家聊的問題叫做 有序轉(zhuǎn)化數(shù)組,我們先來看題面:
          https://leetcode-cn.com/problems/sort-transformed-array/

          Given a sorted array of integers nums and integer values a, band c. Apply a quadratic function of the form f(x) = ax2 + bx + c to each element x in the array. The returned array must be in sorted order. 

          Expected time complexity: O(n)

          給你一個已經(jīng) 排好序 的整數(shù)數(shù)組 nums 和整數(shù) a、b、c。對于數(shù)組中的每一個數(shù) x,計算函數(shù)值 f(x) = ax2 + bx + c,請將函數(shù)值產(chǎn)生的數(shù)組返回。
          要注意,返回的這個數(shù)組必須按照 升序排列,并且我們所期望的解法時間復雜度為 O(n)。

          示例


          示例 1:
          輸入: nums = [-4,-2,2,4], a = 1, b = 3, c = 5
          輸出: [3,9,15,33]

          示例 2:
          輸入: nums = [-4,-2,2,4], a = -1, b = 3, c = 5
          輸出: [-23,-5,1,7]


          解題

          https://blog.csdn.net/weixin_44171872/article/details/108762678
          主要思路:
          (1)理解二次曲線的開口方向?qū)φ麄€輸入數(shù)組對應(yīng)的結(jié)果的單調(diào)性的影響是關(guān)鍵點,而二次曲線的開口方向是由變量a的正負確定的;
          (2)故分情況討論,開口向下時,使用雙指針,從兩端到中間遍歷,將找到的較小值從0的位置開始,放到結(jié)果數(shù)組中;
          (3)開口向上時,使用雙指針,從兩端到中間遍歷,將找到的較大值從 nums.size()-1 的位置開始,放到結(jié)果數(shù)組中;


          class Solution {
          public:
              vector<int> sortTransformedArray(vector<int>& nums, int a, int b, int c) {
                  vector<int> res(nums.size(),0);
                  
                  int left=0;
                  int right=nums.size()-1;
                  for(int& n:nums){//先計算出各個元素對應(yīng)的值
                      n=a*n*n+b*n+c;
                  }
                  //開口向上
                  if(a>0){
                    //從數(shù)組中的較大值從 nums.size()-1 的位置開始,逐個的放入結(jié)果數(shù)組中
                      int pos=nums.size()-1;
                      while(left<=right){
                          if(nums[left]<=nums[right]){
                              res[pos--]=nums[right--];
                          }
                          else{
                              res[pos--]=nums[left++];
                          }
                      }
                  }
                  else{
                     //從數(shù)組中的較小值從 0 的位置開始,逐個的放入結(jié)果數(shù)組中
                      int pos=0;
                       while(left<=right){
                          if(nums[left]<=nums[right]){
                              res[pos++]=nums[left++];
                          }
                          else{
                              res[pos++]=nums[right--];
                          }
                      }
                  }
                  return res;//返回結(jié)果
              }
          };


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

          上期推文:

          LeetCode1-340題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)341:扁平化嵌套列表迭代器
          LeetCode刷題實戰(zhàn)342:4的冪
          LeetCode刷題實戰(zhàn)343:整數(shù)拆分
          LeetCode刷題實戰(zhàn)344:反轉(zhuǎn)字符串
          LeetCode刷題實戰(zhàn)345:反轉(zhuǎn)字符串中的元音字母
          LeetCode刷題實戰(zhàn)346:數(shù)據(jù)流中的移動平均值
          LeetCode刷題實戰(zhàn)347:前 K 個高頻元素
          LeetCode刷題實戰(zhàn)348:設(shè)計井字棋
          LeetCode刷題實戰(zhàn)349:兩個數(shù)組的交集
          LeetCode刷題實戰(zhàn)350:兩個數(shù)組的交集 II
          LeetCode刷題實戰(zhàn)351:安卓系統(tǒng)手勢解鎖
          LeetCode刷題實戰(zhàn)352:將數(shù)據(jù)流變?yōu)槎鄠€不相交區(qū)間
          LeetCode刷題實戰(zhàn)353:貪吃蛇
          LeetCode刷題實戰(zhàn)354:俄羅斯套娃信封問題
          LeetCode刷題實戰(zhàn)355:設(shè)計推特
          LeetCode刷題實戰(zhàn)356:直線鏡像
          LeetCode刷題實戰(zhàn)357:計算各個位數(shù)不同的數(shù)字個數(shù)
          LeetCode刷題實戰(zhàn)358:K 距離間隔重排字符串

          瀏覽 48
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  久久性爱视频<精工厂 | 日韩视频不卡 | 亚洲无码精品在线观看 | 亚洲韩日在线 | 青青草视频两男一女在线看 |