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 ...
随机推荐
- LWIP network interface 网卡 初始化 以 STM32 为例子 后面会有 用 2G 或者4G 模块 用 PPP拨号的 形式 虚拟出网卡 所以先以 这个为 前提
LWIP network interface 网卡 初始化 以 STM32 为例子 后面会有 用 2G 或者4G 模块 用 PPP拨号的 形式 虚拟出网卡 所以先以 这个为 ...
- cocos2d-x3.0 用CCDictionary写文件
bool CDownLoad_LocalData::WriteToConfigFile( DownLoadLocalData* downdata ){ CCDictionary* pDict = CC ...
- Unity 游戏框架搭建 2018 (一) 架构、框架与 QFramework 简介
约定 还记得上版本的第二十四篇的约定嘛?现在出来履行啦~ 为什么要重制? 之前写的专栏都是按照心情写的,在最初的时候笔者什么都不懂,而且文章的发布是按照很随性的一个顺序.结果就是说,大家都看完了,都还 ...
- Oracle11g 行列转换函数PIVOT and UNPIVOT
作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...
- OO课程总结
OO课程总结 OO终于划上了句号,这学期熬夜熬得最多的一门课,掉了最多头发的一门课. 一.测试与正确性 测试是最最最常见的用来找到程序错误以及验证程序正确的手段,在之前的作业中,写完代码还是会存在很多 ...
- javascript中常见的三种开发模式
一.单例模式: 所谓单例模式,即保证一个类只有一个实例,并提供一个访问它的全局访问点. <script type="text/javascript"> //一个类有某个 ...
- C# Web Service简单使用
第一步 打开VS,新建一个项目 第二步 创建一个ASP.NET 空 Web应用程序 我这里用的是VS2017 第三步 添加一个Web 服务(ASMX) 右键解决方案-->添加-->新建项 ...
- jquery选择器基础
简单选择器 类 id 元素/标签 * 复合(sel1,sel2)逗号隔开 层次选择器 s1 s2:后代选择器,空格隔开 p>c:子代选择器:不包括孙代及以下 p+next :相邻选择器 p~su ...
- jQuery $ 的作用
$符号总体来说有两个作用: 1.作为一般函数调用:$(param) (1).参数为函数:当DOM加载完成后,执行此回调函数 $(function(){//dom加载完成后执行 //代码 }) (2). ...
- 基于 pyMongo 和 wxPython 实现的 MongoDB Schema Analyser
MongoDB 作为文档型 NoSql 数据库,它的集合表结构往往不像关系型数据库那么固定和统一,同一个集合下的文档(document)的字段变化和差异可能很大,特别是在数据模型缺乏良好规划和规范的数 ...