445. Cosine Similarity【LintCode java】
Description
Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other angle.
See wiki: Cosine Similarity
Here is the formula:

Given two vectors A and B with the same size, calculate the cosine similarity.
Return 2.0000 if cosine similarity is invalid (for example A = [0] and B = [0]).
Example
Given A = [1, 2, 3], B = [2, 3 ,4].
Return 0.9926.
Given A = [0], B = [0].
Return 2.0000
解题:按照公式计算,对特殊情况进行处理(数组不为空、分母不为0)。代码如下
public class Solution {
/*
* @param A: An integer array
* @param B: An integer array
* @return: Cosine similarity
*/
public double cosineSimilarity(int[] A, int[] B) {
// write your code here
if(A.length == 0 || B.length == 0)
return 2.0000;
int a = 0;
int b = 0;
int sum = 0;
for(int i = 0; i < A.length; i++){
a = a + A[i] * A[i];
b = b + B[i] * B[i];
sum = sum + A[i] * B[i];
}
if(a == 0 || b == 0)
return 2.000;
return (double)sum / (Math.sqrt(a) * Math.sqrt(b));
}
}
445. Cosine Similarity【LintCode java】的更多相关文章
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
随机推荐
- PAT——1035. 插入与归并
根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...
- [iOS]为git设置代理
查看本地git配置信息 git config --global -e 查看自己***的代理地址和端口信息 为git添加代理 git config --global http.proxy https:/ ...
- 通达信k线颜色设置
通达信的k线函数没有颜色选项.如果想要画颜色可以使用STICKLINE函数来覆盖当前k线这样也是可以满足需求. 第一步画针 STICKLINE(条件 , L , H , 0 , 0 ) , 颜色; 第 ...
- fail2ban 防暴力破解总结
公司服务器安全问题一直是个令人头疼的问题,许多运维的小伙伴一直在用脚本来监控服务器登录状态,然而脚本编写比较麻烦,今天就给大家推荐一款小而精致的防暴力破解工具 fail2ban ,他可以监控系统日志, ...
- cookie的简单认识
1.cookie Cokie又叫会话跟踪技术,实际上就是客户端硬盘上的一个文本文件,该文件用来存储一些数据. 首先,HTTP请求时无状态的,也就是说,你打开一个网页和下一个网页之间没有任何关系,数据不 ...
- Redis之配置文件redis.conf
解读下 redis.conf 配置文件中常用的配置项,为不显得过于臃长,已选择性删除原配置文件中部分注释. # Redis must be started with the file path as ...
- Java Hibernate Validator
Hibernate Validator是Hibernate提供的一个开源框架,使用注解方式非常方便的实现服务端的数据校验. 官网:http://hibernate.org/validator/ hib ...
- 学在Java之前
java基础 下载JDK JDK(Java Development Kit Java开发工具包) 官方网址: www.oracle.com 参阅oracle.html ...
- Python3 透明网桥算法
import time #定义网桥1 b1 = {} port_list1 = [1, 2] #主机列表 L1 = ['a','b','c'] L2 = ['d','e'] L = [L1,L2] d ...
- SSIS平台下的对象和概念
包即经检索.执行和保存的工作单元,是最重要的 Integration Services 对象. 控制流元素(任务和容器),用于在包中生成控制流.控制流元素准备或复制数据,与其他进程进行交互,或实现重复 ...