数组-Array

  • 新建数组

    1
    2
    int array = new int[size];
    int array = new int[]{1,2,3,4};
  • 数组的下标是从0开始

  • Java中,访问数组注意是否越界
  • 打擂台算法
    Example:找出数组中前两大的数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class Solution {
    /**
    * @param matrix: an input array
    * @return: res[0]: the maximum,res[1]: the 2nd maximum
    */
    public int[] first2Max(int[] matrix) {
    if (nums == null || nums.length == 0) {
    return new int[0];
    }
    int[] res = new int[] {Integer.MIN_VALUE, Integer.MIN_VALUE};
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] > res[1]) {
    if (nums[i] > res[0]) {
    res[0] = nums[i];
    res[1] = res[0];
    } else {
    res[1] = nums[i];
    }
    }
    }
    return res;
    }
    }
  • 排序算法

  • 双指针算法

Lintcode 相关题目