难度系数排序,容易题1-10题:

Cosine Similarity new 

Fizz Buzz 

O(1)检测2的幂次 

x的平方根 

不同的路径 

不同的路径 II 

两个字符串是变位词 

两个链表的和

中位数

主元素

Cosine Similarity

题目:

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:

lintcode:1-10题

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]).

样例

Given A = [1, 2, 3], B = [2, 3 ,4].

Return 0.9926.

Given A = [0], B = [0].

Return 2.0000

解题:

Java程序:

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
        int ab = 0;
        double aa = 0.0;
        double bb = 0.0;
        if(A.length != B.length) return 2.0000;
        
        if(A.length==1 && A[0]==0 || B.length==1 && B[0]==0) 
            return 2.0000;
        for(int i=0;i<A.length;i++){
            ab += A[i] * B[i];
            aa += A[i] * A[i];
            bb += B[i] * B[i];
        }
        if(aa==0 || bb==0) return 2.0000;
        aa = Math.sqrt(aa);
        bb = Math.sqrt(bb);
        return ab/(aa*bb);
    }
}
View Code

Python程序:

class Solution:
    """
    @param A: An integer array.
    @param B: An integer array.
    @return: Cosine similarity.
    """
    def cosineSimilarity(self, A, B):
        # write your code here
        ab = 0
        aa = 0
        bb = 0
        if(len(A)!=len(B)) : return 2.0000
        # if len(A)==1 and A[0] = 0 or len(B)==1 and B[0]=0 : return 2.0000;
        for i in range(len(A)):
            ab += A[i]*B[i]
            aa += A[i]*A[i]
            bb += B[i]*B[i]
        if aa==0 or bb==0 : return 2.0000
        aa = aa**0.5
        bb = bb**0.5
        return ab/(aa*bb)
View Code

Fizz Buzz

题目:

给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:

  • 如果这个数被3整除,打印fizz.
  • 如果这个数被5整除,打印buzz.
  • 如果这个数能同时被35整除,打印fizz buzz.
样例

比如 n = 15, 返回一个字符串数组:

["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizz buzz"]

解题:

Java程序:

class Solution {
    /**
     * param n: As description.
     * return: A list of strings.
     */
    public ArrayList<String> fizzBuzz(int n) {
        ArrayList<String> results = new ArrayList<String>();
        for (int i = 1; i <= n; i++) {
            if (i % 15 == 0) {
                results.add("fizz buzz");
            } else if (i % 5 == 0) {
                results.add("buzz");
            } else if (i % 3 == 0) {
                results.add("fizz");
            } else {
                results.add(String.valueOf(i));
            }
        }
        return results;
    }
}
View Code

Python程序:

class Solution:
    """
    @param n: An integer as description
    @return: A list of strings.
    For example, if n = 7, your code should return
        ["1", "2", "fizz", "4", "buzz", "fizz", "7"]
    """
    def fizzBuzz(self, n):
        results = []
        for i in range(1, n+1):
            if i % 15 == 0:
                results.append("fizz buzz")
            elif i % 5 == 0:
                results.append("buzz")
            elif i % 3 == 0:
                results.append("fizz")
            else:
                results.append(str(i))
        return results
View Code

O(1)检测2的幂次

O(1) Check Power of 2

题目:

用 O(1) 时间检测整数 n 是否是 2 的幂次。

样例

n=4,返回 true;

n=5,返回 false.

注意

O(1) 时间复杂度

解题:

Java程序:

class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        // write your code here
        if(n==1) return true;
        if(n<=0 || n%2==1) return false;
        while(n!=0){
            n=n/2;
            if(n==1) return true;
            if(n%2==1) return false;
        }
        return true;
    }
};
View Code

原来就是利用到奇数不是2的次幂,进行排除

这里的时间复杂度应该是O(log2n) ,在最坏的情况下n就是2的幂次方,while循环要进行到k次,2^k = n

但是这个也AC了

看到下面程序,利用到若n是2的次幂则,n和n-1的二进制表示对应位置都不一样

class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        // write your code here
        if(n<=0) return false;
        return (n & (n-1)) == 0 ? true : false;
    }
};
View Code

Python程序:

class Solution:
    """
    @param n: An integer
    @return: True or false
    """
    def checkPowerOf2(self, n):
        # write your code here
        if n<=0 : return False
        if n==1 : return True
        if n%2==1 : return False
        while n!=0:
            n/=2
            if n==1 or n==0: return True
            if n%2==1:return False
        return True
View Code

相关文章: