## [剑指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......

## [LeetCode C++实现]43. Multiply Strings

AC代码：

class Solution {

public:

string multiply(string num1, string num2) {

string res(num1.size()+num2.size(),'0');

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

{

int carry = 0;

for(int j = num2.size() - 1;j >= 0;j--)

{

int tmp = res[i+j+1]-'0' + (num1[i] - '0')*(num2[j]-'0') + carry;

res[i+j+1]......

## [LeetCode C++实现]148. Sort List

/**

* struct ListNode {

* int val;

* ListNode *next;

* ListNode() : val(0), next(nullptr) {}

* ListNode(int x) : val(x), next(nullptr) {}

* ListNode(int x, ListNode *next) : val(x), next(next) {}

* ......

## [LeetCode C++实现]16. 3Sum Closest

class Solution {

public:

int threeSumClosest(vector<int>& nums, int target) {

int n = nums.size();

sort(nums.begin(),nums.end());

int min_val = INT_MAX;

int res = 0;

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

int second = i + 1;

int third = n -1;......

## [LeetCode C++实现]1502. Can Make Arithmetic Progression From Sequence

class Solution {

public:

bool canMakeArithmeticProgression(vector<int>& arr) {

sort(arr.begin(),arr.end());

int diff = arr[1] - arr[0];

for(int i = 1;i < arr.size();i++)

{

if(arr[i] - arr[i-1] != diff)

return false;

}

return true;

}

};

Runtime: 4 ms, faster than 65......

## [LeetCode C++实现]151. Reverse Words in a String

class Solution {

public:

string reverseWords(string s) {

int size = s.size();

int i = 0,j = 0;

while(i < size)

{

//skip spaces

while(i < size && s[i] == ' ')

i++;

if(i < size && j > 0)

s[j++] = ' ';

int start = j;

while(i < size && s[i] != ' ')

s[j++] = s[i++];......