Used integer arraylist to sort elements


  • 0
    T
    class Solution {
        public int kthSmallest(int[][] matrix, int k) {
            List<Integer> sList = new ArrayList<Integer>();
            
            if(matrix[0].length > 0 && matrix.length > 0) {
                for(int i=0; i<matrix[0].length; i++) {
                    for(int j=0; j<matrix.length; j++) {
                        sList.add(matrix[i][j]);
                    }
                }
            }
            if(sList != null) {
                Collections.sort(sList);
            }
            if(k > 0) {
                k = k-1;
            }
            return sList.get(k);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.