LeetCode 43.Multiply Strings & 46.Permutations & 47.Permutations II

Problem 43 Multiply Strings

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

解题思路:

1.由于题目要求不能直接将输入转为整型,所以要考虑实现两个数相乘的过程

2.计算过程如图所示,一个数的每一位依次去另一个数的每一位,最后将所得的结果按照对应的位数相加,即所得结果


3. 由于不知道最后的结果有多少位,直接用数组保存不方便,所以用一个HashMap<Integer,Integer>来保存计算的结果,key表示结果的位数,0为个位,1为十位,2为百位等。值表示该位上的值。

4. 计算的时候,要对进位进行处理,所以计算的时候要考虑准确地位置

代码如下:

public class Solution {
    public String multiply(String num1, String num2) {
        int length1 = num1.length();
        int length2 = num2.length();
        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }
        Map<Integer,Integer> res = new HashMap<>();
        
        for(int i = length2 -1;i >=0;i--){
            for(int j = length1 - 1;j >= 0;j--){
                int pos = (length2 - 1 -i) + (length1 - 1 -j);
                char a = num1.charAt(j);
                char b = num2.charAt(i);
                Calculate(res,a,b,pos,0);
                //System.out.print(res.get(pos)+" ");
            }
            //System.out.println(" ");
        }
        StringBuilder sb = new StringBuilder();
        
        for(int i = res.size()-1;i >=0;i--){
            sb.append(res.get(i));
        }
        
        return sb.toString();
        
    }
    
    public void Calculate(Map<Integer,Integer> res,char a,char b,int pos,int carry){
        int num1 = a-'0';
        int num2 = b-'0';
        
        int val = (num1*num2+carry) % 10;
        int newCarry = (num1*num2+carry) /10;
        //System.out.println(val + " " +newCarry);
        int temp;
        if(res.get(pos)!= null){
            temp = res.get(pos);
            temp+= val;
            
            if(temp >=10){
                temp = temp%10;
                newCarry++;
            }
            res.put(pos,temp);
        }
        else{
            res.put(pos,val);
        }
        if(newCarry >0){
            Calculate(res,'0','0',pos+1,newCarry);
        }
    }
    
   
}

Problem 46 Permutations

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

解题思路:

1. 由于输入的数组是没有重复的,所以直接进行递归深搜,添加数字,当list的size等于输入的数组长度,则得出一个结果,并记录

2. 每添加一个数字,用一个set来记录该数字,防止重复添加同一个数字


代码如下:

public class Solution {
    private List<List<Integer>> result;
    public List<List<Integer>> permute(int[] nums) {
        result = new ArrayList<>();
        
        
        find(nums,new ArrayList<>(),new HashSet<>());
        
        return result;
        
        
    }
    
    
    public void find(int[] nums, List<Integer> list,Set<Integer> record){
        if(list.size() == nums.length){
            result.add(new ArrayList<>(list));
            
        }
        
        for(int i = 0;i < nums.length;i++){
            if(record.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            record.add(nums[i]);
            find(nums,list,record);
            record.remove(nums[i]);
            list.remove(list.size()-1);
        }
        
        
    }
}


Problem 47 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

解题思路:

1. 这道题是上面那道题的进阶版,输入的数字可能会有重复,所以情况更加复杂

2. 解题核心就是处理重复的数字,所以在每一个深搜的递归层,都要保证同一个数字就算有多个,也只会便利一次,否则就会出现重复的结果

3. 所以用一个HashMap<Integer,Set<Integer>>来对每一层的遍历过的数字进行记录,前面的key表示递归层,后面的set记录该层所已经使用过的数字,防止重复

4.遍历时,访问该层的set,如果当前数字已经在set中,则跳过该数字


代码如下:

public class Solution {
    private List<List<Integer>> result;
    public List<List<Integer>> permuteUnique(int[] nums) {
        
        result = new ArrayList<>();
        //Arrays.sort(nums);
        List<Integer> Nums = new ArrayList<>();
        for(int num : nums){
            Nums.add(num);
            //System.out.println(num);
        }
        
        find(Nums,new ArrayList<>(),new HashMap<>(),0,nums.length);
        return result;
    }
    
    
    public void find(List<Integer> nums, List<Integer> list,Map<Integer,Set<Integer>> record,int level,int size){
        if(level == size){
            //System.out.println("get");
            result.add(new ArrayList<>(list));
            
            
        }
        
        for(int i = 0;i < nums.size();i++){
            int temp = nums.get(i);
            
           /* if(i > 0){
                record.get(level+1).clear();
            }*/
            //System.out.print("level:"+level + " temp:"+temp);
            if(record.get(level) != null){
                Set<Integer> set = record.get(level);
                if(set.contains(temp)){
                    
                     //System.out.println(" false");
                     continue;
                }
                else{
                    set.add(temp);
                }
            }
            else{
                 Set<Integer> set = new HashSet<>();
                 set.add(temp);
                 record.put(level,set);
                 
            }
            //System.out.println(" true");
            System.out.println("level:"+level + " temp:"+temp+" setSize():"+ record.get(level).size());
            list.add(temp);
            nums.remove(i);
            find(nums,list,new HashMap<>(),level+1,size);
            nums.add(i,temp);
           // record.get(level).clear();
            list.remove(list.size()-1);
            
        }
        
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值