General

Spiral 2-D array

for loop half of array size, add elements in 4 directions

public static List<Integer> matrixInSpiralOrder(List<List<Integer>> squareMatrix){
    List<Integer> result = new ArrayList<>();
    for (int i =0; i < Math.ceil(squareMatrix.size() * 0.5); i++){
        helper(squareMatrix, result, i);
    }
    return result;
}

private static void helper(List<List<Integer>> squareMatrix, List<Integer> result, int offset){
    int size = squareMatrix.size();
    if(offset * 2 + 1 == size){
        result.add(squareMatrix.get(offset).get(offset));
        return;
    }
    for (int i = offset; i < size - offset - 1; i++){
        result.add(squareMatrix.get(offset).get(i));
    }

    for (int i = offset; i < size - offset - 1; i++){
        result.add(squareMatrix.get(i).get(size - 1 - offset));
    }

    for (int i = size - 1 - offset; i > offset ; i--){
        result.add(squareMatrix.get(size - 1 - offset).get(i));
    }

    for (int i = size - 1 - offset; i > offset ; i--){
        result.add(squareMatrix.get(i).get(offset));
    }
}

395. Longest Substring with At Least K Repeating Characters

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

  • Find pivots whose's count less than k,split the string by pivots,untill no pivot in substring

Last updated

Was this helpful?