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 Array Except Self * Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all * the elements of nums except nums[i]. * Input: [1,2,3,4] * Output: [24,12,8,6] * * 給定長度爲 n 的整數數組 nums,其中 n > 1,返回輸出數組 output ,其中 output[i] 等於 nums 中除 nums[i] 以外其他各元素的乘積。 * 來源:力扣(LeetCode) * 連接:https://leetcode-cn.com/problems/product-of-array-except-self * 著做權歸領釦網絡全部。商業轉載請聯繫官方受權,非商業轉載請註明出處。 * * @Date: 2019/7/17 9:31 * @Version: 1.0 */ public class ProductExceptSelf { public int[] solution(int[] nums) { //直接對全部數據求乘積,而後每一個數據遍歷的時候,求除數 int[] res = new int[nums.length]; int allX = 1, zeroNum = 0; for(int i = 0; i < nums.length; ++i) { if(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[] nums) { //輸出全部可能組合,由於涉及到長度的變化,這裏考慮用遞歸 List<List<Integer>> res = new ArrayList<>(); res.add(new ArrayList<>()); //每次遞歸深度加一,至關於去探索,長度不能超過總長,每次遞歸都是前面一次的集合加上下一次的集合 //那就要對位置作標記,可是長度又是不固定的而且不重複,那麼考慮用set作標記 Set mark = new HashSet(); Arrays.sort(nums); //這裏還涉及一個問題,那就是可能有重複的組合,順序不同而已,那麼爲了排除掉亂序的,咱們對數組拍個順,而後每次只看後面的數據 allZhuHe(new ArrayList<>(), mark, res, 1, nums, 0); return res; } public void allZhuHe(List<Integer> curList, Set marks, List<List<Integer>> res, int len, int[] nums, int startIndex) { if(len > nums.length) { return; } //若是再合理範圍內,那麼咱們取不在集合中的數據 // Set tempSet = new HashSet(marks); for(int i = startIndex; i < 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: TODO 287. Find the Duplicate Number * Given an array 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. * * Input: [1,3,4,2,2] * Output: 2 * * 不能更改原數組(假設數組是隻讀的)。 * 只能使用額外的 O(1) 的空間。 * 時間複雜度小於 O(n2) 。 * 數組中只有一個重複的數字,但它可能不止重複出現一次。 * * @Date: 2019/7/17 11:29 * @Version: 1.0 */ public class FindDuplicate { public int solution(int[] nums) { //明顯就是hash了,可是不能修改原數組,由於數組再1~n之間,對於這種有範圍的數組,直接hash不用想了 int[] hashNum = new int[nums.length]; for(int i = 0; i < nums.length; ++i) { if(hashNum[nums[i]] == 0) { hashNum[nums[i]]++; } else { return nums[i]; } } return -1; } }