LintCode第三十三天

420. 报数

报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。

如下所示:

1, 11, 21, 1211, 111221, …

1 读作 “one 1” -> 11.

11 读作 “two 1s” -> 21.

21 读作 “one 2, then one 1” -> 1211.

给定一个整数 n, 返回 第 n 个顺序。

样例
给定 n = 5, 返回 “111221”.


public class Solution {
    /**
     * @param n: the nth
     * @return: the nth sequence
     */
    public String countAndSay(int n) {
        // write your code here
        String oldStr="1";
        while (n-->1){
            StringBuilder s=new StringBuilder();
            char []old=oldStr.toCharArray();
            for(int i=0;i<oldStr.length();i++){
                int count=1;
                while ((i+1)<oldStr.length()&&old[i]==old[i+1]){
                    count++;
                    i++;
                }
                s.append(String.valueOf(count)+String.valueOf(old[i]));
            }
            oldStr=s.toString();
        }
        return oldStr;
    }
}

1079. Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings
that have the same number of 0’s and 1’s, and all the 0’s and all the
1’s in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times
they occur.

样例
Example 1:

Input: “00110011”
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1’s and 0’s: “0011”, “01”, “1100”, “10”, “0011”, and “01”.

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, “00110011” is not a valid substring because all the 0’s (and 1’s) are not grouped together.
Example 2:

Input: “10101”
Output: 4
Explanation: There are 4 substrings: “10”, “01”, “10”, “01” that have equal number of consecutive 1’s and 0’s.

public class Solution {
    /**
     * @param s: a string
     * @return: the number of substrings
     */
    public int countBinarySubstrings(String s) {
        // Write your code here
        int pre=0,cur=1,res=0;
        for(int i=1;i<s.length();i++){
            if(s.charAt(i-1)==s.charAt(i))
                cur++;
            else {
                pre=cur;
                cur=1;
            }
            if(pre>=cur)
                ++res;
        }
        return res;
    }
}

1086. Repeated String Match

Given two strings A and B, find the minimum number of times A has to
be repeated such that B is a substring of it. If no such solution,
return -1.

样例
with A = “abcd” and B = “cdabcdab”.

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times (“abcdabcd”).

public class Solution {
    /**
     * @param A: a string
     * @param B: a string
     * @return: return an integer
     */
    public int repeatedStringMatch(String A, String B) {
        // write your code here
        StringBuilder T= new StringBuilder(A);
        for(int i=1;i<=B.length()/A.length()+2;i++){
            if(T.toString().contains(B))
                return i;
            T.append(A);
        }
        return -1;
    }
}

1094. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the
non-negative value, where each node in this tree has exactly two or
zero sub-node. If the node has two sub-nodes, then this node’s value
is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value
in the set made of all the nodes’ value in the whole tree.

If no such second minimum value exists, output -1 instead.

样例
Example 1:

Input:
2
/ \
2 5
/ \
5 7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.
Example 2:

Input:
2
/ \
2 2

Output: -1
Explanation: The smallest value is 2, but there isn’t any second smallest value.

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root
     * @return: the second minimum value in the set made of all the nodes' value in the whole tree
     */
    Set<Integer> list=new HashSet<>();
    public void helper(TreeNode root,Set<Integer>list){
        if(root==null)
            return;
        if(root.left!=null)
            helper(root.left,list);
        list.add(root.val);
        if(root.right!=null)
            helper(root.right,list);
    }
    public int findSecondMinimumValue(TreeNode root){
            int Min=Integer.MAX_VALUE;
            int res=Integer.MAX_VALUE;
            helper(root,list);
            for(int i:list){
                System.out.println(i);
                if(i<Min)
                    Min=i;
                else if(i<res)
                        res=i;
            }
            if(list.size()==1)
                return -1;
            else return res;
    }
}

1099. Non-decreasing Array

Given an array with n integers, your task is to check if it could
become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds
for every i (1 <= i < n).

样例
Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:

Input: [4,2,1]
Output: False
Explanation: You can’t get a non-decreasing array by modify at most one element.

public class Solution {
    /**
     * @param nums: an array
     * @return: if it could become non-decreasing by modifying at most 1 element
     */
    public boolean checkPossibility(int[] nums) {
        // Write your code here
         int cnt=1,n=nums.length;
        for(int i=1;i<n;i++){
            if(nums[i]<nums[i-1]) {
                if (cnt == 0) return false;
                if (i == 1 || nums[i] >= nums[i - 2])
                    nums[i - 1] = nums[i];
                else nums[i] = nums[i - 1];
                cnt--;
            }
        }
        return true;
    }
}

猜你喜欢

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