当前位置:网站首页>472-82(22、165、39、剑指 Offer II 078、48. 旋转图像)
472-82(22、165、39、剑指 Offer II 078、48. 旋转图像)
2022-07-22 18:58:00 【liufeng2023】
22. 括号生成

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. 比较版本号

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. 组合总和

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;
}
};

剑指 Offer II 078. 合并排序链表

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. 旋转图像

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++)//先沿对角线反转
{
for (int j = 0; j < i; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
for (int i = 0; i < n; i++)//沿中线反转
{
for (int j = 0, k = n - 1; j < k; j++, k--)//类似于双指针
{
swap(matrix[i][j], matrix[i][k]);
}
}
}
};

边栏推荐
- Reinforcement learning 2
- Source code analysis of robot arm manipulator
- R语言 动态气泡图
- H2O R语言搭建
- How about opening an account of Huatai Securities ETF fund? Is it safe
- Thread类中run和start的区别
- USACO data set (2022.07.22)
- Learning Pyramid-Context Encoder Network for High-Quality Image Inpainting论文笔记
- R语言求导数
- Reflection (loading of classes)
猜你喜欢

图文并茂演示小程序movable-view的可移动范围

如何打包你的项目并且可以让别的用户通过pip安装

Tan Zhongyi, the initiator of xingce community: promote the intelligent transformation of enterprises by means of open source

JS 复杂数据类型

Difference between SFM and MVs

ZLMediaKit尝试解决GB28181(UDP方式)的视频花屏问题

Source code analysis of robot arm manipulator

Real time face detection using mediapipe and opencv

My code - speed version
![swing-[MyNote]-实现像IDEA一样的定位scroll from souce功能](/img/ee/53aae922d7a4b3df3871a3e997cc57.png)
swing-[MyNote]-实现像IDEA一样的定位scroll from souce功能
随机推荐
Summer vacation notes 1
Feign remote call lost request header problem solution
[机器学习] -[传统分类问题] - 朴素贝叶斯分类 + 逻辑回归分类
Network diagram of R language student letter chart learning
ICML2022 | ROCK: 关于常识因果关系的因果推理原则
兆易创新GD25WDxxK6 SPI NOR Flash产品系列问世
强化学习第二章习题
SFM与MVS区别
Illustration and text demonstrate the movable range of the applet movable view
JS Boolean Undefined Null typeof 类型转换 number() parseInt() parseFloat String toString() 计算器
MGRE与OSPF综合实验
One type and six methods of urllib
OWA邮件系统登录双因子认证(短信认证)方案
Zhang Li, President of China Electronic Information Industry Development Research Institute: building China's leading open source value chain
如何打包你的项目并且可以让别的用户通过pip安装
如何为您的企业设置内部Wiki?
LeetCode_单调栈_中等_316.去除重复字母
2022华为开发者大赛中国区开幕式重磅启动!
Oracle switches users and queries database commands under linux environment
USACO data set (2022.07.22)