Leetcode-996 Number of Squareful Arrays(正方形数组的数目)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 class Solution
 3 {
 4     public:
 5         bool judge(int n)
 6         {
 7             if(n == (int)sqrt(n)*(int)sqrt(n))
 8                 return true;
 9             return false;
10         }
11         int numSquarefulPerms(vector<int>& A)
12         {
13             int sz = A.size();
14             int cnt = 0;
15             int p = 0;
16             _for(i,0,sz)
17                 _for(j,i+1,sz)
18                     if(judge(A[i]+A[j]))
19                         p ++;
20             if(p*2<sz)
21                 return 0;
22             sort(A.begin(),A.end());
23             do
24             {
25                 int i;
26                 for(i = 0;i < sz-1;i ++)
27                     if(!judge(A[i]+A[i+1]))
28                         break;
29                 if(i==sz-1)
30                     cnt ++;
31             }
32             while(next_permutation(A.begin(),A.end()));
33             return cnt;
34         }
35 };

原文地址:https://www.cnblogs.com/Asurudo/p/10390652.html

时间: 2024-08-30 14:35:05

Leetcode-996 Number of Squareful Arrays(正方形数组的数目)的相关文章

LeetCode&mdash;&mdash;Single Number II(找出数组中只出现一次的数2)

问题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?   Single Number I 升级版,一个数组中其它数出现了

LeetCode——Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode][Python]Intersection of Two Arrays

Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. https://leet

[leetcode] 4. Median of Sorted Arrays

// Question: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).// Solution: get the new sorted array, then get the media number. #include

[LeetCode] Median of Two Sorted Arrays [16]

题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 原题链接(点我) 解题思路 返回两个排序数组的中位数.这个题可以有以下几个思路: 首先可以想到的是将两个数组merge起来,然后返回其中位数. 第二个是,类似merg

LeetCode: Single Number [136]

[题目] Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数数组,其中除了一个数以外,其他数都是成对出现的,找出这

初学knockoutjs记录2——Observables监控属性(2 Observable Arrays 监控数组)

1 Observable Arrays 监控数组 如果你想要监测和响应某个对象的变化,你应该使用Observable监控属性:如果你想要监测和响应一个对象集合的变化,那么请使用ObservableArray监控数组,这在很多场景下都很有用,例如当你要呈现或编辑多个值时,当你需要像列表项添加或移除一样处理UI上多个重复片段时. 例如 var myObservableArry = ko.observableArray(); // Initally an empty array myObservabl

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do