meyr543
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Leetcode刷題學習筆記 -- Stack ### Note 1. 使用 st.top()或是st.pop() 之前==必須先判斷stack是否為empty()。== 2. monotone stack: stack中保持一定的大小順序。 ### 使用vector/string來模擬stack 我們可以在vector/string中使用two pointer來模擬stack,這樣的好處是可以減少很多不必要的push/pop。 [1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/) ```cpp= // 把i當成是stack的top,這樣的話0就會表示有東西, // 所以i == -1才會表示沒stack empty。 string s{"abbaca"}; int i = -1; // stack is empty for(int j = 0; j < sz; ++j) { // 當stack不是empty,且stack top == s[j] // 則 stack pop(--i) if(i != -1 && s[i] == s[j]) --i; else s[++i] = s[j]; // stack push } return i == -1 ? "" : s.substr(0, i + 1); ``` #### stack initialization ```cpp int i = -1; ``` #### check if stack is empty ```cpp boolean isEmpty() { return i == -1; } ``` #### stack push ```cpp s[++i] = s[j]; // 因為i指在top,所以必須先++i ``` ### stack pop ```cpp --i; // 直接把index - 1,當empty的時候會變成-1。 ``` ### [155. Min Stack(Easy)](https://leetcode.com/problems/min-stack/) 實現一個MinStack,除了一般stack的功能push(), pop()和top()之外,還可以取得stack中的最小值getMin()。 > 1. 這邊使用兩個stack,一個正常使用s1,一個是monotonic stack(遞減) s2,來記錄最小值。 > 2. push的時候,因為s2為monotonic所以只推比top還小的值。 > 3. pop的時候,如果s1和s2的top一樣,則兩個一起pop。 > 4. 因為s2有著和s1一樣的順續,只是他只把比自己小的推進去,因為是min stack。 ```cpp= class MinStack { stack<int> s1, s2; public: MinStack() {} void push(int val) { if(s2.empty() || val <= s2.top()) s2.push(val); s1.push(val); } void pop() { if(s1.top() == s2.top()) s2.pop(); s1.pop(); } int top() {return s1.top();} int getMin() {return s2.top();} }; ``` ### [316. Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/) 刪除重複出現的字元,每個字元只能出現一次,並且照字典排序。 ```c string removeDuplicateLetters(string s) { // string 可以使為stack操作 int m[256] = {0}, visited[256] = {0}; // 使用256避免char<->int轉換,減少失誤 string res = "0"; for (auto a : s) ++m[a]; for (auto a : s) { --m[a]; // 後面還剩多少個 if(visited[a]) continue; // 已經在裡面了就繼續,因為題目說只能出現一次 while(a < res.back() && m[res.back()]) { // 目前的char比最後一個還小,而且back()在後面還有 visited[res.back()] = 0; res.pop_back(); } res.push_back(a); visited[a] = 1; } return res.substr(1); } ``` ### [1209. Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/description/) 這題為[1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/description/)的延伸題,原本是兩個一樣就可以刪除,現在是K個一樣才可以刪除。原本的題目是K=2。 > 1. 可以使用stack<pair<char, int>>多一個欄位來儲存char的次數。 > 2. 因為是stack也可以使用two pointers來模擬stack。 > 3. method1和method2都使用額外的空間紀錄count,所以返回的答案也必須要根據count重建。 ```cpp string removeDuplicates(string s, int k) { // method 1 : stack stack<pair<char, int>> st; for(auto& c :s) { if(!st.empty() && st.top().first == c) { if(st.top().second == k - 1) st.pop(); else st.top().second++; } else st.push(make_pair(c, 1)); } string rtn{""}; while(!st.empty()) { rtn = string(st.top().second, st.top().first) + rtn; st.pop(); } return rtn; } ``` ```cpp string removeDuplicates(string s, int k) { // method 2 : two pointer int sz = s.size(); int i = -1; vector<int> cnt(sz); for(int j = 0; j < sz; ++j) { if(~i && s[i] == s[j]) { if(cnt[i] == k - 1) --i; else cnt[i]++; } else { s[++i] = s[j]; cnt[i] = 1; } } if(i == -1) return ""; else { string rtn{""}; for(int j = 0; j <= i; ++j) rtn += string(cnt[j], s[j]); return rtn; } } ``` ### [895. Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/description/) 設計一個stack,pop的時候是把最大freq的element丟出來。 > 1. 一開始使用兩個unordered_map,一個統計數量,一個統計每個freq的element。這樣做每次push或是pop都在搬移element出現在freq的位置。 > 2. 參考答案,使用unordered_map<int, stack<int>>。不同的頻率有不同的stack。因為不同頻率的n,可以在stack中被排列。 > 3. 一個stack可以被拆成3個stack,如下圖 ``` ex: [5, 7, 5, 7, 4, 5] freq1-stack | | | freq2-stack | | freq3-stack | <-- maxfreq ``` ```cpp class FreqStack { unordered_map<int, int> freq; //value, count unordered_map<int, stack<int>> m; //freq, stack<value> int maxfreq{0}; // 因為一個數(n)可以表示為, // freq1-n, freq2-n, freq3-n, ... 每個出現頻率的n // 所以使用unordered_map<int, stack<int>> m; 來表示每個頻率的stack public: FreqStack() { } void push(int val) { freq[val]++; m[freq[val]].push(val); maxfreq = max(maxfreq, freq[val]); } int pop() { int rtn = m[maxfreq].top(); m[maxfreq].pop(); freq[rtn]--; if(m[maxfreq].size() == 0) maxfreq--; return rtn; } }; ``` ###### tags: `leetcode` `刷題`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully