1. 程式人生 > >leetCode練習(47)

leetCode練習(47)

題目:Permutations II

難度:medium

問題描述:

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]
]
解題思路:

與47題基本相同,只是多了在回溯時,需要跳過重複的元素。具體不同操作已經註釋出,程式碼如下:

public class Solution {
    ArrayList<List<Integer>> list=new ArrayList<List<Integer>>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        if (nums==null)	return list;
        Arrays.sort(nums);
        diedai(nums,new ArrayList<Integer>(),nums.length);
        return list;
    }
    public void diedai(int[]nums,ArrayList<Integer> templist,int overstep){
		int[] temp=nums.clone();
		int t;
		if(overstep==0){
			list.add(templist);
		}
		
		for(int i=0;i<temp.length;i++){
			t=temp[i];
			if(i>0&&temp[i]==temp[i-1]){    //與46題不同之處,需要先將nums進行排隊,然後,將重複的跳過。
				continue;
			}
			if(temp[i]==Integer.MAX_VALUE){
				continue;
			}else{	
				ArrayList<Integer> copylist=(ArrayList<Integer>)templist.clone();
				copylist.add(temp[i]);
				temp[i]=Integer.MAX_VALUE;
				diedai(temp,copylist,overstep-1);
					temp[i]=t;
			}
		}
	}
}

相關推薦

leetCode練習47

題目:Permutations II 難度:medium 問題描述: Given a collection of numbers that might contain duplicates, return all possible unique permutations.

leetCode練習98

題目:Validate Binary Search Tree 難度:medium 問題描述: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is

leetCode練習140

題目:Single Number難度:hard問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to

leetCode練習153154

題目:Find Minimum in Rotated Sorted Array難度:medium問題描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you be

leetCode練習61

題目:Rotate List 難度:medium 問題描述: Given a list, rotate the list to the right by k places, where k is n

leetCode練習121

題目:Best Time to Buy and Sell Stock 難度:easy 問題描述: Say you have an array for which the ith element is the price of a given stock on day i.

leetCode練習147

題目:Insertion Sort List難度:medium問題描述:使用插入排序對List進行排序求解思路:從左到右,依次將node插入到左邊已經排好的list中程式碼如下:public stati

leetCode練習1

題目:gas station難度:MEDIUM問題描述:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car

leetCode練習139

題目:Single Number難度:medium問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s ca

leetcode練習2

Container With Most Water 題意:在二維座標系中,(i, ai) 表示 從 (i, 0) 到 (i, ai) 的一條線段,任意兩條這樣的線段和 x 軸組成一個木桶,找出能夠盛水最多的木桶,返回其容積。 程式碼: int l = 0 ; int

leetCode練習88

題目:Merge Sorted Array 難度:easy 問題描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y

leetCode練習172

題目:Factorial Trailing Zeroes難度:EASY問題描述:Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3 Output: 0 Explan

leetCode練習128

題目:Longest Consecutive Sequence 難度:hard 問題描述: Given an unsorted array of integers, find the length of the longest consecutive elements se

leetCode練習43

題目:Multiply Strings 難度:dedium 問題描述: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th

Leetcode代碼練習

sts compare bre 那種 character span 一次 給定 spa 首先,沒有第二題,沒有第二題的原因是,JavaScript中根本就沒有那種數據結構,盡管我在playground裏面調試出了正確的結果,但是也許是因為數據結構問題,最終沒能讓我通過。 所

LeetCode演算法練習C++】Count and Say

題目: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 2

二、Linear Regression 練習轉載

hold off int 必須 html 移動 根據 which win from 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言   本文是多元線性回歸的練習,這裏練習的

六、regularized logisitic regssion練習轉載

method open lose hold on update import 等高線 algo 2個 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html 在上一講Deep learn

日常編程練習

style tar code 依次 span 二叉 div 前序遍歷 cin 一、樹的遍歷算法 樹的創建 struct BinaryTreeNode { int val; BinaryTreeNode* left; BinaryTreeNode*

課堂練習團隊

公開 壓力 自己的 般的 責任 開發人員 做出 勞動力 了解 1.團隊如何做決定? a)獨裁:領導說了算。 優點:領導比較冷靜、客觀,以整體利益為主;避免了團隊間的明面上的糾紛,團隊能有效地服從、執行。 缺點:領導可能不知道團隊的能力,不了解團隊的做事方法,不能做出正確的決