LintCode第三十五天

1401. 抽搐词

我们正常的单词不会有连续两个以上相同的字母,如果出现连续三个或以上的字母,那么这是一个抽搐词。现在给一个单词,从左至右求出所有抽搐字母的起始点和结束点。

样例
给出 str = “whaaaaatttsup”, 返回 [[2,6],[7,9]]。

解释:
“aaaa”和”ttt”是抽搐字母,输出他们的起始点和结束点。
给出 str = “whooooisssbesssst”, 返回 [[2,5],[7,9],[12,15]]。

解释:
“ooo”和”sss”和”ssss”都是抽搐字母,输出他们的起始点和结束点。

public class Solution {
    /**
     * @param str: the origin string
     * @return: the start and end of every twitch words
     */
    public int[][] twitchWords(String str) {
        // Write your code here
        List<Integer>list=new ArrayList<>();
        int start=0,end=0;
          while (end<str.length()){
            while (str.charAt(start)==str.charAt(end)){
                if(++end==str.length())
                    break;
            }
            if(end-start>=3){
                list.add(start);
                list.add(end-1);
            }
            start=end;
        }

        int[][]result=new int[list.size()/2][2];
        int i=0;
        while (i<list.size()/2){
            result[i][0]=list.get(2*i);
            result[i][1]=list.get(2*i+1);
            i++;
        }
        return result;
    }
}

385. 幸运数字8

8是小九的幸运数字,小九想知道在1~n的数中有多少个数字含有8。

样例
给出 n = 20, 返回2。

解释:
只有8,18 含有8。

public class Solution {
    /**
     * @param n: count lucky numbers from 1 ~ n
     * @return: the numbers of lucky number
     */
   public boolean helper(int n){
        while (n!=0){
            if(n%10==8)
                return true;
            n/=10;
        }
        return false;
    }
    public int luckyNumber(int n){
        int sum=0;
        for(int i=8;i<=n;i++)
            if(helper(i))
                sum++;
        return sum;
    }
}

1368. 相同数字

给一个数组,如果数组中存在相同数字,且相同数字的距离小于给定值k,输出YES,否则输出NO。

样例
给出 array = [1,2,3,1,5,9,3], k = 4, 返回 “YES”。

解释:
index为3的1和index为0的1距离为3,满足题意输出YES。
给出 array =[1,2,3,5,7,1,5,1,3], k = 4, 返回 “YES”。

解释:
index为7的1和index为5的1距离为2,满足题意。

public class Solution {
    /**
     * @param nums: the arrays
     * @param k: the distance of the same number
     * @return: the ans of this question
     */
    public String sameNumber(int[] nums, int k) {
        // Write your code here
        HashMap<Integer,Integer>map=new HashMap<>();
        int min=Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
            if(!map.containsKey(nums[i]))
                map.put(nums[i],i);
            else{
                if(min>(i-map.get(nums[i]))) {
                    min = i -map.get(nums[i]);
                    map.put(nums[i],i);
                }
            }
        if(min<k)
            return "YES";
        else return "NO"; 
    }
}

1360. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie,
symmetric around its center).

样例
For example, this binary tree {1,2,2,3,4,4,3} is symmetric:

1

/ \
2 2
/ \ / \
3 4 4 3
But the following {1,2,2,#,3,#,3} is not:

1

/ \
2 2
\ \
3 3

/**
 * 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: root of the given tree
     * @return: whether it is a mirror of itself 
     */
    public boolean helper(TreeNode left,TreeNode right){
        if(left==null&&right==null)
            return true;
        if(left==null||right==null)
            return false;
        if(left.val!=right.val)
            return false;
        else return helper(left.left,right.right)&&helper(left.right,right.left);
    }
    public boolean isSymmetric(TreeNode root){
        if(root==null)
            return true;
        return helper(root.left,root.right);
    }
}

1334. Rotate Array

Given an array, rotate the array to the right by k steps, where k is
non-negative.

样例
Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

public class Solution {
    /**
     * @param nums: an array
     * @param k: an integer
     * @return: rotate the array to the right by k steps
     */
    public int[] rotate(int[] nums, int k) {
        // Write your code here
        int[]reslut=new int[nums.length];
        if(nums.length<=1)
        return nums;
        if(nums.length<k)
          k%=nums.length;
        int len=nums.length-k;
        List<Integer>list=new ArrayList<>();
        for(int i=0;i<len;i++)
            list.add(nums[i]);

        for(int i=len;i<nums.length;i++)
            reslut[i-len]=nums[i];
        for(int i=0;i<list.size();i++)
            reslut[k+i]=list.get(i);
        return reslut;
    }
}

猜你喜欢

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