【题目描述】
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]).
余弦相似度是度量一个内积空间的两个向量之间的相似度的度量,它测量了它们之间夹角的余弦值。0°的余弦是1,小于1其他角度。
看到维基:余弦相似性
这是公式:
给定两个向量A和B的大小相同,计算余弦相似度。
如果cos相似性无效(例如A =[0]和B =[0]),则返回2.0000。
【题目链接】
www.lintcode.com/en/problem/cosine-similarity/
【题目解析】
这是一道容易题。关键是通过循环分别求出分子和分母。
【参考答案】