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 ...
随机推荐
- 安卓混合开发——原生Java和H5交互,保证你一看就懂!
** 在Android开发中,越来越多的商业项目使用了Android原生控件与WebView进行混合开发,当然不仅仅就是显示一个WebView那么简单,有时候还需要本地Java代码与HTML中的Jav ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- CABasicAnimation使用总结
CABasicAnimation使用总结 实例化 使用方法animationWithKeyPath:对 CABasicAnimation进行实例化,并指定Layer的属性作为关键路径进行注册. //围 ...
- [原]nginx 一下快一下慢的问题
在本机用thinkphp建了一个小网站,没任何问题,发布到云空间,就出现访问很慢的情况,而且是一下快一下慢,奇数次快,偶数次慢 换了一台win10的笔记本,情况一样,更新了phpstudy更新了thi ...
- Shell学习积累//持续更新
1.until的使用 直到判断条件满足,否则会一直执行,与while使用相反 until [ $command -eq 200 ] do command=`curl -o /dev/null -s - ...
- jenkins + ansible + docker 代码集成发布
一.环境搭建 1. 安装Java 配java_home, /etc/profile 2.安装Jenkins 下载war包,用 Java -jar Jenkins.war或者 把war包放tomca ...
- structc 开源框架简介
了解 structc-https://github.com/wangzhione/structc structc 是 C 构建基础项目框架. 不是太惊艳, 但绝对是 C 简单项目中一股清流. 它的前身 ...
- 002---rest_framework认证组件
rest_framework认证组件 问题:有些API,需要用户登录才能访问,有些无需登录就能访问. 解决: a. 创建两个模型类:UserInfo---OneToOne---UsetToken b. ...
- java基础之while循环练习(2)
实现猜数游戏,如果没有猜对随机数,则程序继续,猜对后停止程序. 方法思路: 1:要产生一个随机数,所以需要创建一个随机数对象 Random random=new Random(): 2: 调用随机数对 ...
- Java基本修饰符
java中的修饰符分为类修饰符,字段修饰符,方法修饰符.根据功能的不同,主要分为以下几种: *权限访问修饰符(可以用来修饰类.方法和字段) 适用范围<访问权限范围越小,安全性越高> 访问权 ...