Number of Squareful Arrays Solutions in Go
Number 996
Difficulty Hard
Acceptance 47.8%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("math""sort")func numSquarefulPerms(A []int) int {if len(A) == 0 {return 0}used, p, res := make([]bool, len(A)), []int{}, [][]int{}sort.Ints(A) // 这里是去重的关键逻辑generatePermutation996(A, 0, p, &res, &used)return len(res)}func generatePermutation996(nums []int, index int, p []int, res *[][]int, used *[]bool) {if index == len(nums) {checkSquareful := truefor i := 0; i < len(p)-1; i++ {if !checkSquare(p[i] + p[i+1]) {checkSquareful = falsebreak}}if checkSquareful {temp := make([]int, len(p))copy(temp, p)*res = append(*res, temp)}return}for i := 0; i < len(nums); i++ {if !(*used)[i] {if i > 0 && nums[i] == nums[i-1] && !(*used)[i-1] { // 这里是去重的关键逻辑continue}if len(p) > 0 && !checkSquare(nums[i]+p[len(p)-1]) { // 关键的剪枝条件continue}(*used)[i] = truep = append(p, nums[i])generatePermutation996(nums, index+1, p, res, used)p = p[:len(p)-1](*used)[i] = false}}return}func checkSquare(num int) bool {tmp := math.Sqrt(float64(num))if int(tmp)*int(tmp) == num {return true}return false}