LintCode第二十七天

823. 输入流

给出两个输入流inputA和inputB,有Backspace,如果两个输入流最后的结果相等,输出YES,否则输出NO。

样例
给出 inputA = “abcde<<”, inputB = “abcd

public class Solution {
    /**
     * @param inputA: Input stream A
     * @param inputB: Input stream B
     * @return: The answer
     */
    public String inputStream(String inputA, String inputB) {
        // Write your code here
        Stack<Character>stackA=new Stack<>();
        Stack<Character>stackB=new Stack<>();
        for(Character c:inputA.toCharArray()){
            if(c!='<')
                stackA.push(c);
            else if(!stackA.empty())
                stackA.pop();
        }
        for(Character c:inputB.toCharArray()){
            if(c!='<')
                stackB.push(c);
            else if(!stackB.empty())
                stackB.pop();
        }
        if(stackA.equals(stackB))
            return "YES";
        else return "NO";
    }
}

828. 字模式

给定一个模式和一个字符串str,查找str是否遵循相同的模式。
这里遵循的意思是一个完整的匹配,在一个字母的模式和一个非空的单词str之间有一个双向连接的模式对应。

样例
给定模式= “abba”, str = “dog cat cat dog”,返回true。给定模式= “abba”, str = “dog cat cat fish”,返回false。
给定模式= “aaaa”, str = “dog cat cat dog”,返回false。给定模式= “abba”, str = “dog dog dog dog”,返回false。

public class Solution {
    /**
     * @param pattern: a string, denote pattern string
     * @param str: a string, denote matching string
     * @return: an boolean, denote whether the pattern string and the matching string match or not
     */
    public static int StrChange(String str){
        int sum=0;
        for(Character c:str.toCharArray())
            sum+=c;
        return sum;
    }
    public boolean wordPattern(String pattern,String str){
        int a[]=new int[1000];
        int b[]=new int[1000];
        String S[]=str.split(" ");
        for(int i=0;i<pattern.length();i++){
            int N=StrChange(S[i]);
            if(a[pattern.charAt(i)]!=b[N])
                return false;
            a[pattern.charAt(i)]=i+1;
            b[N]=i+1;
        }
        return true;
    }
}

835. Hamming Distance

The Hamming distance between two integers is the number of positions
at which the corresponding bits are different

.

Given two integers x and y, calculate the Hamming distance.

样例
Input: x = 1, y = 4

Output: 2

public class Solution {
    /**
     * @param x: an integer
     * @param y: an integer
     * @return: return an integer, denote the Hamming Distance between two integers
     */
    public int hammingDistance(int x, int y) {
        // write your code here
        int count=0;
        int sum=x^y;
        while (sum!=0){
            count++;
            sum=sum&(sum-1);
        }
        return count;
    }
}

837. Palindromic Substrings

Given a string, your task is to count how many palindromic substrings
in this string.

The substrings with different start indexes or end indexes are counted
as different substrings even they consist of same characters.

样例
Input: “abc”
Output: 3
Explanation: Three palindromic strings: “a”, “b”, “c”.

public class Solution {
    /**
     * @param str: s string
     * @return: return an integer, denote the number of the palindromic substrings
     */
    int count=0;
    public int countPalindromicSubstrings(String str){
        for (int i=0;i< str.length();i++){
            helper(str,i,i);
            helper(str,i,i+1);
        }
        return count;
    }
    public void helper(String s,int left,int right){
        while (left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){
            count++;
            left--;
            right++;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/mikeoperfect/article/details/80702757