LintCode第三十二天

1046. Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L,
R] (inclusive) having a prime number of set bits in their binary
representation.

(Recall that the number of set bits an integer has is the number of 1s
present when written in binary. For example, 21 written in binary is
10101 which has 3 set bits. Also, 1 is not a prime.)

样例
Example 1:

Input: L = 6, R = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
9 -> 1001 (2 set bits , 2 is prime)
10->1010 (2 set bits , 2 is prime)
Example 2:

Input: L = 10, R = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)

public class Solution {
    /**
     * @param L: an integer
     * @param R: an integer
     * @return: the count of numbers in the range [L, R] having a prime number of set bits in their binary representation
     */
    public int CountHelper(int n){
        int sum=0;
        while (n!=0){
            sum++;
            n&=n-1;
        }
        return sum;
    }
    public boolean JudgeHelper(int n){
        if(n==1)
            return false;
        if(n==2)
            return true;
        for(int i=2;i*i<=n;i++){
            if(n%i==0)
                return false;
        }
        return true;
    }
    public int countPrimeSetBits(int L,int R){
        int sum=0;
        for(int i=L;i<=R;i++){
            if(JudgeHelper(CountHelper(i)))
                sum++;
        }
        return sum;
    }
}

1054. Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i]
assigned (0 indexed).

Once you pay the cost, you can either climb one or two steps. You need
to find minimum cost to reach the top of the floor, and you can either
start from the step with index 0, or the step with index 1.

样例
Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].

public class Solution {
    /**
     * @param cost: an array
     * @return: minimum cost to reach the top of the floor
     */
    public int minCostClimbingStairs(int[] cost) {
        // Write your code here
    int[]dp=new int[cost.length];
        dp[0]=cost[0];
        dp[1]=cost[1];
        for(int i=2;i<cost.length;i++)
            dp[i]=cost[i]+Math.min(dp[i-1],dp[i-2]);
        return Math.min(dp[cost.length-1],dp[cost.length-2]);
    }
}

1056. Find Smallest Letter Greater Than Target

Given a list of sorted characters letters containing only lowercase
letters, and given a target letter target, find the smallest element
in the list that is larger than the given target.

Letters also wrap around. For example, if the target is target = ‘z’
and letters = [‘a’, ‘b’], the answer is ‘a’.

样例
Input:
letters = [“c”, “f”, “j”]
target = “a”
Output: “c”

Input:
letters = [“c”, “f”, “j”]
target = “c”
Output: “f”

Input:
letters = [“c”, “f”, “j”]
target = “d”
Output: “f”

Input:
letters = [“c”, “f”, “j”]
target = “g”
Output: “j”

Input:
letters = [“c”, “f”, “j”]
target = “j”
Output: “c”

Input:
letters = [“c”, “f”, “j”]
target = “k”
Output: “c”

public class Solution {
    /**
     * @param letters: a list of sorted characters
     * @param target: a target letter
     * @return: the smallest element in the list that is larger than the given target
     */
    public char nextGreatestLetter(char[] letters, char target) {
        // Write your code here
        char res=letters[0];
        for(int i=1;i<letters.length;i++){
            if(letters[i-1]<=target&&letters[i]>target)
                res=letters[i];
        }
        return res;
    }
}

1062. Flood Fill

An image is represented by a 2-D array of integers, each integer
representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and
column) of the flood fill, and a pixel value newColor, “flood fill”
the image.

To perform a “flood fill”, consider the starting pixel, plus any
pixels connected 4-directionally to the starting pixel of the same
color as the starting pixel, plus any pixels connected 4-directionally
to those pixels (also with the same color as the starting pixel), and
so on. Replace the color of all of the aforementioned pixels with the
newColor.

At the end, return the modified image.

样例
Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

public class Solution {
    /**
     * @param image: a 2-D array
     * @param sr: an integer
     * @param sc: an integer
     * @param newColor: an integer
     * @return: the modified image
     */
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        // Write your code here
    if(image[sr][sc]==newColor)
            return image;
        helper(image,sr,sc,image[sr][sc],newColor);
        return image;
    }
    void helper(int[][]image,int i,int j,int color,int newColor){
        int m=image.length,n=image[0].length;
        if(i<0||i>=m||j<0||j>=n||image[i][j]!=color)
            return;
        image[i][j]=newColor;
        helper(image,i+1,j,color,newColor);
        helper(image,i,j+1,color,newColor);
        helper(image,i-1,j,color,newColor);
        helper(image,i,j-1,color,newColor);
    }
}

1068. Find Pivot Index

Given an array of integers nums, write a method that returns the
“pivot” index of this array.

We define the pivot index as the index where the sum of the numbers to
the left of the index is equal to the sum of the numbers to the right
of the index.

If no such index exists, we should return -1. If there are multiple
pivot indexes, you should return the left-most pivot index.

样例
Example 1:
Input:
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
Example 2:
Input:
nums = [1, 2, 3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.

public class Solution {
    /**
     * @param nums: an array
     * @return: the "pivot" index of this array
     */
    public int SumInt(List<Integer>list){
        int sum=0;
        for(int i:list)
            sum+=i;
        return sum;
    }
    public int pivotIndex(int[]nums){
        int sum=0;
        for(int i:nums)
            sum+=i;
        List<Integer>list=new LinkedList<>();
        for(int i=0;i<nums.length;i++){
            if(sum-nums[i]==2*SumInt(list))
                return i;
            list.add(nums[i]);
        }
        return -1;
    }
}

  1. Longest Word in Dictionary
    Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.

If there is no answer, return the empty string.

样例
Example 1:

Input:
words = [“w”,”wo”,”wor”,”worl”, “world”]
Output: “world”
Explanation:
The word “world” can be built one character at a time by “w”, “wo”, “wor”, and “worl”.
Example 2:

Input:
words = [“a”, “banana”, “app”, “appl”, “ap”, “apply”, “apple”]
Output: “apple”
Explanation:
Both “apply” and “apple” can be built from other words in the dictionary. However, “apple” is lexicographically smaller than “apply”.

public class Solution {
    /**
     * @param words: a list of strings
     * @return: the longest word in words that can be built one character at a time by other words in words
     */
    public String longestWord(String[] words) {
        // Write your code here
        String res="";
        Arrays.sort(words);
        Set<String>set=new HashSet<>();
        for(String word:words){
            if(word.length()==1||set.contains(word.substring(0,word.length()-1))){
                res=(word.length()>res.length())?word:res;
                set.add(word);
            }
        }
        return res;
    }
}

猜你喜欢

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