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. 【Step By Step】将Dotnet Core部署到Docker上

    本教程的前提是,你已经在Linux服务器上已经成功的安装了Docker,我会大概介绍在此过程中用到的Docker命令,并不会介绍所有的Docker命令(因为我也不会). 一.在Docker中运行Dot ...

  2. IIS中ASP.NET虚拟目录不继承主站点web.config设置的办法(转载)

    ASP.NET提供了强大的Web.config来配置网站,一般来说一个网站只有一个根目录下的Web.config文件,有时候我们希望子目录有着不同的权限或者参数设置,则可以在相应子目录增加一个Web. ...

  3. CCF认证201803-1 跳一跳

    import java.util.Scanner; public class Jump { public static void main(String[] args) { Scanner sc = ...

  4. oracle定时器在项目中的应用

    业务需求: 现在业务人员提出了一个需求: 在项目中的工作流,都要有一个流程编号,此编号有一定的规则: 前四五位是流程的字母缩写,中间是8位的日期,后面五位是流水码,要求流水码每天从00001开始.即: ...

  5. MySQL->复制表[20180509]

    MySQL复制表     通常复制表所采用CREATE TABLE .... SELECT 方式将资料复制,但无法将旧表中的索引,约束(除非空以外的)也复制.   完整复制MySQL数据表所需步骤: ...

  6. docker 安装 MySQL 8.0

    1.查找镜像 查找Docker Hub上的mysql镜像 X230:~$ docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED m ...

  7. node 借助Node Binary管理模块“n”更新

    Node.js的版本频繁变化,如果有模块不能在你当前的Node版本上使用,需要升级Node环境 1)首先:查看当前node版本:node –v 2)安装n模块:npm install -g n 3)检 ...

  8. 我的 Delphi 学习之路 —— Delphi 助手的安装

    标题:我的 Delphi 学习之路 -- Delphi 助手的安装 作者:断桥烟雨旧人伤 Delphi 助手的安装 CnWizards 类似于 VS 中的番茄助手,在编写 Delphi 代码时帮助极大 ...

  9. Hive配置项的含义详解

    关于MetaStore:metastore是个独立的关系数据库,用来持久化schema和系统元数据. hive.metastore.local:控制hive是否连接一个远程metastore服务器还是 ...

  10. ruby安装devkit

    双击下载文件,指定解压路径,路径中不能有空格.如C:\DevKit,这个路径就是<DEVKIT_INSTALL_DIR>. > cd <DEVKIT_INSTALL_DIR&g ...