LintCode第二十五天

766. 闰年

判断给出的年份 n 是否为闰年. 如果 n 为闰年则返回 true

样例
给出 n = 2008 返回 true
给出 n = 2018 返回 false

public class Solution {
    /**
     * @param n: a number represent year
     * @return: whether year n is a leap year.
     */
    public boolean isLeapYear(int n) {
        // write your code here
        if(n%100==0){
            if(n%400==0)
                return true;
            else return false;
        }else {
            if(n%4==0)
                return true;
            else return false;
        }
    }
}

767. 翻转数组

原地翻转给出的数组 nums

样例
给出 nums = [1,2,5]
返回 [5,2,1]

public class Solution {
    /**
     * @param nums: a integer array
     * @return: nothing
     */
    public void reverseArray(int[] nums) {
        // write your code here
        int len=nums.length;
            for(int i=0;i<len/2;i++){
                int temp=nums[i];
                nums[i]=nums[len-1-i];
                nums[len-1-i]=temp;
            }
    }
}

768. 杨辉三角

给一整数 n, 返回杨辉三角的前 n 行

样例
给出 n = 4
返回
[
[1]
[1,1]
[1,2,1]
[1,3,3,1]
]

public class Solution {
    /**
     * @param n: a Integer
     * @return: the first n-line Yang Hui's triangle
     */
    public List<List<Integer>> calcYangHuisTriangle(int n) {
        // write your code here
        List<List<Integer>> lists=new ArrayList<>();
        if(n==0)
            return lists;
        for(int i=0;i<n;i++){
            List<Integer>list=new ArrayList<>();
            list.add(1);
            for(int j=1;j<i;j++)
                list.add(lists.get(i-1).get(j-1)+lists.get(i-1).get(j));
            if(i>0)
                list.add(1);
            lists.add(list);
        }
        return lists;
    }
}

69. 螺旋矩阵

给出整数 n, 返回一个大小为 n * n 的螺旋矩阵

样例
给出 n = 3
则螺旋矩阵为:

[
[1,2,3]
[8,9,4]
[7,6,5]
]
给出 n = 5
则螺旋矩阵为:

[
[1,2,3,4,5]
[16,17,18,19,6]
[15,24,25,20,7]
[14,23,22,21,8]
[13,12,11,10,9]
]

public class Solution {
    /**
     * @param n: a Integer
     * @return: a spiral array
     */
    public int[][] spiralArray(int n) {
        // write your code here
         int result[][]=new int[n][n];
        int row=0;
        int col=n-1;
        int num=1;
        int temp=n-1;
        for(int i=0;i<n;i++){
            result[0][i]=num;
            num++;
        }
        while (temp>0){
            for(int i=1;i<=temp;i++){
                row++;
                result[row][col]=num;
                num++;
            }
            for(int i=1;i<=temp;i++){
                col--;
                result[row][col]=num;
                num++;
            }
            temp--;
            if(temp==0)
                break;
            for(int i=1;i<=temp;i++){
                row--;
                result[row][col]=num;
                num++;
            }
            for(int i=1;i<=temp;i++){
                col++;
                result[row][col]=num;
                num++;
            }
            temp--;
        }
        return result;
    }
}

70. 最大数和最小数

给定一个矩阵,返回矩阵中的最大数和最小数

样例
给定一个矩阵:

[
[1,2,3],
[4,3,2],
[6,4,4]
]
返回 [6,1]

public class Solution {
    /**
     * @param matrix: an input matrix 
     * @return: nums[0]: the maximum,nums[1]: the minimum
     */
    public int[] maxAndMin(int[][] matrix) {
        // write your code here
            int []result;

        if(matrix==null||matrix.length<1)
            return  new int[0];
        int min=Integer.MAX_VALUE;
        int max=Integer.MIN_VALUE;
        for(int i=0;i<matrix.length;i++)
            for(int j=0;j<matrix[0].length;j++){
            if(matrix[i][j]>max)
                max=matrix[i][j];
            if(matrix[i][j]<min)
                min=matrix[i][j];
            }
        result=new int[2];
        result[0]=max;
        result[1]=min;
        return result;
    }
}

小结

难受,昨天被华为鄙视了一顿,确实自己也实在是菜的掉渣了,也难怪那个华为的面试官会如此轻蔑的评价我的简历,一个劲的建议我考研,研我是要读的,但不是现在,如果我出生于富锦世家,那自然无需多言必定读研,但中国的社会等级决定了并不是每个家庭都是大富大贵之流,我已经成年了,我有自己的想法,正因为自己太过于平凡,所以才不想这辈子再继续平庸下去,华为这个优招专场,有意思,也算是和硕士们、博士们同台竞技过了一会,不亏,不服!继续战斗!

猜你喜欢

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