当前位置:网站首页>Writing to remember the ~ goal and
Writing to remember the ~ goal and
2022-04-22 09:37:00 【Xiaoqiang~】
Writing to remember ~ Objectives and
0、 Preface
This blog is to buckle 494. Objectives and topic , The difficulty of the topic is medium , Can be said to be DFS The exercises of , I little interesting ~
1、 Title Description

2、 Their thinking
2.1 Method 1 ~ utilize DFS
2.1.1 Ideas
Array nums You can add symbols to every element of the + or -, So each element has 2 A way to add symbols ,n Total number 2^n A way to add symbols . Then you can give n After adding symbols to all elements , Make a simple summation , If the result is equal to the target number target, Then the expression is the expression that meets the requirements .
2.1.2 Program code
class Solution {
public:
// DFS How to write it 1
/*int findTargetSumWays(vector<int>& nums, int target) { return dfs(nums, target, 0); } int dfs(vector<int> &nums, int target, int left) { if (target == 0 && left == nums.size()) return 1; if (left >= nums.size()) return 0; int ans = 0; ans += dfs(nums, target - nums[left], left + 1); ans += dfs(nums, target + nums[left], left + 1); return ans; }*/
// DFS How to write it 2
int num = 0;
int findTargetSumWays(vector<int>& nums, int target) {
getNum(nums, target, 0, 0);
return num;
}
void getNum(vector<int>& nums, int target, int index, int sum)
{
if (index == nums.size())
{
if (sum == target)
{
num++;
}
}
else
{
getNum(nums, target, index + 1, sum + nums[index]);
getNum(nums, target, index + 1, sum - nums[index]);
}
}
};
2.2 Other methods
There are other ways to solve this problem , Such as dynamic return , There is time for follow-up. Continue to add !
版权声明
本文为[Xiaoqiang~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220935414168.html
边栏推荐
- 软件测试工程师简历要怎么写,才能让 HR 看到?
- 2022年危险化学品经营单位安全管理人员上岗证题库及在线模拟考试
- 支持AUTOSAR Classic以及Adaptive平台的DEXT诊断数据库
- Easy to use screenshot software
- 【SQL server】SQL 概览
- SQL 数据类型
- L3-002 special stack (30 points) (two points stack)
- 杰理之CPU性能测试【篇】
- 超越iTerm! 号称下一代终端神器,功能贼强大!
- Command ‘yum‘ not found, but can be installed with: apt install yum
猜你喜欢

Online yaml to properties tool

遥感图像分割数据集整理(发布)

Detailed explanation of p-type mos tube switch circuit and working principle - Kia MOS tube

加密压缩备份BAT脚本

matplotlib教程04---绘制常用的图形

【Go】程序流程控制

一文学会text-justify,orientation,combine文本属性

MySQL多实例安装方法一

Beyond iterm! Known as the next generation terminal artifact, powerful!

经典场效应管如何快速关断技巧-KIA MOS管
随机推荐
服务端Watch监听处理过程解析
QTabelWidget实例
Sorting and publishing of remote sensing image segmentation data set
echo “新密码”|passwd --stdin 用户名
量化投资学习——介绍orderflow
Led by the cloud number, the revenue of Sanda shares in 2021 was 42.704 billion yuan, a year-on-year increase of 33.21%
拜登宣布再向乌克兰提供8亿美元安全援助
ShardingSphere简介与分表使用
MOS管开关频率最高多少如何测算-KIA MOS管
L2-030 冰岛人 (25 分) (最近公共祖先 思维
深度优先搜索(一):二叉树的中序遍历(力扣)
云数引领下,桑达股份2021年营收427.04亿元,同比增长33.21%
辐射度量学基本概念
P8 data broadcasting
tensorflow实验四----波士顿房价预测
Unity Editor Hierarchy下拉菜单扩展
Zhezheng nail scanning code login
2022年熔化焊接与热切割操作证考试题模拟考试平台操作
2022低压电工考试题及答案
L2-030 Icelander (25 points) (nearest public ancestor)