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】的更多相关文章

  1. 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 ...

  2. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  3. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  4. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  5. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  6. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  7. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  8. 413. Reverse Integer【LintCode java】

    Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...

  9. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

随机推荐

  1. ansible实用例子

    寻找/etc/ 名为"hosts" 递归查找 ansible webserver -m find -a ' path=/etc/ file_type=any recurse=yes ...

  2. redis集群环境搭建的错误

    安装redis集群需要版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本. 需要安装ruby的环境. yum -y install ru ...

  3. Filters in ASP.NET Core (转自MSDN)

    Filters in ASP.NET Core MVC allow you to run code before or after specific stages in the request pro ...

  4. Linux -- 目录基本操作(1)

    cd 切换目录 1.切换到指定目录下 #cd 相对/绝对目录 [root@localhost ~]# cd /home/tom/demo [root@localhost demo]# 2.切换到某个用 ...

  5. js(jQuery)tips

    一:页面加上$(function(){***内容***})与不加的区别 1.这个是DOM加载完之后再加载JS代码,你的JS如果放在文档后面可能一样,但是如果你要是把JS放在head里面就有差别了(放在 ...

  6. PHP实现多继承 trait 语法

    原文地址:http://small.aiweimeng.top/index.php/archives/50.html PHP没有多继承的特性.即使是一门支持多继承的编程语言,我们也很少会使用这个特性. ...

  7. [译]C语言实现一个简易的Hash table(5)

    上一章中,我们使用了双重Hash的技术来处理碰撞,并用了C语言实现,贲张我们将实现Hash表中的插入.搜索和删除接口. 实现接口 我们的hash函数将会实现如下的接口: // hash_table.h ...

  8. Python3 图像识别(二)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 以图搜图的使用已经非常广泛了,我现在来介绍一下简单的以图搜图的相关算法及其实践. 一.感知hash算法 感知哈希算 ...

  9. Gulp的安装配置过程和一些小坑

    谈谈gulp. 项目尾声,老师叫我去熟悉一下grunt前端自动化工具,第一次知道这种东西,我就查各种资料啊,发现grunt已经‘过时’了,大家都用gulp和webpack.我当然选择了配置最简单的gu ...

  10. 20155234 2016-2017-2《Java程序设计》课程总结

    20155234 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:师生关系 预备作业2:优秀技能经验 预备作业3:虚拟机linux初接触 第一周作业:认识 ...