当前位置:网站首页>472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
2022-07-24 04:44:00 【liufeng2023】
22. Bracket generation

class Solution {
private:
vector<string> res;
public:
void dfs(int n, int left, int right, string seq)
{
if (left == n && right == n)
{
res.push_back(seq);
return;
}
if(left < n) dfs(n, left+1, right, seq + '(');
if (right < n && left > right) dfs(n, left, right + 1, seq + ')');
}
public:
vector<string> generateParenthesis(int n) {
dfs(n, 0, 0, "");
return res;
}
};

165. Compare version number

class Solution {
public:
int compareVersion(string version1, string version2) {
int i = 0, j = 0;
while (i < version1.size() || j < version2.size())
{
unsigned int num1 = 0, num2 = 0;
while (i < version1.size() && version1[i] != '.')
{
num1 = num1 * 10 + version1[i] - '0';
i++;
}
while (j < version2.size() && version2[j] != '.')
{
num2 = num2 * 10 + version2[j] - '0';
j++;
}
if (num1 > num2)
{
return 1;
}
else if (num1 < num2)
{
return -1;
}
i++, j++;
}
return 0;
}
};

39. Combinatorial summation

class Solution {
private:
vector<vector<int>> res;
vector<int> path;
private:
void back_tracking(vector<int>& candidates, int target, int sum, int start_index)
{
if (sum == target)
{
res.push_back(path);
return;
}
for (int i = start_index; i < candidates.size() && sum + candidates[i] <= target; i++)
{
sum += candidates[i];
path.push_back(candidates[i]);
back_tracking(candidates, target, sum, i);
sum -= candidates[i];
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
res.clear();
path.clear();
if (candidates.size() == 0) return res;
sort(candidates.begin(), candidates.end());
back_tracking(candidates, target, 0, 0);
return res;
}
};

The finger of the sword Offer II 078. Merge sort list

class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<int> temp;
for (const auto& list : lists)
{
ListNode* cur = list;
while (cur != nullptr)
{
temp.push_back(cur->val);
cur = cur->next;
}
}
sort(temp.begin(), temp.end());
ListNode* dummy_node = new ListNode(0);
ListNode* cur = dummy_node;
for (int i = 0; i < temp.size(); i++)
{
ListNode* node = new ListNode(temp[i]);
cur->next = node;
cur = cur->next;
}
return dummy_node->next;
}
};

48. Rotated image

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++)// First reverse diagonally
{
for (int j = 0; j < i; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
for (int i = 0; i < n; i++)// Reverse along the center line
{
for (int j = 0, k = n - 1; j < k; j++, k--)// Similar to double pointer
{
swap(matrix[i][j], matrix[i][k]);
}
}
}
};

边栏推荐
- 由硬件确定(服务的服绍,可参看官方2 和
- How to make yourself look young in how old robot? How old do I look? Younger method skills
- Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
- Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)
- C. Recover an RBS(括号序列,思维)
- Print leap years between 1000 and 2000
- Share a login interface template of QT implementation
- Robustness evaluation of commercial in vivo detection platform
- How can e-commerce projects solve the over issuance of online coupons (troubleshooting + Solutions) (glory Collection)
- Excel cell formula - realize Ackerman function calculation
猜你喜欢

C语言经典习题之猴子吃桃问题

高频小信号谐振放大器设计-课程设计Multisim仿真

Airiot Q & A issue 5 | how to use low code business flow engine?

Robustness evaluation of commercial in vivo detection platform

pycharm 调试功能介绍与使用

OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication

LabVIEW主VI冻结挂起

How to solve the engine prompt alias herodb and game engine startup exceptions?

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
随机推荐
Determined by hardware (see official 2 and
How to download vscode using domestic image seconds
Xiaomi finance was officially launched today (May 11) with a free 10000 yuan experience fee attached to the official address
Up sampling method (deconvolution, interpolation, anti pooling)
Array force buckle (continuously updated)
Little black gnawing leetcode:589. Preorder traversal of n-ary tree
工程师能力模型与技能要求
最大公约数
What if the computer time is often inaccurate? Set up tutorials to automatically update and proofread computer time
口叫SC 或者 pb 文件为读写控制ensor为
Print leap years between 1000 and 2000
项目普遍格式问题 src下添加 .eslinctrc.js
An online accident, I suddenly realized the essence of asynchrony
How to do if the right-click attribute of the network neighbor cannot be opened? The solution of the right-click attribute of the network neighbor cannot be opened
排序——QuickSort
Er system, in Lin reply bit, count, successfully open r com change
想知道一个C程序是如何进行编译的吗?——带你认识程序的编译
Design of high frequency small signal resonant amplifier course design Multisim Simulation
Robustness evaluation of commercial in vivo detection platform
由硬件确定(服务的服绍,可参看官方2 和