LintCode第四十二天

1282. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the
vowels of a string.

样例
Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “lintcode”, return “lentcodi”.

public class Solution {
    /**
     * @param s: a string
     * @return: reverse only the vowels of a string
     */
    public String reverseVowels(String s) {
        // write your code here
        List<Character>AEI=new ArrayList<>();
        AEI.add('a');AEI.add('e');AEI.add('i');AEI.add('o');AEI.add('u');
        int start=0,end=s.length()-1;
        StringBuilder str=new StringBuilder(s);
        while (start<end){
            while (!AEI.contains(Character.toLowerCase(s.charAt(start)))&&start<end)
                start++;
            while (!AEI.contains(Character.toLowerCase(s.charAt(end)))&&start<end)
                end--;
            if(start!=end){
                char temp=s.charAt(start);
                str.setCharAt(start,s.charAt(end));
                str.setCharAt(end,temp);
            }
            start++;
            end--;
        }
        return str.toString();
    }
}

1270. Ransom Note

Given an arbitrary ransom note string and another string containing
letters from all the magazines, write a function that will return true
if the ransom note can be constructed from the magazines ; otherwise,
it will return false.

Each letter in the magazine string can only be used once in your
ransom note.

样例
canConstruct(“a”, “b”) -> false
canConstruct(“aa”, “ab”) -> false
canConstruct(“aa”, “aab”) -> true

public class Solution {
    /**
     * @param ransomNote: a string
     * @param magazine: a string
     * @return: whether the ransom note can be constructed from the magazines
     */
    public boolean canConstruct(String ransomNote, String magazine) {
        // Write your code here
        int a[]=new int[256];
        int b[]=new int[256];
        for(char c:ransomNote.toCharArray())
            a[c]++;
        for(char c:magazine.toCharArray())
            b[c]++;
        for(int i=0;i<256;i++)
            if(a[i]>b[i])
                return false;
        return true;
    }
}

1266. Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one
more letter at a random position.

Find the letter that was added in t.

样例
Input:
s = “abcd”
t = “abcde”

Output:
e

Explanation:
‘e’ is the letter that was added.

public class Solution {
    /**
     * @param s: a string
     * @param t: a string
     * @return: the letter that was added in t
     */
    public char findTheDifference(String s, String t) {
        // Write your code here
        int a[]=new int[26];
        int b[]=new int[26];
        for(char c:s.toCharArray())
            a[c-'a']++;
        for(char c:t.toCharArray())
            b[c-'a']++;
        char res = 0;
        for(int i=0;i<26;i++)
            if(b[i]-a[i]>0)
                res=(char)(i+'a');
        return res;
    }
}

1256. Nth Digit

Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, …

样例
Example 1:

Input:
3

Output:
3
Example 2:

Input:
11

Output:
0

Explanation:
The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … is a 0, which is part of the number 10.

public class Solution {
    /**
     * @param n: a positive integer
     * @return: the nth digit of the infinite integer sequence
     */
    public int findNthDigit(int n) {
        // write your code here
        if(n<=0)
            return 0;
        long count=9;
        int start=1;
        int len=1;
        while (n>len*count){
            n-=len*count;
            start*=10;
            count*=10;
            len++;
        }
        start+=(n-1)/len;
        return String.valueOf(start).charAt((n-1)%len)-'0';
    }
}

猜你喜欢

转载自blog.csdn.net/Mikeoperfect/article/details/80910432