<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)491:遞增子序列

          共 1468字,需瀏覽 3分鐘

           ·

          2022-01-09 20:49

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

          今天和大家聊的問題叫做?遞增子序列,我們先來看題面:
          https://leetcode-cn.com/problems/increasing-subsequences/

          Given an integer array nums, return all the different possible increasing subsequences of the given array with at least two elements. You may return the answer in any order.
          The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.

          給你一個整數(shù)數(shù)組 nums ,找出并返回所有該數(shù)組中不同的遞增子序列,遞增子序列中 至少有兩個元素 。你可以按 任意順序 返回答案。
          數(shù)組中可能含有重復元素,如出現(xiàn)兩個整數(shù)相等,也可以視作遞增序列的一種特殊情況。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          示例 1:

          輸入:nums = [4,6,7,7]
          輸出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

          示例 2:

          輸入:nums = [4,4,3,2,1]
          輸出:[[4,4]]


          解題

          https://www.cnblogs.com/kexinxin/p/10372504.html

          利用遞歸的思想,維護一個棧,將每次找到的比當前棧頂大的數(shù),然后入棧,將更新過后的棧扔進遞歸函數(shù),然后更新查找的初始位置即從當前的位置后一個位置開始查找。遞歸函數(shù)結束后,取出棧頂,進入下個循環(huán),這樣將所有元素都作為棧底元素遍歷一遍。

          public?class?Solution?{
          ????public?List> findSubsequences(int[] nums) {
          ????????Set> res = new?HashSet>();
          ????????helper(res, new?ArrayList(), nums, 0);
          ????????return?new?ArrayList>(res);
          ????}

          ????private?void?helper(Set> res, List subList, int[] nums, int?start) {
          ????????if?(subList.size() >= 2) {
          ????????????res.add(new?ArrayList(subList));
          ????????}
          ????????for?(int?i = start; i < nums.length; i++) {
          ????????????if?(subList.size() == 0?|| subList.get(subList.size() - 1) <= nums[i]) {
          ????????????????subList.add(nums[i]);
          ????????????????helper(res, subList, nums, i + 1);
          ????????????????subList.remove(subList.size() - 1);
          ????????????}
          ????????}
          ????}
          }


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

          上期推文:

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

          LeetCode刷題實戰(zhàn)481:神奇字符串

          LeetCode刷題實戰(zhàn)482:密鑰格式化

          LeetCode刷題實戰(zhàn)483:最小好進制

          LeetCode刷題實戰(zhàn)484:尋找排列

          LeetCode刷題實戰(zhàn)485:最大連續(xù) 1 的個數(shù)

          LeetCode刷題實戰(zhàn)486:預測贏家

          LeetCode刷題實戰(zhàn)487:最大連續(xù)1的個數(shù) II

          LeetCode刷題實戰(zhàn)488:祖瑪游戲

          LeetCode刷題實戰(zhàn)489:掃地機器人

          LeetCode刷題實戰(zhàn)490:迷宮


          瀏覽 28
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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无码久久久影音先锋 | 黄色电影视频在线精品 | 欧美黄色宗合一级二级三级 | 一区二区无人区 | 国产女人在线观看 |