<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                **一. 題目描述** Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library’s sort function for this problem. Follow up:? A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s,? then 1’s and followed by 2’s. Could you come up with an one-pass algorithm using only constant space? **二. 題目分析** 題目一開始說到一組對象,包含紅白藍三種顏色,然后要對他們進行排序,說白了就是對一個只含有`0, 1, 2`三個數字的數組從小到大排序。 題目要求:come up with an one-pass algorithm using only constant space,因此只能掃描一次。這里采取的方法是,統計`0, 1, 2`三個數字分別出現的次數,再將數組nums重新構建為從0到2排列的數組,這種方法沒有使用數組元素間的交換,只需掃描一次nums。 **三. 示例代碼** ~~~ #include <iostream> #include <vector> using namespace std; class Solution { public: void sortColors(vector<int>& nums) { int SIZE = nums.size(); int count[3] = {0, 0, 0}; for (int i = 0; i < SIZE; ++i) ++count[nums[i]]; for (int i = 0, index = 0; i < 3; ++i) for (int j = 0; j < count[i]; ++j) nums[index++] = i; } }; ~~~ 一個測試結果: ![](https://box.kancloud.cn/2016-01-05_568bb5f18a45c.jpg) **四. 小結** 本題的解法還是挺多的,可多參考網上的其他解法。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看