問題陳述給定一個由不同整數組成的數組,返回所有可能的排列 您可以按任何順序返回答案,接下來我們就來聊聊關于leetcode 基礎算法?以下內容大家不妨參考一二希望能幫到您!

leetcode 基礎算法
問題陳述
給定一個由不同整數組成的數組,返回所有可能的排列。 您可以按任何順序返回答案。
示例 1:
Input: nums = [1, 2, 3]
Output: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
示例 2:
Input: nums = [0, 1]
Output: [[0, 1], [1, 0]]
示例 3:
Input: nums = [1]
Output: [[1]]
約束:
- 1 <= nums.length <= 6
- -10 <= nums[i] <= 10
- All the integers of nums are unique.
解釋
回溯
當我們需要生成排列或序列時,遞歸是最好的使用方法。 與標準遞歸方法相比,此問題的遞歸將有所不同。
解決此問題的一種方法是跟蹤我們訪問過的元素并為其余數組元素生成排列。 但是,我們可以通過交換數組元素來解決這個問題。
讓我們跳到算法以更好地理解它。
- set result = [[]]- call _getPermutations(result, nums, 0, nums.length - 1)- return result// _getPermutations(result, nums, l, r)
- if l == r
- push the current nums permutation in the result
- result.push(nums)
- else
- loop for i = l; i <= r; i
- swap(nums[l], nums[i]) - _getPermutations(result, nums, l 1, r) - swap(nums[l], nums[i])
- end if
讓我們在 C 、Golang 和 Javascript 中檢查我們的算法。
C 解決方案
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result; _getPermutations(result, nums, 0, nums.size() - 1);
return result;
} void _getPermutations(vector<vector<int>>& result, vector<int> nums, int l, int r){
if(l == r){
result.push_back(nums);
return;
} else {
for(int i = l; i <= r; i ){
swap(nums[l], nums[i]); _getPermutations(result, nums, l 1, r); swap(nums[l], nums[i]);
}
}
}
};
Golang 解決方案
func permute(nums []int) [][]int {
result := [][]int{} _getPermutations(&result, nums, 0, len(nums) - 1) return result
}func _getPermutations(result *[][]int, nums []int, l, r int) {
if l == r {
cp := make([]int, len(nums))
copy(cp, nums)
*result = append(*result, cp)
} else {
for i := l; i <= r; i {
nums[l], nums[i] = nums[i], nums[l] _getPermutations(result, nums, l 1, r) nums[l], nums[i] = nums[i], nums[l]
}
}
}
Javascript 解決方案
var permute = function(nums) {
const result = []; _getPermutations(result, nums, 0, nums.length - 1); return result;
};function _getPermutations(result, nums, l, r) {
if(l === r) {
result.push(nums.slice(0));
return;
} else {
for(let i = l; i <= r; i ) {
[nums[l], nums[i]] = [nums[i], nums[l]]; _getPermutations(result, nums, l 1, r); [nums[l], nums[i]] = [nums[i], nums[l]];
}
}
}
讓我們為示例 1 試運行我們的算法。
Input: nums = [1, 2, 3]// in permute function
Step 1: vector<vector<int>> resultStep 2: _getPermutations(result, nums, 0, nums.size() - 1)
_getPermutations(result, nums, 0, 2)// in _getPermutations function
Step 3: if l == r
0 == 2
false else
loop for i = l; i <= r
i = 0
0 <= 2
true swap(nums[l], nums[i])
swap(nums[0], nums[0])
nums = [1, 2, 3] _getPermutations(result, nums, l 1, r)
_getPermutations(result, nums, 0 1, 2)
_getPermutations(result, nums, 1, 2)Step 4: if l == r
1 == 2
false else
loop for i = l; i <= r
i = 1
1 <= 2
true swap(nums[l], nums[i])
swap(nums[1], nums[1])
nums = [1, 2, 3] _getPermutations(result, nums, l 1, r)
_getPermutations(result, nums, 1 1, 2)
_getPermutations(result, nums, 2, 2)Step 5: if l == r
2 == 2
true
result.push_back(nums)
result = [[1, 2, 3]]
return // We return to step 4Step 6: swap(nums[l], nums[i])
swap(nums[1], nums[1])
nums = [1, 2, 3] i
i = 2
loop for i <= r
i = 2
2 <= 2
true swap(nums[l], nums[i])
swap(nums[1], nums[2])
nums = [1, 3, 2] _getPermutations(result, nums, l 1, r)
_getPermutations(result, nums, 1 1, 2)
_getPermutations(result, nums, 2, 2)Step 7: if l == r
2 == 2
true
result.push_back(nums)
result = [[1, 2, 3], [1, 3, 2]]
return // We return to step 6Step 8: swap(nums[l], nums[i])
swap(nums[1], nums[2])
nums = [1, 2, 3] i
i = 3
loop for i <= r
i = 3
3 <= 2
false // we backtrack to step 3Step 9: swap(nums[l], nums[i])
swap(nums[0], nums[0])
nums = [1, 2, 3] i
i = 1
loop for i <= r
i = 1
1 <= 2
true swap(nums[l], nums[i])
swap(nums[0], nums[1])
nums = [2, 1, 3] _getPermutations(result, nums, l 1, r)
_getPermutations(result, nums, 0 1, 2)
_getPermutations(result, nums, 1, 2)Step 10: if l == r
1 == 2
false else
for i = l; i <= r
i = 1
1 <= 2
true swap(nums[l], nums[i])
swap(nums[1], nums[1])
nums = [2, 1, 3] _getPermutations(result, nums, l 1, r)
_getPermutations(result, nums, 1 1, 2)
_getPermutations(result, nums, 2, 2)Step 11: if l == r
2 == 2
true
result.push_back(nums)
result = [[1, 2, 3], [1, 3, 2], [2, 1, 3]]
return // We return to step 10We similarly backtrack to generate the rest of the solution
We return the solution as[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
關注七爪網,獲取更多APP/小程序/網站源碼資源!
以上就是leetcode 基礎算法(七爪源碼LeetCode-)的內容,下面小編又整理了網友對leetcode 基礎算法(七爪源碼LeetCode-)相關的問題解答,希望可以幫到你。有哪些資深程序員總結的寫代碼的秘訣分享一下?
5. 我的程序是否有out of memory, stack overflow的風險 當然這些問題并不是那么好回答,需要一定的積累。平時多練練算法(安利一下leetcode,很好用),千萬別。 當。
如何自學Python?
中國大學慕課:用Python玩轉數據 講了一些用 Python 做數據分析的基本方法,老師很有意思,不過前面的章節還涉及到一些基礎的部分,可以當做再復習一遍啦 嵩天。 上。
有哪些軟件值得你強烈推薦?
.. 專為中小企業打造,使用成本低,并且能真正實現讓協作更高效,創作變得更輕松。 無憂·企業文檔我認為有幾大優勢: 第一點是無憂·企業文檔能夠多端同步,云端儲。
編程用什么軟件好?哪種前景更廣闊?
摘要:在我認識的所有程序員里,每個人幾乎都有專屬于自己的常用工具和相關資源,今天給大家奉上數十個程序員硬核工具,我相信這里總有一款工具是屬于你的! 程。 支。
如何把JavaScript的基礎打好?你有哪些建議?
(跨域)、Web Worker、Blob、客戶端數據庫等內容。 入門JavaScript還是比較容易的,實驗環境也比較好搭建,另外可。 入門JavaScript還是比較容易的,實驗環境也比較。
神級程序員都在用什么工具?
摘要:在我認識的所有程序員里,每個人幾乎都有專屬于自己的常用工具和相關資源,今天給大家奉上數十個程序員硬核工具,我相信這里總有一款工具是屬于你的! 程。 以。
作為計算機專業的學生,算法很差,該怎么提升?
我在學生時代也覺得算法很難,很抽象,一開始還不理解為什么要學習算法。工作幾年之后我越來越意識到算法的重要性。分享一下我的經驗,希望對你有所啟發。 算法。
JAVA算法能力差,該怎么提高?
謝謝邀請! Java程序員有不少都在從事應用級開發崗位,與C語言程序員相比,Java程序員往往在算法設計方面的能力稍差一些,與R語言程序員相比就更是如此了。 Java。
怎樣才能把算法學好?
上學時候傻,為了校招,看了不下于五本算法書,加上LeetCode,刷了大半年。 總共一兩千道題啊……不刷怕考到……忘了刷,刷了忘……毛都快掉沒了…… 現在工作近。