#include
#include
using namespace std;
class Solution {
public:
int MAX = 0;
int MoreThanHalfNum_Solution(vector<int> numbers);
bool ismorenum(vector<int> numbers, int len, int result);
vector<int> GetLeastNumbers_Solution(vector<int> input, int k);
int partition(vector<int> &input, int begin, int end);
int MAX_30 = 0;
int FindGreatestSumOfSubArray(vector<int> array);
int sumofith(vector<int> array, int i, int &result);
int NumberOf1Between1AndN_Solution(int n);
int subnumber(int n, int count);
};
int Solution::MoreThanHalfNum_Solution(vector<int> numbers) {
if (numbers.empty()) {
MAX = -1;
return 0;
}
int len = numbers.size();
int num = 1;
int result = numbers[0];
for (int i = 1; i < len; i++) {
if (num == 0) {
result = numbers[i];
num = 1;
}
else if (numbers[i] == result)
num++;
else
{
num--;
}
}
if (ismorenum(numbers, len, result)) return result;
else {
MAX = -2;
return 0;
}
}
bool Solution::ismorenum(vector<int> numbers, int len, int result) {
int num = 0;
for (vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++)
if (*it == result) num++;
if (num > (len >> 1)) return true;
return false;
}
vector<int> Solution::GetLeastNumbers_Solution(vector<int> input, int k) {
if (input.empty()) return vector<int>();
int len = input.size();
if (len < k || k <= 0) return vector<int>();
int begin = 0, end = len - 1;
int index = partition(input, begin, end);
while (index != k-1) {
if (index > k-1) {
end = index - 1;
index = partition(input, begin, end);
}
else {
begin = index + 1;
index = partition(input, begin, end);
}
}
vector<int> result;
for (int i = 0; i < k; i++)
result.push_back(input[i]);
return result;
}
int Solution::partition(vector<int> &input, int begin, int end) {
int base = input[begin];
while (begin < end) {
while(begin < end && input[end] >= base)
end--;
if (begin < end)
input[begin++] = input[end];
while (begin < end && input[begin] <= base)
begin++;
if (begin < end)
input[end--] = input[begin];
}
input[begin] = base;
return begin;
}
int Solution::FindGreatestSumOfSubArray(vector<int> array) {
if (array.empty()) {
MAX_30 = -1;
return 0;
}
int len = array.size();
int result = array[0];
int temp = sumofith(array, len - 1, result);
return result;
}
int Solution::sumofith(vector<int> array, int i, int &result) {
if (i == 0) return array[0];
int sumofiminus1 = sumofith(array, i - 1, result);
int temp = 0;
if (sumofiminus1 > 0) {
temp = sumofiminus1 + array[i];
}
else
{
temp = array[i];
}
result = result > temp ? result : temp;
return temp;
}
int Solution::NumberOf1Between1AndN_Solution(int n) {
if (n < 1) return 0;
int nums = 0;
int count = 0;
int temp = n;
while (temp) {
count++;
temp /= 10;
}
nums += subnumber(n, count);
return nums;
}
int Solution::subnumber(int n, int count) {
if (count <= 1) {
if (n >= 1) return 1;
else
{
return 0;
}
}
int result = 0;
int temp = pow(10, count - 1);
int topdigit = n / temp;
int n_temp = n - temp;
if (topdigit == 1) {
result += n_temp + 1 + topdigit * (count - 1) * (temp / 10);
}
else if (topdigit == 0);
else {
result += temp + topdigit * (count - 1) * (temp / 10);
}
result += subnumber(n - topdigit * temp, --count);
return result;
}
void test1(){
vector<int> temp = {1,2,3,3,3,4,5,4,3,3,3};
Solution s;
cout << s.MoreThanHalfNum_Solution(temp) << endl;
cout << s.MAX;
return;
}
void test2() {
vector<int> temp = { 4, 5, 1, 6, 2, 7, 3, 8 };
int k = 4;
Solution s;
vector<int> result = s.GetLeastNumbers_Solution(temp, k);
for (vector<int>::iterator it = result.begin(); it != result.end(); it++)
cout << *it << ' ';
return;
}
void test3() {
vector<int> temp = { 6,-3,-2,7,-15,1,2,2 };
Solution s;
cout << s.FindGreatestSumOfSubArray(temp);
return;
}
void test4() {
Solution s;
cout << s.NumberOf1Between1AndN_Solution(100);
return;
}
int main() {
test4();
system("pause");
return 0;
}