[LeetCode C++实现]316. Remove Duplicate Letters

Remove Duplicate

这道题目和1081. Smallest Subsequence of Distinct Characters是同一道题目,解题方法如下:

class Solution {

public:

string removeDuplicateLetters(string s) {

vector<int> dict(256, 0);

vector<bool> visited(256, false);

for(auto ch : s) dict[ch]++;

string result = "0";

/** the key idea is......

[LeetCode C++实现]39. Combination Sum

39. Combination Sum

这道题目应当是一类题目,这类题目有个统一的解法—回溯法(backtracking)

class Solution {

public:

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

vector<vector<int>> res;

vector<int> combination;

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

co......

[LeetCode C++实现]315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self

常规暴力破解解法:

class Solution {

public:

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

int n = nums.size();

vector<int> res;

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

{

int cnt = 0;

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

{

if(nums[j] < nums[i])

cnt++;

}

res.......

[LeetCode C++实现]1572. Matrix Diagonal Sum

1572.Matrix Diagonal Sum

常规解法如下,目前没发现有更好的方法,时间复杂度优于O(N).

class Solution {

public:

int diagonalSum(vector<vector<int>>& mat) {

int n = mat.size(),sum = 0;

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

{

sum += mat[i][i] + mat[i][n -1 -i];

}

//sub middle element,when n is odd,it add twice.

if(n......

[LeetCode C++实现]48. Rotate Image

48. Rotate Image

看了大神的这类翻转问题通用解法,

解这类问题就变得非常简单了。

class Solution {

public:

void rotate(vector<vector<int>>& matrix) {

reverse(matrix.begin(),matrix.end());

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

{

for(int j = i + 1;j < matrix[i].size();j++)

{

swap(matrix[i][j],matrix[j][i]);

......

[LeetCode C++实现]1534. Count Good Triplets

1534 Count Good Triplets

无脑上暴力破解解法:

class Solution {

public:

int countGoodTriplets(vector<int>& arr, int a, int b, int c) {

int res = 0,n = arr.size();

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

{

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

{

for(int k = j + 1;k < n;k++)

{

if(abs(arr[i] - arr[j]) <= a &......

[LeetCode C++实现]1588. Sum of All Odd Length Subarrays

Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.

A subarray is a contiguous subsequence of the array.

Return the sum of all odd-length subarrays of arr.

Example 1:

Input: arr = [1,4,2,5,3]

Output: 58

Explanation: The odd-length subarrays of arr and t......

[LeetCode C++实现]1221. Split a String in Balanced Strings

Balanced strings are those who have equal quantity of 'L' and 'R' characters.

Given a balanced string s split it in the maximum amount of balanced strings.

Return the maximum amount of splitted balanced strings.

Example 1:

Input: s = "RLRRLLRLRL"

Output: 4

Explanation: s can be split into "RL", "RR......

[LeetCode C++实现]1365. How Many Numbers Are Smaller Than the Current Number

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i].

Return the answer in an array.

Example 1:

Input: nums = [8,1,2,2,3]

Output: [4,0,1,1,3]

E......

[LeetCode C++实现]1528. Shuffle String

常规解法,空间复杂度时间复杂度O(n)

class Solution {

public:

string restoreString(string s, vector<int>& indices) {

string res(s);

int n = s.size();

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

{

res[indices[i]] = s[i];

}

return res;

}

};

运行结果:

Runtime: 8 ms, faster than 98.74% of C++ online submissions for Shuf......