## [LeetCode C++实现]1551. Minimum Operations to Make Array Equal

class Solution {

public:

int minOperations(int n) {

int res = 0;

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

{

int num = 2*(i-1) + 1;

if(num > n)

res += num -n;

}

return res;

}

};

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Minimum Operations to Make Array Equal......

## [LeetCode C++实现]319. Bulb Switcher

class Solution {

public:

int bulbSwitch(int n) {

vector<int> state(n,0);

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

{

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

{

state[j] ^= 1;

}

}

return count(state.begin(),state.end(),1);

}

};

Time Limit Exceeded

Details

Last executed input

10000......

## [LeetCode C++实现]1773. Count Items Matching a Rule

class Solution {

public:

int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {

int index = (ruleKey == "type")? 0 : (ruleKey == "color") ? 1: 2;

int res = 0;

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

{

if(items[i][index] == ruleValue)

res += 1;

}......

## [LeetCode C++实现]1381. Design a Stack With Increment Operation

class CustomStack {

public:

CustomStack(int maxSize) {

m_maxsize = maxSize;

}

void push(int x) {

if(m_elem.size() >= m_maxsize)

return;

m_elem.push(x);

}

int pop() {

if(m_elem.empty())

return -1;

int x = m_elem.top();

m_elem.pop();

return x;

}

void increment(int k, int val) {

sta......

## [LeetCode C++实现]1305. All Elements in Two Binary Search Trees

/**

* 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++实现]1437. Check If All 1's Are at Least Length K Places Away

class Solution {

public:

bool kLengthApart(vector<int>& nums, int k) {

int n = nums.size();

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

{

if(nums[i] == 1)

{

int cnt = k;

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

{

if(nums[j] == 1)

{

if(cnt > 0)

return false;

break;

}

if(nums[j] ......

## [LeetCode C++实现]1769. Minimum Number of Operations to Move All Balls to Each Box

class Solution {

public:

vector<int> minOperations(string boxes) {

int n = boxes.size();

vector<int> res;

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

{

int distance = 0;

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

{

if(i == j) continue;

if('1' == boxes[j])

distance += abs(i-j);

}

res.push_back(d......

## std::sort segmentfault问题分析

#include <algorithm>

#include <vector>

bool comp(int d1, int d2)

{

return d1 >= d2;

}

int main()

{

std::vector<int> d(50,10);

std::sort(d.begin(), d.end(), comp);

return 0;

}

[root workspace]#./sort

**......

## [LeetCode C++实现]494. Target Sum

class Solution {

private:

//目标和个数

int count;

//vector大小,减少递归函数里查询O(n)复杂度

int size;

public:

int findTargetSumWays(vector<int>& nums, int S) {

size = nums.size();

helper(nums,0,0,S);

return count;

}

private:......

## [LeetCode C++实现]17. Letter Combinations of a Phone Number

class Solution {

public:

vector<string> letterCombinations(string digits) {

if(digits.empty()) return {};

vector<string> res = {""};

for(auto digit:digits)

{

vector<string> temp;