C++

 class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: Cosine similarity.
*/
double cosineSimilarity(vector<int> A, vector<int> B) {
// write your code here
int AB = , size;
double lenA = , lenB = ;
size = A.size();
for (int i = ; i < size; i++) {
AB += A[i]*B[i];
lenA += A[i]*A[i];
lenB += B[i]*B[i];
}
if ( == AB && == lenA && == lenB) {
return ;
} else {
return AB/(sqrt(lenA)*sqrt(lenB));
} }
};

LintCode: Cosine Similarity的更多相关文章

  1. [LintCode] Cosine Similarity 余弦公式

    Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...

  2. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  3. cosine similarity

    Cosine similarity is a measure of similarity between two non zero vectors of an inner product space  ...

  4. 相似度度量:欧氏距离与余弦相似度(Similarity Measurement Euclidean Distance Cosine Similarity)

    在<机器学习---文本特征提取之词袋模型(Machine Learning Text Feature Extraction Bag of Words)>一文中,我们通过计算文本特征向量之间 ...

  5. 皮尔逊相关系数与余弦相似度(Pearson Correlation Coefficient & Cosine Similarity)

    之前<皮尔逊相关系数(Pearson Correlation Coefficient, Pearson's r)>一文介绍了皮尔逊相关系数.那么,皮尔逊相关系数(Pearson Corre ...

  6. Cosine Similarity of Two Vectors

    #include <iostream>#include <vector>#include <cmath>#include <numeric> templ ...

  7. spark MLlib 概念 5: 余弦相似度(Cosine similarity)

    概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. 利用JAVA计算TFIDF和Cosine相似度-学习版本

    写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...

随机推荐

  1. ES6的一些基本用法

    ● let ● variable hoisting ● arrow Function, Lambda表达式 ● Destructuring Assignments 解构赋值 ● 默认参数值 Defau ...

  2. 在ASP.NET MVC中使用Knockout实践08,使用foreach绑定集合

    本篇体验使用 foreach 绑定一个Product集合. 首先使用构造创建一个View Model. var Product = function(data) { this.name = ko.ob ...

  3. UIWindow 介绍:概述、作用、主要属性及方法

    概述: UIWindow 类是 UIView 的子类,用于管理.协调应用中显示的窗口,其两个最重要的职能就是 容器,给 view 提供展示的区域: 将事件(例如:点击事件.拖拉事件等)分发给 view ...

  4. 通过连接池和字段索引,提升单点登录cas的性能

    cas是多个系统的中心认证,认证的过程就是用户的登录信息和数据库中的信息匹对的过程,假设某一时刻登录的人数非常多,须要频繁的读取数据库,数据库连接的管理就是问题. 前天測试评教时无意之中把单点登录的问 ...

  5. 找了一个api管理工具

    找了一个工具,https://github.com/nutsteam/apiManager选择了如下方式,进行了安装. ● 下载https://git.oschina.net/zhoujingjie/ ...

  6. 多个Jar的合并操作

    同事要写Android平台下的打包工具,遇到需要将多个jar合并成一个jar的问题.这里列一下操作步骤: 1.将所有jar文件复制至某临时目录中,通过jar命令解压得到所有的.class文件 > ...

  7. 抄袭证据之中的一个CMM与CMMI的名称

    以下文字来自我即将完毕的文章,谢博士说她没有抄袭,可是文中实在是有太多的漏洞了. 6.2.7 P120页中: "实际上终于所谓的统一方法论就是标准,尽管作标准并非目的.但标准是必须有的.能够 ...

  8. CentOS 7 Tomcat服务的安装与配置

    3422人阅读  http://blog.51cto.com/13525470/2073657 一.Linux下的Java运行环境 Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由S ...

  9. 【转】vs2012-vs2010使用stlport库的配置

    http://www.cnblogs.com/sbaicl/archive/2012/08/30/2663114.html STLport下载地址:http://sourceforge.net/pro ...

  10. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...