<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 旋轉幾次后,在給定索引處查找元素 > 原文: [https://www.geeksforgeeks.org/find-element-given-index-number-rotations/](https://www.geeksforgeeks.org/find-element-given-index-number-rotations/) 給出了一個由 N 個整數組成的數組。 我們執行范圍為`[L..R]`的[右旋轉](https://www.geeksforgeeks.org/array-rotation/)。 完成這些旋轉之后,我們需要在給定索引處找到元素。 **示例**: ``` Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation : After first given rotation {0, 2} arr[] = {3, 1, 2, 4, 5} After second rotation {0, 3} arr[] = {4, 3, 1, 2, 5} After all rotations we have element 3 at given index 1\. ``` **方法:暴力**暴力方法是針對所有給定范圍實際[旋轉數組](https://www.geeksforgeeks.org/array-rotation/),最后返回修改后數組中給定索引處的元素。 **方法:高效**保存所有范圍后,我們可以進行脫機處理。 假設我們的旋轉范圍是:[0..2]和[0..3] 我們從反向開始遍歷這些范圍。 在范圍`[0..3]`之后,索引 0 將具有位于索引 3 上的元素。 在范圍`[0..2]`之后,索引 3 將保持不變。 因此,我們可以得出 3 種情況:如果`index = left`,索引將變為`right`。如果索引不受范圍限制,則沒有旋轉效果。如果`index`處于范圍內,則`index`的元素將位于`index-1`處。 下面是實現: ## C++ ```cpp // CPP code to rotate an array // and answer the index query #include <bits/stdc++.h> using namespace std; // Function to compute the element at // given index int findElement(int arr[], int ranges[][2], ???????????????int rotations, int index) { ????for (int i = rotations - 1; i >= 0; i--) { ????????// Range[left...right] ????????int left = ranges[i][0]; ????????int right = ranges[i][1]; ????????// Rotation will not have any effect ????????if (left <= index && right >= index) { ????????????if (index == left) ????????????????index = right; ????????????else ????????????????index--; ????????} ????} ????// Returning new element ????return arr[index]; } // Driver int main() { ????int arr[] = { 1, 2, 3, 4, 5 }; ????// No. of rotations ????int rotations = 2; ????// Ranges according to 0-based indexing ????int ranges[rotations][2] = { { 0, 2 }, { 0, 3 } }; ????int index = 1; ????cout << findElement(arr, ranges, rotations, index); ????return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看