Skip to content

Commit c5ff47d

Browse files
committed
solved some problems.
1 parent 85f69c8 commit c5ff47d

File tree

4 files changed

+137
-0
lines changed
  • 165.Compare Version Numbers/coderfive
  • 166.Fraction to Recurring Decimal/coderfive
  • 168.Excel Sheet Column Title/coderfive
  • 169.Majority Element/coderfive

4 files changed

+137
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <string>
2+
#include <iostream>
3+
using std::string;
4+
5+
class Solution {
6+
public:
7+
int compareVersion(string v1, string v2) {
8+
std::string::size_type b1 = 0, b2 = 0;
9+
std::string::size_type n1, n2;
10+
while (b1 < v1.size() || b2 < v2.size()) {
11+
if (b1 < v1.size())
12+
n1 = std::stoi(v1.substr(b1));
13+
else
14+
n1 = 0;
15+
if (b2 < v2.size())
16+
n2 = std::stoi(v2.substr(b2));
17+
else
18+
n2 = 0;
19+
if (n1 < n2) return -1;
20+
else if (n1 > n2) return 1;
21+
b1 = v1.find('.', b1);
22+
b2 = v2.find('.', b2);
23+
if (b1 == std::string::npos) {
24+
b1 = v1.size();
25+
}
26+
else {
27+
b1++;
28+
}
29+
if (b2 == std::string::npos) {
30+
b2 = v2.size();
31+
}
32+
else {
33+
b2++;
34+
}
35+
}
36+
return 0;
37+
}
38+
};
39+
40+
int main() {
41+
std::cout << Solution().compareVersion("1.3", "1.4") << std::endl;
42+
43+
return 0;
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <string>
2+
#include <algorithm>
3+
#include <iterator>
4+
#include <iostream>
5+
#include <vector>
6+
#include <sstream>
7+
using std::string;
8+
9+
class Solution {
10+
public:
11+
string fractionToDecimal(int arg_num, int arg_den) {
12+
long long numerator = arg_num, denominator = arg_den;
13+
std::ostringstream buf;
14+
if ((numerator < 0 && denominator > 0) || (numerator > 0 && denominator < 0))
15+
buf << '-';
16+
numerator = std::abs(numerator);
17+
denominator = std::abs(denominator);
18+
long long d = numerator/denominator;
19+
numerator %= denominator;
20+
buf << d;
21+
if (numerator != 0)
22+
buf << '.';
23+
else
24+
return buf.str();
25+
std::vector<int> num;
26+
std::vector<int> div;
27+
while (numerator != 0) {
28+
auto it = std::find(num.begin(), num.end(), numerator);
29+
if (it != num.end()) {
30+
auto i = it-num.begin();
31+
std::copy(div.begin(), div.begin()+i, std::ostream_iterator<int>(buf, ""));
32+
buf << '(';
33+
std::copy(div.begin()+i, div.end(), std::ostream_iterator<int>(buf, ""));
34+
buf << ')';
35+
return buf.str();
36+
}
37+
num.push_back(numerator);
38+
numerator *= 10;
39+
d = numerator/denominator;
40+
div.push_back(d);
41+
numerator %= denominator;
42+
}
43+
std::copy(div.begin(), div.end(), std::ostream_iterator<int>(buf, ""));
44+
return buf.str();
45+
}
46+
};
47+
48+
49+
int main() {
50+
std::cout << Solution().fractionToDecimal(1, 3) << std::endl
51+
<< Solution().fractionToDecimal(1, 2) << std::endl
52+
<< Solution().fractionToDecimal(2, 1) << std::endl
53+
<< Solution().fractionToDecimal(-2147483648, -1) << std::endl;
54+
55+
return 0;
56+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string convertToTitle(int n) {
4+
string res;
5+
int t;
6+
do {
7+
t = n%26;
8+
if (t == 0) {
9+
res.push_back('Z');
10+
n -= 26;
11+
}
12+
else {
13+
n -= t;
14+
res.push_back(t-1+'A');
15+
}
16+
n /= 26;
17+
} while (n);
18+
std::reverse(res.begin(), res.end());
19+
return res;
20+
}
21+
};

169.Majority Element/coderfive/169.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int majorityElement(vector<int>& nums) {
4+
int res = nums[0];
5+
int count = 0;
6+
for (auto n : nums) {
7+
if (n == res) count++;
8+
else count--;
9+
if (count <= 0) {
10+
res = n;
11+
count = 1;
12+
}
13+
}
14+
return res;
15+
}
16+
};

0 commit comments

Comments
 (0)