白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下:

这一题是要实现数组的全排列。这一题是要使用遍历以及递归的思想去实现,代码如下:

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[][]}
 4  */
 5 var permute = function(nums) {
 6     var res = new Array();
 7
 8     helper(nums, 0, nums.length - 1 , res);
 9
10     return res;
11 };
12
13 var helper = function(nums, k, n, res){
14
15     if(k==n){
16         let tmparr = new Array();
17         nums.forEach(function(v,i){
18             tmparr.push(v);
19         });
20         res.push(tmparr);
21     }else{
22         for(let i = k ; i <= n ; i++){
23             let tmp = nums[k];
24             nums[k] = nums[i];
25             nums[i] = tmp;
26
27             helper(nums, k+1, n, res);
28
29             tmp = nums[k];
30             nums[k] = nums[i];
31             nums[i] = tmp;
32         }
33     }
34 }

(今天因为将第25、31行的tmp写成了nums[k],导致结果一致不正确,以后需要注意一下这些小细节。)

END

原文地址:https://www.cnblogs.com/sssysukww/p/9580761.html

时间: 2024-07-31 11:55:44

白菜刷LeetCode记-46. Permutations的相关文章

白菜刷LeetCode记-811.Subdomain Visit Count

好久没有写LeetCode,所以说坚持真的是一件很难的事情啊.今日重新开始吧,先来一道简单的题目,如下: 这道题首先想到的还是使用Map,代码如下: /** * @param {string[]} cpdomains * @return {string[]} */ var subdomainVisits = function(cpdomains) { let tmp = new Map(); let res = new Array(); for(let i = 0 ; i < cpdomains

白菜刷LeetCode记-122. Best Time to Buy and Sell Stock II

今天题目如下: 要求出最大的利益.这题个人不太想得通,看了答案也不太知道为什么这样能获得最大值.代码如下: 1 /** 2 * @param {number[]} prices 3 * @return {number} 4 */ 5 var maxProfit = function(prices) { 6 let maxp = 0; 7 for(let i = 0 ; i < prices.length ; i++){ 8 if(prices[i-1] < prices[i]){ 9 maxp

白菜刷LeetCode记-384. Shuffle an Array

今天早上是一道中等难度的题目,考的是洗牌算法. 个人对洗牌算法还是比较不熟悉的,因此是看答案的.参考链接为:https://www.jianshu.com/p/44100741cef5 基本思路为: 1) 将第一个元素与 n 个元素中的任意一个交换: 2) 将第二个与 n - 1 个元素进行交换: 3) 重复上述步骤,直到剩下1个元素. 代码如下: 1 var original; 2 var copy; 3 var num; 4 /** 5 * @param {number[]} nums 6

白菜刷LeetCode记-328. Odd Even Linked List

发现简单题越来越少了,想偷懒都不可以了,今天的题目是中等难度的题目,题目如下: 这个题目是要根据链表的位置来修改链表,位置为奇数的节点全部排到前面,位置为偶数的节点全部排到奇数的后面,并且保持顺序不变. 想到的解决步骤为: 1.遍历数组,奇数的位置的节点组成一条新链表,偶数位置的节点组成另一个新链表: 2.将偶数链表接在奇数链表后面. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4

白菜刷LeetCode记-350. Intersection of Two Arrays II

今天题目如下: 比较简单,代码如下: /** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, nums2) { let myMap = new Map(); let myArr = new Array(); nums1.forEach(function(item){ if(myMap.has(item)){ myMap.set(i

&lt;LeetCode OJ&gt; 46. Permutations

46. Permutations My Submissions Question Total Accepted: 81495 Total Submissions: 239854 Difficulty: Medium Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,

【leetcode刷题笔记】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], and [2,1,1]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

【leetcode刷提笔记】Permutations

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]. 用深度搜索的方法搜索下面的一棵树(示例,非完整的树),每次搜索到叶子时经过的路径就是一个排列. 代码如下: 1 public class Soluti