Commit c1aef7e0 authored by misterbooo's avatar misterbooo
Browse files

Remove Code

parent 5871d9af
/// Source : https://leetcode.com/problems/number-of-boomerangs/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import java.util.HashMap;
/// Using Hash Map
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)
public class Solution {
public int numberOfBoomerangs(int[][] points) {
int res = 0;
for( int i = 0 ; i < points.length ; i ++ ){
// record中存储 点i 到所有其他点的距离出现的频次
HashMap<Integer, Integer> record = new HashMap<Integer, Integer>();
for(int j = 0 ; j < points.length ; j ++)
if(j != i){
// 计算距离时不进行开根运算, 以保证精度
int dis = dis(points[i], points[j]);
if(record.containsKey(dis))
record.put(dis, record.get(dis) + 1);
else
record.put(dis, 1);
}
for(Integer dis: record.keySet())
res += record.get(dis) * (record.get(dis) - 1);
}
return res;
}
private int dis(int[] pa, int pb[]){
return (pa[0] - pb[0]) * (pa[0] - pb[0]) +
(pa[1] - pb[1]) * (pa[1] - pb[1]);
}
public static void main(String[] args) {
int[][] points = {{0, 0}, {1, 0}, {2, 0}};
System.out.println((new Solution()).numberOfBoomerangs(points));
}
}
cmake_minimum_required(VERSION 3.5)
project(cpp_0454)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(cpp_0454 ${SOURCE_FILES})
\ No newline at end of file
/// https://leetcode.com/problems/4sum-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cassert>
using namespace std;
/// Using Hash Map
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> hashtable;
for(int i = 0 ; i < C.size() ; i ++)
for(int j = 0 ; j < D.size() ; j ++)
hashtable[C[i]+D[j]] += 1;
int res = 0;
for(int i = 0 ; i < A.size() ; i ++)
for(int j = 0 ; j < B.size() ; j ++)
if(hashtable.find(-A[i]-B[j]) != hashtable.end())
res += hashtable[-A[i]-B[j]];
return res;
}
};
int main() {
int a[] = {1, 2};
int b[] = {-2, -1};
int c[] = {-1, 2};
int d[] = {0, 2};
vector<int> a_vec = vector<int>(a, a + sizeof(a)/sizeof(int));
vector<int> b_vec = vector<int>(b, b + sizeof(b)/sizeof(int));
vector<int> c_vec = vector<int>(c, c + sizeof(c)/sizeof(int));
vector<int> d_vec = vector<int>(d, d + sizeof(d)/sizeof(int));
cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl;
return 0;
}
/// https://leetcode.com/problems/4sum-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cassert>
#include <stdexcept>
using namespace std;
/// Another Way to use Hash Map
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int,int> hashtable1;
unordered_map<int,int> hashtable2;
for(int i = 0 ; i < A.size() ; i ++)
for(int j = 0 ; j < B.size() ; j ++)
hashtable1[A[i]+B[j]] += 1;
for(int i = 0 ; i < C.size() ; i ++)
for(int j = 0 ; j < D.size() ; j ++)
hashtable2[C[i]+D[j]] += 1;
int res = 0;
for(unordered_map<int,int>::iterator iter = hashtable1.begin() ; iter != hashtable1.end() ; iter ++)
if(hashtable2.find(-(iter->first)) != hashtable2.end())
res += iter->second * hashtable2[-(iter->first)];
return res;
}
};
int main() {
int a[] = {1, 2};
int b[] = {-2, -1};
int c[] = {-1, 2};
int d[] = {0, 2};
vector<int> a_vec = vector<int>(a, a + sizeof(a)/sizeof(int));
vector<int> b_vec = vector<int>(b, b + sizeof(b)/sizeof(int));
vector<int> c_vec = vector<int>(c, c + sizeof(c)/sizeof(int));
vector<int> d_vec = vector<int>(d, d + sizeof(d)/sizeof(int));
cout << Solution().fourSumCount(a_vec, b_vec, c_vec, d_vec) << endl;
return 0;
}
/// https://leetcode.com/problems/4sum-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import java.util.HashMap;
/// Using Hash Map
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
public class Solution1 {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
if(A == null || B == null || C == null || D == null)
throw new IllegalArgumentException("Illegal argument");
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0 ; i < C.length ; i ++)
for(int j = 0 ; j < D.length ; j ++){
int sum = C[i] + D[j];
if(map.containsKey(sum))
map.put(sum, map.get(sum) + 1);
else
map.put(sum, 1);
}
int res = 0;
for(int i = 0 ; i < A.length ; i ++)
for(int j = 0 ; j < B.length ; j ++)
if(map.containsKey(-A[i]-B[j]))
res += map.get(-A[i]-B[j]);
return res;
}
public static void main(String[] args) {
int[] a = {1, 2};
int[] b = {-2, -1};
int[] c = {-1, 2};
int[] d = {0, 2};
System.out.println((new Solution1()).fourSumCount(a, b, c, d));
}
}
/// https://leetcode.com/problems/4sum-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import java.util.HashMap;
/// Another Way to use Hash Map
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
public class Solution2 {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
if(A == null || B == null || C == null || D == null)
throw new IllegalArgumentException("Illegal argument");
HashMap<Integer, Integer> mapAB = new HashMap<Integer, Integer>();
for(int i = 0 ; i < A.length ; i ++)
for(int j = 0 ; j < B.length ; j ++){
int sum = A[i] + B[j];
if(mapAB.containsKey(sum))
mapAB.put(sum, mapAB.get(sum) + 1);
else
mapAB.put(sum, 1);
}
HashMap<Integer, Integer> mapCD = new HashMap<Integer, Integer>();
for(int i = 0 ; i < C.length ; i ++)
for(int j = 0 ; j < D.length ; j ++){
int sum = C[i] + D[j];
if(mapCD.containsKey(sum))
mapCD.put(sum, mapCD.get(sum) + 1);
else
mapCD.put(sum, 1);
}
int res = 0;
for(Integer sumab: mapAB.keySet()){
if(mapCD.containsKey(-sumab))
res += mapAB.get(sumab) * mapCD.get(-sumab);
}
return res;
}
public static void main(String[] args) {
int[] a = {1, 2};
int[] b = {-2, -1};
int[] c = {-1, 2};
int[] d = {0, 2};
System.out.println((new Solution2()).fourSumCount(a, b, c, d));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment