Skip to content

Commit cb6e588

Browse files
committed
update
1 parent e3b8663 commit cb6e588

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class StringIterator {
5+
public:
6+
StringIterator(string compresult_sedString)
7+
: result_(compresult_sedString),
8+
num_(0),
9+
ch_(' ') {
10+
11+
}
12+
13+
char next() {
14+
if (!hasNext()) {
15+
return ' ';
16+
}
17+
if (num_ == 0) {
18+
result_ >> ch_ >> num_;
19+
}
20+
--num_;
21+
return ch_;
22+
}
23+
24+
bool hasNext() {
25+
return !result_.eof() || num_ != 0;
26+
}
27+
28+
private:
29+
istringstream result_;
30+
int num_;
31+
char ch_;
32+
};
33+
34+
/**
35+
* Your StringIterator object will be instantiated and called as such_:
36+
* StringIterator obj = new StringIterator(compresult_sedString);
37+
* ch_ar param_1 = obj.next();
38+
* bool param_2 = obj.hasNext();
39+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int findIntegers(int num) {
7+
vector<int> dp(32);
8+
dp[0] = 1;
9+
dp[1] = 2;
10+
for (int i = 2; i < dp.size(); ++i) {
11+
dp[i] = dp[i - 1] + dp[i - 2];
12+
}
13+
int result = 0, prev_bit = 0;
14+
for (int i = 30; i >= 0; --i) {
15+
if ((num & (1 << i)) != 0) {
16+
result += dp[i];
17+
if (prev_bit == 1) {
18+
--result;
19+
break;
20+
}
21+
prev_bit = 1;
22+
} else {
23+
prev_bit = 0;
24+
}
25+
}
26+
return result + 1;
27+
}
28+
};

0 commit comments

Comments
 (0)