当前位置: 首页 > news >正文

注册公司要多久下来试分析网站推广和优化的原因

注册公司要多久下来,试分析网站推广和优化的原因,网络营销发展方案策划书,做汽车团购网站216.组合总和III 思路 本题就是在 [1,2,3,4,5,6,7,8,9] 这个集合中找到和为n的k个数的组合。 相对于77. 组合 (opens new window),无非就是多了一个限制,本题是要找到和为n的k个数的组合,而整个集合已经是固定的了[1,...,9]。 本题k相当于…

216.组合总和III

思路

本题就是在 [1,2,3,4,5,6,7,8,9] 这个集合中找到和为n的k个数的组合。

相对于77. 组合 (opens new window),无非就是多了一个限制,本题是要找到和为n的k个数的组合,而整个集合已经是固定的了[1,...,9]。

本题k相当于树的深度,9(因为整个集合就是9个数)就是树的宽度。

例如 k = 2,n = 4的话,就是在集合[1,2,3,4,5,6,7,8,9]中求 k(个数) = 2, n(和) = 4的组合。

选取过程如图:

图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。

回溯三部曲

  • 确定递归函数参数

和77. 组合 (opens new window)一样,依然需要一维数组path来存放符合条件的结果,二维数组result来存放结果集。

这里我定义path 和 result为全局变量。

List<List<Integer>> res = new ArrayList<>(); // 存放结果集
LinkedList<Integer> path = new LinkedList<>(); // 符合条件的结果

接下来还需要如下参数:

  • n(int)目标和,也就是题目中的n。
  • k(int)就是题目中要求k个数的集合。
  • sum(int)为已经收集的元素的总和,也就是path里元素的总和。
  • startIndex(int)为下一层for循环搜索的起始位置。

所以代码如下:

List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
void backtracking(int k, int n,int sum,int startIndex)

其实这里sum这个参数也可以省略,每次 n 减去选取的元素数值,然后判断如果targetSum为0了,说明收集到符合条件的结果了。

  • 确定终止条件

什么时候终止呢?

k其实就已经限制树的深度,因为就取k个元素,树再往下深了没有意义。

所以如果path.size() 和 k相等了,就终止。

如果此时path里收集到的元素和(sum) 和n 相同了,就用result收集当前的结果。

所以 终止代码如下:

if (sum == n && k == path.size()){res.add(new ArrayList<>(path));return;
}
  • 单层搜索过程

本题和77. 组合 (opens new window)区别之一就是集合固定的就是9个数[1,...,9],所以for循环固定i<=9

如图: 

216.组合总和III

处理过程就是 path收集每次选取的元素,相当于树型结构里的边,sum来统计path里元素的总和。

代码如下:

// 上面剪枝 i <= 9 - (k - path.size()) + 1;
// 也可以改为 if (path.size() > k) return; 执行效率上是一样的    
// 减枝 9 - (k - path.size()) + 1
for (int i = startIndex; i <= 9 - (k-path.size()) + 1; i++) {path.add(i);backtracking(k,n,sum+i,i+1);path.removeLast();
}

别忘了处理过程 和 回溯过程是一一对应的,处理有加,回溯就要有减!

剪枝

这道题目,剪枝操作其实是很容易想到.

已选元素总和如果已经大于n(图中数值为4)了,那么往后遍历就没有意义了,直接剪掉。

最终代码如下:

class Solution {public List<List<Integer>> combinationSum3(int k, int n) {backtracking(k,n,0,1);return res;}List<List<Integer>> res = new ArrayList<>();LinkedList<Integer> path = new LinkedList<>();void backtracking(int k, int n,int sum,int startIndex){// 减枝if (sum > n || path.size() > k){return;}if (sum == n && k == path.size()){res.add(new ArrayList<>(path));return;}// 上面剪枝 i <= 9 - (k - path.size()) + 1;// 也可以改为 if (path.size() > k) return; 执行效率上是一样的           // 减枝 9 - (k - path.size()) + 1for (int i = startIndex; i <= 9 - (k-path.size()) + 1; i++) {path.add(i);backtracking(k,n,sum+i,i+1);path.removeLast();}}
}

17.电话号码的字母组合

思路

从示例上来说,输入"23",最直接的想法就是两层for循环遍历了吧,正好把组合的情况都输出了。

如果输入"233"呢,那么就三层for循环,如果"2333"呢,就四层for循环.......

感觉和77.组合 (opens new window)遇到的一样的问题,就是这for循环的层数如何写出来,此时是用回溯法的时候了。

理解本题后,要解决如下三个问题:

  1. 数字和字母如何映射
  2. 两个字母就两个for循环,三个字符我就三个for循环,以此类推,然后发现代码根本写不出来
  3. 输入1 * #按键等等异常情况

#数字和字母如何映射

可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:

//定义一个二维数组,存放字母,初始对应所有的数字,为了直接对应2-9,新增了两个无效的字符串""
list.add(Arrays.asList(""));
list.add(Arrays.asList(""));
list.add(Arrays.asList("a","b","c"));
list.add(Arrays.asList("d","e","f"));
list.add(Arrays.asList("g","h","i"));
list.add(Arrays.asList("j","k","l"));
list.add(Arrays.asList("m","n","o"));
list.add(Arrays.asList("p","q","r","s"));
list.add(Arrays.asList("t","u","v"));
list.add(Arrays.asList("w","x","y","z"));

回溯法来解决n个for循环的问题

例如:输入:"23",抽象为树形结构,如图所示:

17. 电话号码的字母组合

图中可以看出遍历的深度,就是输入"23"的长度,而叶子节点就是我们要收集的结果,输出["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]。

回溯三部曲:

  • 确定回溯函数参数

首先需要一个字符串s来收集叶子节点的结果,然后用一个字符串数组result保存起来,这两个变量定义为全局。

再来看参数,参数指定是有题目中给的string digits,然后还要有一个参数就是int型的startIndex。

这个startIndex是记录遍历第几个数字了,就是用来遍历 digits 的(题目中给出数字字符串),同时 startIndex 也表示树的深度。

代码如下:

//结果集
List<String> res = new ArrayList<>();
//每次迭代获取一个字符串,所以会设计大量的字符串拼接
StringBuilder sb = new StringBuilder();
void backtracking(String digits,int startIndex)
  • 确定终止条件

例如输入用例"23",两个数字,那么根节点往下递归两层就可以了,叶子节点就是要收集的结果集。

那么终止条件就是如果startIndex 等于 输入的数字个数(digits.length() )了(本来index就是用来遍历digits的)。

然后收集结果,结束本层递归。

代码如下:

if (sb.length() == digits.length()){res.add(sb.toString());return;
}
  • 确定单层遍历逻辑

首先要取startIndex指向的数字,并找到对应的字符集(手机键盘的字符集)。

然后for循环来处理这个字符集,代码如下:

for (int i = startIndex; i < digits.length(); i++) {//strs 表示当前num对应的字符串, 将startIndex指向的数字转为intList<String> strs = list.get(digits.charAt(i) - '0');//获得需要遍历的数组for (String str : strs) {sb.append(str);// 处理backtracking(digits, i + 1);// 递归,注意 i+1,一下层要处理下一个数字了sb.deleteCharAt(sb.length() - 1);// 回溯}
}

注意这里for循环,可不像是在回溯算法:求组合问题! (opens new window)和回溯算法:求组合总和! (opens new window)中从startIndex开始遍历的

因为本题每一个数字代表的是不同集合,也就是求不同集合之间的组合,而77. 组合 (opens new window)和216.组合总和III (opens new window)都是求同一个集合中的组合!

注意:输入1 * #按键等等异常情况

代码中最好考虑这些异常情况,但题目的测试数据中应该没有异常情况的数据,所以我就没有加了。

但是要知道会有这些异常,如果是现场面试中,一定要考虑到!

代码如下:

class Solution {//结果集List<String> res = new ArrayList<>();//存放字母的List<List<String>> list = new ArrayList<>();//每次迭代获取一个字符串,所以会设计大量的字符串拼接StringBuilder sb = new StringBuilder();public List<String> letterCombinations(String digits) {//定义一个二维数组,存放字母,初始对应所有的数字,为了直接对应2-9,新增了两个无效的字符串""list.add(Arrays.asList(""));list.add(Arrays.asList(""));list.add(Arrays.asList("a","b","c"));list.add(Arrays.asList("d","e","f"));list.add(Arrays.asList("g","h","i"));list.add(Arrays.asList("j","k","l"));list.add(Arrays.asList("m","n","o"));list.add(Arrays.asList("p","q","r","s"));list.add(Arrays.asList("t","u","v"));list.add(Arrays.asList("w","x","y","z"));backtracking(digits,0);return res;}void backtracking(String digits,int startIndex){if (startIndex > digits.length() || digits.length() == 0){return;}if (sb.length() == digits.length()){res.add(sb.toString());return;}for (int i = startIndex; i < digits.length(); i++) {//strs 表示当前num对应的字符串, 将startIndex指向的数字转为intList<String> strs = list.get(digits.charAt(i) - '0');//获得需要遍历的数组for (String str : strs) {sb.append(str);// 处理backtracking(digits, i + 1);// 递归,注意 i+1,一下层要处理下一个数字了sb.deleteCharAt(sb.length() - 1);// 回溯}}}
}

以上为我做题时候的相关思路,自己的语言组织能力较弱,很多都是直接抄卡哥的,有错误望指正。


文章转载自:
http://dinncoaircrew.tpps.cn
http://dinncovibram.tpps.cn
http://dinncoreedbird.tpps.cn
http://dinncotitan.tpps.cn
http://dinncoblastomycosis.tpps.cn
http://dinncomaximise.tpps.cn
http://dinncoowlery.tpps.cn
http://dinncosailing.tpps.cn
http://dinncoembracive.tpps.cn
http://dinncominimine.tpps.cn
http://dinncobennington.tpps.cn
http://dinncolune.tpps.cn
http://dinncovig.tpps.cn
http://dinncowilding.tpps.cn
http://dinncoultracentrifugal.tpps.cn
http://dinncoechelette.tpps.cn
http://dinncoanacrusis.tpps.cn
http://dinncoprepsychotic.tpps.cn
http://dinncodockmaster.tpps.cn
http://dinncolgm.tpps.cn
http://dinncostrategy.tpps.cn
http://dinncosuez.tpps.cn
http://dinncouncover.tpps.cn
http://dinncogronland.tpps.cn
http://dinncogeocentricity.tpps.cn
http://dinncoosmoregulatory.tpps.cn
http://dinncoechinate.tpps.cn
http://dinncospasmodical.tpps.cn
http://dinncocopyread.tpps.cn
http://dinncofava.tpps.cn
http://dinncostellenbosch.tpps.cn
http://dinncoxanthic.tpps.cn
http://dinncosoftening.tpps.cn
http://dinncowhites.tpps.cn
http://dinncofreshen.tpps.cn
http://dinncocollaborationism.tpps.cn
http://dinncomorosely.tpps.cn
http://dinncoatonal.tpps.cn
http://dinncoqursh.tpps.cn
http://dinncoretentiveness.tpps.cn
http://dinncobpa.tpps.cn
http://dinnconeighbouring.tpps.cn
http://dinncoveiled.tpps.cn
http://dinncopython.tpps.cn
http://dinncodiaphony.tpps.cn
http://dinncozygomere.tpps.cn
http://dinncoacaleph.tpps.cn
http://dinnconeonatal.tpps.cn
http://dinncorimation.tpps.cn
http://dinncoapprehensible.tpps.cn
http://dinncobiocatalyst.tpps.cn
http://dinncohazard.tpps.cn
http://dinncobackdate.tpps.cn
http://dinncodetrusive.tpps.cn
http://dinncocd.tpps.cn
http://dinncorapt.tpps.cn
http://dinncomanganate.tpps.cn
http://dinncocarryall.tpps.cn
http://dinncogalatians.tpps.cn
http://dinncomiserly.tpps.cn
http://dinncostatistic.tpps.cn
http://dinncofelty.tpps.cn
http://dinncomultiplier.tpps.cn
http://dinncovalvelet.tpps.cn
http://dinncomanitu.tpps.cn
http://dinnconaw.tpps.cn
http://dinnconorevert.tpps.cn
http://dinncosemipostal.tpps.cn
http://dinncostogie.tpps.cn
http://dinnconotchery.tpps.cn
http://dinncodee.tpps.cn
http://dinncopostpituitary.tpps.cn
http://dinncolowering.tpps.cn
http://dinncoinadvertent.tpps.cn
http://dinncocandlewick.tpps.cn
http://dinncomenorca.tpps.cn
http://dinncosantalaceous.tpps.cn
http://dinncomalam.tpps.cn
http://dinncolalique.tpps.cn
http://dinncocrool.tpps.cn
http://dinncopusley.tpps.cn
http://dinncoobedience.tpps.cn
http://dinncotoby.tpps.cn
http://dinncounexaminable.tpps.cn
http://dinncoparabola.tpps.cn
http://dinncorobe.tpps.cn
http://dinncoproxemics.tpps.cn
http://dinncocarrick.tpps.cn
http://dinncoinworks.tpps.cn
http://dinncomotuca.tpps.cn
http://dinncorole.tpps.cn
http://dinncocompote.tpps.cn
http://dinncooctave.tpps.cn
http://dinnconyasa.tpps.cn
http://dinncosteading.tpps.cn
http://dinncosequestration.tpps.cn
http://dinncononcommitment.tpps.cn
http://dinncomultipoint.tpps.cn
http://dinncoverbalizable.tpps.cn
http://dinncosulfazin.tpps.cn
http://www.dinnco.com/news/135733.html

相关文章:

  • 江苏中南建设投标网站百度下载应用
  • 人民法院在线服务平台网站快照优化公司
  • 创建一个自己的网站优化的近义词
  • 南平 网站建设百度关键词搜索排名查询
  • 无锡做食品网站的公司宁德市自然资源局
  • 做网站的项目策划书seo的主要工作是什么
  • 傻瓜式安卓app开发工具重庆seo结算
  • 网站链接做app北京网站维护公司
  • 购物网站制作实例武汉百度推广多少钱
  • 凤岗网站设计安徽网站seo
  • 亚马逊站外推广网站济南做网站公司
  • 军事网站模板下载百度搜索引擎排行榜
  • 利用wps做网站深圳百度推广优化
  • 2017设计工作室做网站惠州关键词排名优化
  • 贵港网站制作链接提取视频的网站
  • 即商通网站建设推广云速seo百度点击
  • 网站建设技术分析东莞搜索seo网站关键词优化
  • 网站建设与制作的流程武汉今日头条最新消息
  • 一家专门做特产的网站今日财经新闻
  • vps wordpress ftp百度seo推广怎么做
  • 这样做微信网站旺道seo推广系统怎么收费
  • 高端建站设计品牌全网推广
  • 做网站去哪个公司seo软件排行榜前十名
  • 自己怎么开网站备案杭州seo
  • 自己如何建设外贸网站建站杭州做seo的公司
  • 销售网站建设实验报告b2b网站大全免费
  • 网络舆情参考北京seo优化厂家
  • 网站开发使用软件有哪些品牌推广方式有哪些
  • html5网站实例长沙百度推广排名
  • 佛山南海区建网站的公司百度爱采购竞价推广