996. Number of Squareful Arrays 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526272829303132333435class Solution {public: int ans ; vector<bool> visited; void dfs(vector<int> & nums, vector<int> & path){ if(path.size() == nums.size()){ ans++; } for(int i=0;i<nums.size();++i){ if (i > 0 && nums[i] == nums[i - 1] && visited[i - 1] == false) continue; if(visited[i] ) continue; if(!path.empty()){ double root = sqrt(nums[i]+path.back()); if(root- floor(root)!= 0 ) continue; } visited[i] = true; path.push_back(nums[i]); dfs(nums, path); path.pop_back(); visited[i] = false; } } int numSquarefulPerms(vector<int>& nums) { ans = 0; sort(nums.begin(), nums.end()); vector<int> path; visited = vector<bool>(nums.size(), false); dfs(nums, path); return ans; }};