LintCode第二十八天

845. Greatest Common Divisor

Given two numbers, number a and number b. Find the greatest common
divisor of the given two numbers.

样例
Given a = 10, b = 15, return 5.
Given a = 15, b = 30, return 15.

public class Solution {
    /**
     * @param a: the given number
     * @param b: another number
     * @return: the greatest common divisor of two numbers
     */
    public int gcd(int a, int b) {
        // write your code here
        if(a<b){
            a=a+b;
            b=a-b;
            a=a-b;
        }
        int c;
        while (b>0){
            c=a%b;
            a=b;
            b=c;
        }
        return a;
    }
}

868. 子数组的最大平均值

给定一个由n个整数组成的数组,找到给定长度k的连续子数组,该子数组具有最大平均值。你需要输出最大平均值。

样例
给定nums = [1,12,-5,-6,50,3], k = 4,返回12.75

解释:
最大平均为(12-5-6+50)/4 = 51/4 = 12.75。
输入测试数据 (每行一个参数)

public class Solution {
    /**
     * @param nums: an array
     * @param k: an integer
     * @return: the maximum average value
     */
    public double findMaxAverage(int[] nums, int k) {
        // Write your code here
        int Max=Integer.MIN_VALUE;
        int sum=0,start=0,end=k-1;
        while(end<nums.length){
            sum=0;
            for(int i=start;i<=end;i++)
                sum+=nums[i];
            if(sum>Max)
                Max=sum;
            start++;
            end++;
        }
        return (double)Max/k;
    }
}

891. Valid Palindrome II

Given a non-empty string s, you may delete at most one character.
Judge whether you can make it a palindrome.

样例
Given s = “aba” return true
Given s = “abca” return true // delete c

public class Solution {
    /**
     * @param s: a string
     * @return: nothing
     */
    public boolean validPalindrome(String s) {
        // Write your code here
     int left=0,right=s.length()-1;
        while (left<right){
            if(s.charAt(left)!=s.charAt(right))
                break;
            left++;
            right--;
        }
        if(left>right)
            return true;
        return helper(s,left+1,right)||helper(s,left,right-1);
    }
    private boolean helper(String s,int left,int right){
        while (left<right){
            if(s.charAt(left)!=s.charAt(right))
                return false;
            left++;
            right--;
        }
        return true;
    }
}

900. Closest Binary Search Tree Value

Given a non-empty binary search tree and a target value, find the
value in the BST that is closest to the target.

样例
Given root = {1}, target = 4.428571, return 1.

/**
 * 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 given BST
     * @param target: the given target
     * @return: the value in the BST that is closest to the target
     */
    double min=(double)Integer.MAX_VALUE;
    int result=0;
    public int closestValue(TreeNode root,double target) {
        if(Math.abs(root.val-target)<min){
            min=Math.abs(root.val-target);
            result=root.val;
        }
        if(root.left!=null)
            closestValue(root.left,target);
        if(root.right!=null)
            closestValue(root.right,target);
        return result;
    }
}

888. 有效单词词广场

给定一个单词序列,检查它是否构成一个有效单词广场。
一个有效的单词广场满足:如果第k行和第k列读取相同的字符串,并且0≤k

public class Solution {
    /**
     * @param words: a list of string
     * @return: a boolean
     */
    public boolean validWordSquare(String[] words) {
        // Write your code here
        for(int i=0;i<words.length;i++) {
            for (int j = 0; j < words[i].length(); j++){
                try {
                     if(words[i].charAt(j)!=words[j].charAt(i))
                         return false;
                }catch (Exception e){
                    return false;
                }
            }
        }
        return true;
    }
}

猜你喜欢

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