[LeetCode C++实现]394. Decode String

递归实现:

class Solution {

public:

string decodeString(string s) {

int i = 0;

return decodeString(s,i);

}

private:

string decodeString(string s,int &i) {

string res;

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

{

if(!isdigit(s[i]))

res += s[i++];

else

{

int n = 0;

//获取数字

while(i < s.size() &......

[LeetCode C++实现]199. Binary Tree Right Side View

这道题看了一会发现其实使用BFS会非常简单:

/**

* 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) {}

* TreeNode(int x, TreeNo......

[LeetCode C++实现]1038. Binary Search Tree to Greater Sum 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) {}

* TreeNode(int ......

[LeetCode C++实现]1302. Deepest Leaves Sum

BFS实现:

/**

* 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) {}

* TreeNode(int x, TreeNode *left, TreeNo......

[LeetCode C++实现]1315. Sum of Nodes with Even-Valued Grandparent

最开始看到这题想到用BFS实现,写完后才发现不行,如果当前行存在偶数,我下面的代码,会把下下行的结点加进去。

/**

* 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++实现]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

首先使用先序遍历的方法:

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

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

* };

*/

class Solution {

public:

TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode......

[LeetCode C++实现]1662. Check If Two String Arrays are Equivalent

使用最直接方法,先将两个单词拼接出来,再进行整体判断:

class Solution {

public:

bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {

string s1,s2;

for(auto str:word1)

s1 += str;

for(auto str:word2)

s2 += str;

return s1 == s2;

}

};

运行效率:

Runtime: 4 ms, faster than 89.96% of ......

[LeetCode C++实现]1742. Maximum Number of Balls

自己写的AC代码:

class Solution {

public:

int countBalls(int lowLimit, int highLimit) {

vector<int> cnt(highLimit+1,0);

int res = 0;

for(int i = lowLimit;i <= highLimit;i++)

{

cnt[calsum(i)]++;

}

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

{

res = max(res,cnt[i]);

}

return res;

}

private:

int calsum......

[LeetCode C++实现]1752. Check if Array Is Sorted and Rotated

开始分析nums序列中至多只能有一个前面元素大于后面元素,否则就不是通过排序后的序列轮转得到。

class Solution {

public:

bool check(vector<int>& nums) {

int greater_cnt = 0;

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

{

if(nums[i] > nums[(i+1)%nums.size()] )

{

if(++greater_cnt > 1)

return false;

}

}

return true;

}

};

这里解法比较简单,分析旋......

[LeetCode C++实现]1748. Sum of Unique Elements

最朴素的解法:

class Solution {

public:

int sumOfUnique(vector<int>& nums) {

int cnt[101] = {0},sum = 0;

for(auto n:nums)

cnt[n]++;

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

{

if(cnt[i] == 1) sum += i;

}

return sum;

}

};

运行效率:

Runtime: 4 ms, faster than 66.54% of C++ online submissions for Sum of Uni......