implement in O(n2),not O(n)