forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04c1009
commit 2708d92
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0900) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main.cpp) | ||
add_executable(cpp_0900 ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// Source : https://leetcode.com/problems/rle-iterator/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Ad-Hoc | ||
/// Time Complexity: init: O(n) | ||
/// next: O(n) | ||
/// Space Complexity: O(n) | ||
class RLEIterator { | ||
|
||
private: | ||
vector<int> A; | ||
int curnum_index = 1, index = 0; | ||
|
||
public: | ||
RLEIterator(vector<int> A) { | ||
for(int a: A) | ||
this->A.push_back(a); | ||
} | ||
|
||
int next(int n) { | ||
|
||
if(curnum_index >= A.size()) | ||
return -1; | ||
|
||
index += n; | ||
while(curnum_index < A.size() && index > A[curnum_index - 1]){ | ||
index -= A[curnum_index - 1]; | ||
curnum_index += 2; | ||
} | ||
|
||
if(curnum_index < A.size() && index <= A[curnum_index - 1]) | ||
return A[curnum_index]; | ||
return -1; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
vector<int> A = {3, 8, 0, 9, 2, 5}; | ||
RLEIterator rleIterator(A); | ||
cout << rleIterator.next(2) << endl; // 8 | ||
cout << rleIterator.next(1) << endl; // 8 | ||
cout << rleIterator.next(1) << endl; // 5 | ||
cout << rleIterator.next(2) << endl; // -1 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -503,4 +503,5 @@ email: [[email protected]](mailto:[email protected]) | |
| 897 | [Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/description/) | [solution](https://leetcode.com/problems/increasing-order-search-tree/solution/) | [C++](0897-Increasing-Order-Search-Tree/cpp-0897/) | | | | ||
| 898 | [Bitwise ORs of Subarrays](https://leetcode.com/problems/bitwise-ors-of-subarrays/description/) | [solution](https://leetcode.com/problems/bitwise-ors-of-subarrays/solution/) | [C++](0898-Bitwise-ORs-of-Subarrays/cpp-0898/) | | | | ||
| 899 | [Orderly Queue](https://leetcode.com/problems/orderly-queue/description/) | [solution](https://leetcode.com/problems/orderly-queue/solution/) | [C++](0899-Orderly-Queue/cpp-0899/) | | | | ||
| 900 | [RLE Iterator](https://leetcode.com/problems/rle-iterator/description/) | [solution](https://leetcode.com/problems/rle-iterator/solution/) | [C++](0900-RLE-Iterator/cpp-0900/) | | | | ||
| | | | | | | |