LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
解析:从一组不重复的数字中选择若干个,和为指定的数字,注意单个数字能重复选择,找出所有这种组合。一个可行的想法是利用递归的思想来解决,每当选一个值 a 后,剩下的是如何从数字组中找出和为 target-a 的组合,其实是相同的问题。
基本递归思路:保持一个“全局”的 List<List<integer>> 用于存储符合条件的数字对 ——> 保持一个临时存储遍历到的数字对的集合 tempList ——> 用一个remain来记录target减去临时数字集合后剩下的值,用一个start来记录数组递归遍历的开始位置 ——> 从数组的第一位开始递归遍历,将数字加入到临时集合中,这个时候判断 remain的正负,如果小于0表明这个值不应该加入到当前的 tempList中,直接跳出,返回到上一层递归;如果等于0,则表明当前的tempList中的数字对之和正好为target,直接加入到 List<List<integer>> 中;如果大于0则继续向下递归。
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine().replaceAll("\\]|\\[| ",""); //这里 ] 好像一定要在 [ 的前面,要不然不能正确替换
int target=sc.nextInt();
String[] strs=input.split(",");
int[] nums=new int[strs.length];
for(int i=0;i<strs.length;i++)
nums[i]=Integer.parseInt(strs[i]); //对于char可以直接减字符0转为整数但是对于String是不行的
List<List<Integer>> list=new ArrayList<>();
backtrack(list, new ArrayList<Integer>(), nums, target,0);
for(List<Integer> i:list){
for(int a:i)
System.out.print(a+" ");
System.out.println();
}
}
public static void backtrack(List<List<Integer>> list, List<Integer> tempList,int[] nums, int remain, int start){
if(remain<0) return;
else if(remain==0) list.add(new ArrayList<>(tempList)); //以tempList中的数字实例化新的List再放入到集合中,不能直接放tempList
else{
for(int i=start;i<nums.length;i++){
tempList.add(nums[i]);
backtrack(list,tempList,nums,remain-nums[i],i); //i不加1是因为能再选nums[i]
tempList.remove(tempList.size()-1);
}
}
}
}
关于上面学到的一点是,如果往集合里加集合,那么加入的貌似是它的引用。试了先将 List<List<integer>> 中加入一个空的List<Integer>, 之后这个List<Integer>怎么改变,原List<List<Integer>>也会改变。这也解释了为什么每次要以tempList中的数字实例化新的List再放入到集合中,而不能直接放tempList。
2. Combination Sum II
上面题目的一种变体,区别就是数字不能重复选择了。
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
解法和上面的类似:
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String sc_r=sc.nextLine().replaceAll("\\]| |\\[","");
int target=sc.nextInt();
String[] scs=sc_r.split(",");
int[] array=new int[scs.length];
for(int i=0;i<scs.length;i++){
array[i]=Integer.parseInt(scs[i]);
}
List<List<Integer>> res=combinationsSum2(array,target);
for(List<Integer> list:res){
for(int a:list)
System.out.print(a+" ");
System.out.println();
}
}
static List<List<Integer>> combinationsSum2(int[] cand, int target){
Arrays.sort(cand);
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> path=new ArrayList<Integer>();
dfs_com(cand,0,target,path,res);
return res;
}
static void dfs_com(int[] cand, int cur, int target, List<Integer> path, List<List<Integer>> res){
if(target==0){
res.add(new ArrayList(path));
return;
}
if(target<0)return;
for(int i=cur;i<cand.length;i++){
if(i>cur&&cand[i]==cand[i-1]) continue;
path.add(path.size(),cand[i]);
dfs_com(cand,i+1,target-cand[i],path,res);
path.remove(path.size()-1);
}
}
}
3. Multiply Strings
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.
Note:
- The length of both
num1andnum2is < 110. - Both
num1andnum2contains only digits0-9. - Both
num1andnum2does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
思路:这题的要求很明显,就是对字符串形式的两个数字,不实用内置的字符串转整数方法来实现两者之间的乘法。基本思路是现将字符串转为数字数组,再观察下乘法的规律,当然了,涉及到数组,那么就要找到单个数字乘法与数组索引之间的规律。一个重点是:
Start from right to left, perform multiplication on every pair of digits, and add them together. Let’s draw the process! From the following draft, we can immediately conclude:
num1[i] * num2[j] will be placed at indices [i + j, i + j + 1]

找到了这个规律,那么便可以使用两层循环来实现乘法,数字和数字相乘时,和应该是乘积加上一轮循环计算中得到的相应位置低位值,然后分别利用除法和取余来更新高位和低位的数字,注意这里高位还要加上原有位置计算得到的值。
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String num1=sc.nextLine();
String num2=sc.nextLine();
System.out.println(multiply(num1,num2));
}
static String multiply(String num1, String num2){
int m=num1.length(),n=num2.length();
int[] pos=new int[m+n];
for(int i=m-1;i>=0;i--){
for(int j=n-1;j>=0;j--){
int mul=(num1.charAt(i)-'0')*(num2.charAt(j)-'0');
int p1=i+j,p2=i+j+1;
int sum=mul+pos[p2];
pos[p1]+=sum/10; //高位是除法后加上上一次循环时得到的高位的值
pos[p2]=sum%10; //低位直接取余
}
}
// 将pos数组转为字符创,不要前面的0
StringBuilder sb=new StringBuilder();
for(int p:pos)if(!(sb.length()==0&&p==0)) sb.append(p); //去除pos数组中前面的0,一开始当sb中没有值并且遇到的是数组中前面的0时,不将数字放入到sb中
return sb.length()==0? "0":sb.toString();
}
}
LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetcode解题报告(12):Maximum Subarray
描述 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- leetcode解题报告(22):Two Sum II - Input array is sorted
描述 Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾
2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...
随机推荐
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ.1009 FatMouse' Trade (贪心)
FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...
- matlab求一个矩阵中各元素出现的个数(归一化)
function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...
- intellij idea 破解补丁激活
一.说明 idea激活可以用JetBrains account,Activation Code注册码或者填License server网址,使用注册码的方式可以参考lanyun提供的注册码,但是有效时 ...
- Informatica _组件使用介绍及优化
转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.
- bzoj 2730: [HNOI2012]矿场搭建——tarjan求点双
Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...
- 有关计数问题的dp
问题一:划分数 问题描述 有n个去区别的物体,将它们划分成不超过m组,求出划分方法数模M的余数. 我们定义dp[i][j],表示j的i划分的总数 将j划分成i个的话,可以先取出k个,然后将剩下的j-k ...
- vue_使用npm搭建vue2.0脚手架开发环境
前言: 在使用vue进行开发时需要搭建vue的运行环境,这里主要是使用淘宝镜像cnpm进行搭建vue的脚手架开发环境.主要是分为mac和window两个版本,两个环境的搭建都是大同小异. mac开发环 ...
- hdu 1166敌兵布阵(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) M ...