Java using sort


  • 0
    M
    public class Solution {
        public int kthSmallest(int[][] matrix, int k) {
            if(matrix.length != 0){
                List<Integer> l = new ArrayList<Integer>();
                int j = 0;
                for(int i = 0; i < matrix.length; i++){
                    j = 0;
                    while(j < matrix[0].length){
                        l.add(matrix[i][j]); // Add All elements to ArrayList
                        j++;
                    }
                }
                Collections.sort(l);  // Perform Sort
                return l.get(k-1);     // Get kth element
            }
            return 0;
        }
    }
    

Log in to reply
 

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