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

南京旅游网站建设公司竞价排名推广

南京旅游网站建设公司,竞价排名推广,wordpress 淘宝优惠券,免费招收手游代理电话号码的字母组合 思路:多个循环可以考虑回溯。 首先明确: 循环的宽度是多少,即从哪些区间取数(本题目中每个数字都是3个字母,都是从三个字母中取一个数,所以可以确定循环宽度就是每个数字对应的字符串…

电话号码的字母组合

思路:多个循环可以考虑回溯。

首先明确:

  1. 循环的宽度是多少,即从哪些区间取数(本题目中每个数字都是3个字母,都是从三个字母中取一个数,所以可以确定循环宽度就是每个数字对应的字符串的长度)
  2. 循环的高度是多少,即循环终止条件(分别从每个数字里取一个字母,所以循环高度就是给出的数字的数量)
  3. 循环的区间是什么,用什么可以统一表达并递归(字母的联系是数字,需要找出每层循环中的循环数字,那就使用Index取数字)
  4. 是否需要剪枝:如果求sum问题可以提前剪枝,本题目不用
class Solution {List<String> res = new ArrayList<>();List<String> sub = new ArrayList<>();StringBuffer sb = new StringBuffer();Map<Character, String> map = new HashMap<>(); //Character用的好int n = 3;public List<String> letterCombinations(String digits) {int len = digits.length(); //几层--循环宽度if(len == 0){return res;}map.put('2', "abc"); //找出对应关系map.put('3', "def");map.put('4', "ghi");map.put('5', "jkl");map.put('6', "mno");map.put('7', "pqrs");map.put('8', "tuv");map.put('9', "wxyz");fucLetter(digits, len, 0);return res;}public void fucLetter(String digits, int len, int Index){if(sb.length() == len){res.add(sb.toString());return;}char c = digits.charAt(Index); //找到每个数字String s = map.get(c); // 取出对应字符串for(int i = 0; i < s.length(); i++){ //循环字符串sb.append(s.charAt(i));fucLetter(digits, len, Index + 1);sb.deleteCharAt(sb.length() - 1);}}
}

组合总和

class Solution {List<List<Integer>> res = new ArrayList<>();List<Integer> sub = new ArrayList<>();int target;public List<List<Integer>> combinationSum(int[] candidates, int target) {this.target = target;Arrays.sort(candidates); // 剪枝的话先进行排序fucSum(candidates, 0, 0);return res;}public void fucSum(int[] candidates, int sum, int startIndex){if(sum > target){return;}if(sum == target){res.add(new ArrayList<>(sub));return;}for(int i = startIndex; i < candidates.length; i++){ //从startIndex开始if(sum + candidates[i] > target){break; //这个不要return,直接break} //这个剪枝注意一下sub.add(candidates[i]);sum += candidates[i];fucSum(candidates, sum, i); //允许可以重复,从当前元素即可!sub.remove(sub.size() - 1);sum -= candidates[i];}}
}

组合总和II

  1. 思路1:去除重复的集合,但是if(i != 0 && candidates[i] == candidates[i - 1])单纯这样写是不够的,因为这样也把纵向重复去除了(纵向重复并不会导致集合重复)!!!区分纵向和横向的关系:纵向是递归,横向是回溯,只有return后才是回溯,所以还是需要一个标注代表是同一层还是同一纵,用boolean use[] = new boolean[candidates.length]来记录,正常纵向递归时标注为true,回溯时变为false。
class Solution {List<List<Integer>> res = new ArrayList<>();List<Integer> sub = new ArrayList<>();boolean use[];public List<List<Integer>> combinationSum2(int[] candidates, int target) {use = new boolean[candidates.length]; //默认为falseArrays.sort(candidates); // 剪枝前先进行排序fucCombine(candidates, target, 0, 0);return res;}public void fucCombine(int[] candidates, int target, int startIndex, int sum){if(sum > target){return;}if(sum == target){res.add(new ArrayList<>(sub));return;}for(int i = startIndex; i < candidates.length; i++){if(i != 0 && candidates[i] == candidates[i - 1] && use[i - 1] == false){ //单纯这样写是不够的,因为这样也把纵向重复去除了!区分纵向和横向的关系:纵向是递归,横向是回溯,只有return后才是回溯continue;}if(candidates[i] + sum > target){break;}use[i] = true;sub.add(candidates[i]);sum += candidates[i];fucCombine(candidates, target, i + 1, sum);sub.remove(sub.size() - 1);sum -= candidates[i];use[i] = false;}}
}
  1. 思路2:使用if(i > startIndex && candidates[i] == candidates[i - 1])去重!!天才!!ifi > startIndex说明是同一层的for循环!!直接去重
class Solution {List<List<Integer>> res = new ArrayList<>();List<Integer> sub = new ArrayList<>();public List<List<Integer>> combinationSum2(int[] candidates, int target) {Arrays.sort(candidates); // 剪枝前先进行排序fucCombine(candidates, target, 0, 0);return res;}public void fucCombine(int[] candidates, int target, int startIndex, int sum){if(sum > target){return;}if(sum == target){res.add(new ArrayList<>(sub));return;}for(int i = startIndex; i < candidates.length; i++){if(i > startIndex && candidates[i] == candidates[i - 1]){continue;}if(candidates[i] + sum > target){break;}sub.add(candidates[i]);sum += candidates[i];fucCombine(candidates, target, i + 1, sum);sub.remove(sub.size() - 1);sum -= candidates[i];}}
}

回溯注意事项

  1. 何时定义startIndex,即循环从startIndex开始,例如: for(int i = startIndex; i < candidates.length; i++) ?当每一层要循环的区间有关联时,不允许跟之前重复,此时循环从startIndex开始。
  2. 递归时startIndex的值如何赋予?如果要求一个元素只能出现一次
    ,那么使用i+1,即:fucSum(candidates, sum, i+1); 否则使用i,即:fucSum(candidates, sum, i)
  3. 剪枝优化

写博客的目的是每日督促并记录刷题,也欢迎大家批评指正~(day25)

http://www.dtcms.com/wzjs/351979.html

相关文章:

  • 梁山做网站的公司百度搜索引擎优化的方法
  • 惠州响应式网站哪家好百度竞价点击软件奔奔
  • 个人网站主页设计企业网站优化服务
  • 营销网站建站企业南昌seo全网营销
  • 网站开发工作进度表seo博客写作
  • 成都网站建设潮州seo关键词排名软件流量词
  • 工信部网站备案查询验证码错误宁波seo推广推荐
  • wordpress开启链接百度seo优化排名
  • 男女直接做免费的网站网站制作开发
  • 网站开发用什么程序好互联网营销的特点
  • 网站开发与设计的参考文献全网最全搜索引擎app
  • 池州网站制作公网络宣传
  • 鸡西制作网站今日刚刚发生的军事新闻
  • 做网站的那家公司好武汉关键词排名工具
  • 网站里的专题页面百度怎么创建自己的网站
  • 周口建设网站平台推广
  • 大尺寸图网站网络营销的概念和含义
  • 做网站图标的软件站长之家官网登录入口
  • 成都网站建设名录广告媒体资源平台
  • 贝壳企业网站管理系统明星百度指数在线查询
  • 青岛 机械 中企动力提供网站建设百度收录查询
  • 碗网站宁波网络营销公司
  • 世界做诡异的地方网站技能培训班有哪些
  • 使用java做后台网站seo优化技术排名
  • 成都个人兼职做网站百度竞价渠道代理商
  • 网站评论 设计万能导航网
  • 在家做兼职的正规网站平台广东河源最新疫情
  • 广东哪家网站建设上海最新新闻热点事件
  • 自动网站建设企业的互联网推广
  • 网站开发的关键技术与难点推广赚钱的平台