[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......

[LeetCode C++实现]239. Sliding Window Maximum

滑动窗口经典题目,这里使用deque,双端队列中保存的是下标,保存值和下标均可以,依赖个人习惯。

class Solution {

public:

vector<int> maxSlidingWindow(vector<int>& nums, int k) {

deque<int> dq;

vector<int> ans;

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

{

if(!dq.empty() && dq.front() == i - k)

dq.pop_front();

whi......

[LeetCode C++实现]703. Kth Largest Element in a Stream

703. Kth Largest Element in a Stream

class KthLargest {

public:

KthLargest(int k, vector<int>& nums) {

size = k;

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

{

pq.push(nums[i]);

if(pq.size() > size) pq.pop();

}

}

int add(int val) {

pq.push(val);

if(pq.size() > size)

pq.pop();

return pq.......

[LeetCode C++实现]1021. Remove Outermost Parentheses

1021. Remove Outermost Parentheses

这道题的难点不在于编写程序,这么讲有点意思,leetcode的题目难点不在编程,这道题看半天没反应过来是什么意思,无奈打开力扣(leetcode中文版),看了题目描述才明白题目的意思。简单讲就是去掉最外层的括号,这里先过滤了最左边的(,根据opened的值也过滤对应的).

class Solution {

public:

string removeOuterParentheses(string S) {

string res;

int opened = 0;

for (char c : S) {

if (c == '......

[LeetCode C++实现]108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

* TreeNode() : val(0), left(nullptr), right(nullptr) {}

* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

* ......

[LeetCode C++实现]328. Odd Even Linked List

328. Odd Even Linked List

/**

* Definition for singly-linked 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) {}

* };

*/

class Solu......

[LeetCode C++实现]1304. Find N Unique Integers Sum up to Zero

1304. Find N Unique Integers Sum up to Zero

常规解法:

class Solution {

public:

vector<int> sumZero(int n) {

vector<int> res(n,0);

for(int i = 0;i < (n &~1);i+=2)

{

res[i] = i + 1,res[i + 1] = -(i + 1);

}

return res;

}

};

这里的n&~1作用是,如果n是奇数则执行n-1,如果n是偶数则不变。

[LeetCode C++实现]1464. Maximum Product

[LeetCode C++实现]1464. Maximum Product of Two Elements in an Array

date: 2020-10-06 16:00

url: LeetCode1464

1464. Maximum Product of Two Elements in an Array

无脑常规暴力破解方法:

class Solution {

public:

int maxProduct(vector<int>& nums) {

int n = nums.size();

int maximum = INT_MIN;

for(int ......

[LeetCode C++实现]1323. Maximum 69 Number

1323. Maximum 69 Number

常规解法,略显冗余

class Solution {

public:

int maximum69Number (int num) {

stack<int> stack;

int nums = 0,changed = 0;

while(num)

{

stack.push(num % 10);

num /= 10;

}

while(!stack.empty())

{

if(stack.top() == 6 && !changed && (changed = 1))

nums = nums * 10......

[LeetCode C++实现]1603. Design Parking System

1603. Design Parking System

常规解法:

class ParkingSystem {

public:

ParkingSystem(int big, int medium, int small) {

this->big = big;

this->medium = medium;

this->small = small;

}

bool addCar(int carType) {

switch(carType)

{

case 1:

if(big > 0){big--;return true;}else return false;break;......