[LeetCode C++实现]41. First Missing Positive

class Solution {

public:

int firstMissingPositive(vector<int>& nums) {

int n = nums.size();

for(int i = 0;i < n;i++)

{

if(nums[i] <= 0)

nums[i] = n + 1;

}

for (int i = 0; i < n; ++i) {

int num = abs(nums[i]);

if (num <= n) {

nums[num - 1] = -abs(nums[num - 1]);

}

}

for(int i......

[LeetCode C++实现]29. Divide Two Integers

class Solution {

public:

int divide(int dividend, int divisor) {

if(dividend == INT_MIN && divisor == -1) return INT_MAX;

long long A = llabs(dividend),B = llabs(divisor),ans = 0;

while(A >= B)

{

long long base = B,m = 1;

while(base << 1 <= A)

{

base <<= 1;

m <<= 1;

......

[LeetCode C++实现]912. Sort an Array

冒泡排序

class Solution {

public:

vector<int> sortArray(vector<int>& nums) {

for(int i = nums.size()-1;i > 0;i--)

{

for(int j = 0;j < i;j++)

if(nums[j] > nums[j+1])

swap(nums[j],nums[j+1]);

}

return nums;

}

};

执行结果:

Time Limit Exceeded

Details

Last executed input

[5864,-1......

[LeetCode C++实现]1114. Print in Order

#include <semaphore.h>

class Foo {

protected:

sem_t firstJobDone;

sem_t secondJobDone;

public:

Foo() {

sem_init(&firstJobDone, 0, 0);

sem_init(&secondJobDone, 0, 0);

}

void first(function<void()> printFirst) {

// printFirst() outputs "first". Do not change or remove th......

[剑指offer C++实现]剑指 Offer 43. 1~n 整数中 1 出现的次数

输入一个整数 n ,求1~n这n个整数的十进制表示中1出现的次数。

例如,输入12,1~12这些整数中包含1 的数字有1、10、11和12,1一共出现了5次。

class Solution {

public:

int countDigitOne(int n) {

int res = 0;

for(int i = 1;i <= n;i++)

{

res += helper(i);

}

return res;

}

private:

int helper(int n)

{

int cnt = 0;

while(n)

{

if(n%10 == 1)

cnt++;

n /= 10;

}

r......

[剑指offer C++实现]剑指 Offer 50. 第一个只出现一次的字符

方法一:记录每个字符出现次数

class Solution {

public:

char firstUniqChar(string s) {

unordered_map<char,int> hash;

for(auto c:s)

hash[c] += 1;

for(int i = 0;i < s.size();i++)

{

if(hash[s[i]] == 1)

return s[i];

}

return ' ';

}

};

方法二:

class Solution {

public:

char firstUniqChar(string s) {

unorde......

[LeetCode C++实现]343. Integer Break

递归解法

class Solution {

public:

int integerBreak(int n) {

if(n <= 2) return 1;

int ans = INT_MIN;

for(int i = 2;i < n;i++)

{

int product = i * max(n-i,integerBreak(n-i));

if(product > ans)

ans = product;

}

return ans;

}

};

运行结果:

Time Limit Exceeded

Details

Last executed input

43

......

[剑指offer C++实现]剑指 Offer 05. 替换空格

自己实现的方法一,思路是先获取空格的个数,然后根据空格个数申请新的字符串长度。

class Solution {

public:

string replaceSpace(string s) {

int size = s.size();

int cnt = 0,loc = 0;

for(int i = 0;i < size;i++)

if(s[i] == ' ')

cnt++;

string res(size + 2*cnt,'0');

for(int i = 0;i < size;i++)

{

if(s[i] != ' ')

{

......

[LeetCode C++实现]137. Single Number II

class Solution {

public:

int singleNumber(vector<int>& nums) {

int size = nums.size();

int ans = 0;

for(int i = 0;i < 32;i++)

{

int sum = 0;

for(auto num:nums)

{

sum += ((num >> i)&1);

}

if(sum % 3)

ans |= (1 << i);

}

return ans;

}

};

运行效率:

Runtime: 4 ms, faster t......

[LeetCode C++实现]260. Single Number III

class Solution {

public:

vector<int> singleNumber(vector<int>& nums) {

int sum = 0,flag = 1;

vector<int> ans(2,0);

for(int i = 0;i < nums.size();i++)

sum ^= nums[i];

while(!(sum & flag))

flag <<= 1;

for(int i = 0;i < nums.size();i++)

{

if(flag & nums[i])

an......