[LeetCode C++实现]225. Implement Stack using Queues

225. Implement Stack using Queues这里使用队列实现stack,使用c++ stl中的deque.

class MyStack {

public:

/** Initialize your data structure here. */

MyStack() {

}

/** Push element x onto stack. */

void push(int x) {

stack.push_back(x);

}

/** Removes the element on top of the stack and returns that element. */......

[LeetCode C++实现]704. Binary Search

经典面试题二分查找,当年校招面试经常遇到,先用最简单最直接的方法AC。

class Solution {

public:

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

int start = 0,end = nums.size() - 1;

while(start <= end)

{

int mid = (start + end) / 2;

if(nums[mid] == target)

return mid;

else if(nums[mid] < target)

start = mid + 1;

else

en......

[LeetCode C++实现]233. Number of Digit One

233. Number of Digit One

老规矩,上来先暴力破解,一眼看上去非常简单,1分钟写出答案,最后Time Limit Exceeded.

class Solution {

public:

int countDigitOne(int n) {

int res = 0;

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

{

res += cal_digit(i);

}

return res;

}

private:

int cal_digit(int n){

int cnt = 0;

while(n)

{

if(n % 10 == 1)

cnt++;

n /= 10......

[LeetCode C++实现]69. Sqrt(x)

69. Sqrt(x)

暴力破解求解:使用最直观的解法,性能也符合预期,非常之慢。

class Solution {

public:

int mySqrt(int x) {

int ans = 0;

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

{

//avoid overflow

if(i > x / i)

return ans;

ans = i;

}

return ans;

}

};

Runtime: 68 ms, faster than 5.95% of C++ online submissions for Sqrt(x).

Memory Usage: 6......

[LeetCode C++实现]172. Factorial Trailing Zeroes

非常有意思的一道题目,读研的时候当家教,在一个小学奥数辅导班上见到过这个题,先介绍阶乘的概念,然后问结果中有多少个0,很多年过去了,再看这道题感觉很有缘分。

class Solution {

public:

int trailingZeroes(int n) {

int cnt = 0;

while(n) cnt += n/5,n /= 5;

return cnt;

}

};

结果中有0,实际是判断5的个数,如果n > 25,实际上还要判断包含多少个25,因为25中包含两个5,原来加过一个,还需要再加一次,如果n >125,那么还要再加第三次,依次类推。

2021......

[LeetCode C++实现]204. Count Primes

204.Count Primes

这么一道求素数个数的简单题目,首先尝试最简单的暴力破解法:

class Solution {

public:

int countPrimes(int n) {

int cnt = 0;

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

{

if(isPrime(i))

cnt++;

}

return cnt;

}

private:

bool isPrime(int n) {

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

{

if(n % i == 0)

return false;

}

return true;

}

};

执......

[LeetCode C++实现]326. Power of Three

326.Power of Three

while循环解法

class Solution {

public:

bool isPowerOfThree(int n) {

while(n > 1)

{

if(n % 3 != 0)

return false;

n /= 3;

}

return (n == 1) ? true:false;

}

};

循环解法更优解法:

class Solution {

public:

bool isPowerOfThree(int n) {

if(n < 1)

return false;

while(n % 3 == 0)

{

n......

[LeetCode C++实现]15. 3Sum

15.3Sum

3sum一道非常经典的题目,第一次做,以前只做了2Sum,老规矩先上暴力破解解法:

class Solution {

public:

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

int n = nums.size();

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

set<vector<int>> result;

vector<vector<int>> res;

for(int i = 0;i < n ......

[LeetCode C++实现]92. Reverse Linked List II

92. Reverse Linked List II

自己吭哧吭哧花了接近40分钟写完代码,AC后从评论区看到的解法,新增一个结点,方便处理从开头反转的情况,不用单独判断m==0进行特殊处理。

/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

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

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

* Li......

[LeetCode C++实现]49. Group Anagrams

49. Group Anagrams

这道题是242Valid Anagram这道题目的升级版,242是判断两个字符串是不是Anagram,现在是将多个Anagram字符串归类到一起。

class Solution {

public:

vector<vector<string>> groupAnagrams(vector<string>& strs) {

unordered_map<string, vector<string>> mp;

vector<vector<string>> res;

f......