GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/Permutations.java at master · mywbob/leetcode

Expand file tree

/

Permutations.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
57 lines (48 loc) · 1.82 KB
/

Permutations.java

Copy path

File metadata and controls

57 lines (48 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Given a collection of 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], and [3,2,1].
*/
public class Solution {
//slow
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> ares = new ArrayList<Integer>();
if (nums.length == 0) return res;
ares.add(nums[0]);
res.add(new ArrayList<Integer>(ares));
for (int i=1; i<nums.length; i++) {//pick num
List<List<Integer>> temp = new ArrayList<List<Integer>>();
for (int j=0; j<res.size(); j++) {//pick list
for (int k=0; k<=res.get(j).size(); k++) {//pick pos
List<Integer> atemp = new ArrayList<Integer>(res.get(j));
atemp.add(k, nums[i]);
temp.add(atemp);
}
}
res = temp;
}
return res;
}
//old
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[0]);//init
res.add(temp);
for (int i=1; i< nums.length; i++) {//pick a number
List<List<Integer>> tempres = new ArrayList<List<Integer>>();
for (int j=0; j<res.size(); j++) {//pick list from res
for (int index=0; index<=res.get(j).size(); index++) {//pick a position
temp = new ArrayList<Integer>(res.get(j));
temp.add(index, nums[i]);
tempres.add(temp);
}
}
//reset new res
res = tempres;
}
return res;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.