[58] LEETCODE, arrays classification, moderate level, Title: 238,78,287

Package y2019.Algorithm.array.medium; 

Import java.util.Arrays; 

/ ** 
 * @ProjectName: Cutter-Point 
 * @package: y2019.Algorithm.array.medium 
 * @ClassName: ProductExceptSelf 
 * @author: xiaof 
 * @Description : TODO 238. Product of the Except the Array Self 
 * of the Given the nums AN Array WHERE n-n-integers>. 1, that return SUCH AN Array Output Output [I] IS equal to The Product of All 
 * The Elements of the nums the except the nums [I]. 
 the Input *: [1,2,3,4] 
 * the output: [24,12,8,6] 
 * 
 * given length n integer array nums, wherein n> 1, the output returns an array output, wherein the output [i ] is equal to the product of the addition nums [I] of the remaining elements nums. 
 * Source: stay button (LeetCode) 
 * Link:https://leetcode-cn.com/problems/product-of-array-except-self 
 * all the copyright collar button network. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. 
 * 
 * @Date: 2019/7/17 9:31 
 * @Version: 1.0 
 * / 
public  class ProductExceptSelf { 

    public  int [] Solution ( int [] the nums) { 

        // find the direct product of all data, and each data traversal in the time of the divisor 
        int [] RES = new new  int [nums.length];
         int Allx =. 1, zeroNum = 0 ;
         for ( int I = 0; I <nums.length; ++ I) {
             IF (the nums [I ]! = 0 ) { 
                Allx * = nums[i];
            } else {
                ++zeroNum;
            }
        }

        //求各个位置的值
        if(zeroNum <= 1) {
            for(int i = 0; i < res.length; ++i) {
                if(nums[i] == 0 && zeroNum == 1) {

                    res[i] = allX;
                } else if (zeroNum == 0) {
                    res[i] = allX / nums[i];
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        int data[] = {1,0};
        ProductExceptSelf fuc = new ProductExceptSelf();
        System.out.println(fuc.solution(data));
        System.out.println();
    }

}

 

package y2019.Algorithm.array.medium;

import java.io.*;
import java.util.*;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: Subsets
 * @Author: xiaof
 * @Description: TODO 78. Subsets
 * Given a set of distinct integers, nums, return all possible subsets (the power set).
 * Note: The solution set must not contain duplicate subsets.
 *
 * Input: nums = [1,2,3]
 * Output:
 * [
 *   [3],
 *   [1],
 *   [2],
 *   [1,2,3],
 *   [1,3],
 *   [2,3],
 *   [1,2],
 * [] 
 *] 
 * 
 * 
 * 
 * @Date: 2019/7/17 10:49 
 * @Version: 1.0 
 * / 
public  class Subsets { 

    public List <List <Integer >> Solution ( int [] the nums) {
         // Output all possible combinations, as related to the change in length, considered here recursive 
        List <List <Integer >> RES = new new the ArrayList <> (); 
        res.add ( new new the ArrayList <> ());
         // every recursion depth plus first, equivalent to explore, with a maximum total length, each recursive front is a collection of plus next collection
         // it should be on location mark, but the length is not fixed and is not repeated, then consider set marking 
        the set mark = new new HashSet (); 
        Arrays.sort (the nums);
        //Here also relates to a problem that may have repeated combinations, just not the same order, then in order to exclude out of order, we shoot along the array, and then look at the back of each data 
        allZhuHe ( new new ArrayList <> () , Mark, RES,. 1, the nums, 0 ); 

        return RES; 
    } 

    public  void allZhuHe (List <Integer> curList, the Set Marks, List <List <Integer >> RES, int len, int [] the nums, int startIndex) {
         IF (len> nums.length) {
             return ; 
        } 
        // if within a reasonable range, then we take the data is not set
 //         the set TEMPSET = new new HashSet (Marks); 
        for ( int I = startIndex; I <the nums. length; ++i) {
            if(!marks.contains(nums[i])) {
                //如果不包含
                List<Integer> tempList = new ArrayList<>(curList);
                tempList.add(nums[i]);
                res.add(tempList);
                marks.add(nums[i]);
                allZhuHe(tempList, marks, res, len + 1, nums, i);
                marks.remove(nums[i]);
            }
        }
    }

//    public List deepClone(List<Integer> curList) throws IOException, ClassNotFoundException {
//        //直接拷贝对象
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
//        objectOutputStream.writeObject(curList);
//
//        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//        ObjectInputStream in = new ObjectInputStream(byteIn);
//        @SuppressWarnings("unchecked")
//        List dest = (List) in.readObject();
//        return dest;
//
//    }

    public static void main(String[] args) {
        int data[] = {1,2,3};
        Subsets fuc = new Subsets();
        System.out.println(fuc.solution(data));
        System.out.println();
    }
}

 

Package y2019.Algorithm.array.medium; 

/ ** 
 * @ProjectName: Cutter-Point 
 * @package: y2019.Algorithm.array.medium 
 * @ClassName: FindDuplicate 
 * @author: xiaof 
 * @Description: the Find the TODO 287. The Duplicate Number The 
 * the Given AN Array the nums containing n-+. 1 integers WHERE each Integer IS BETWEEN. 1 and n-(Inclusive), 
 * Prove that AT Least One Duplicate Number MUST exist. Assume that there IS only One Duplicate Number, 
 * Find The Duplicate One. 
 * 
 the Input *: [1,3,4,2,2] 
 * the Output: 2 
 * 
 * not change the original array (assuming the array is read-only). 
 * Use only extra space O (1) is. 
 * Is less than the time complexity of O (n2). 
 * Only a duplicate array of numbers, but it may be repeated more than once.
 *
 @Date *: 2019/7/17 11:29 
 * @Version: 1.0 
 * / 
public  class FindDuplicate { 

    public  int Solution ( int [] the nums) {
         // obviously hash, but can not modify the original array, because the array and then 1 between ~ n, to have an array of such range, you do not want to hash a direct 
        int [] = hashNum new new  int [nums.length];
         for ( int I = 0; I <nums.length; ++ I) {
             IF (hashNum [the nums [I]] == 0 ) { 
                hashNum [the nums [I]] ++ ; 
            } the else {
                 return the nums [I]; 
            } 
        }

        return -1;

    }

}

 

Guess you like

Origin www.cnblogs.com/cutter-point/p/11200101.html