Java short and easy to understand beat %97


  • 0
    A
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            int[] sorted = Arrays.copyOf(nums, nums.length);
            Arrays.sort(sorted);
            String[] ret = new String[nums.length];
            
            for (int i = 0; i < nums.length; i++) {
                int r = Arrays.binarySearch(sorted, nums[i]);
                if (r == nums.length-1) {
                    ret[i] = "Gold Medal";
                }
                else if (r == nums.length-2) {
                    ret[i] = "Silver Medal";
                }
                else if (r == nums.length -3) {
                    ret[i] = "Bronze Medal";
                }
                else {
                    ret[i] = Integer.toString(nums.length - r);
                }
            }
            return ret;
        }
    }
    

  • 0
    P

    @adammama said in Java short and easy to understand beat %97:

    public class Solution {
    public String[] findRelativeRanks(int[] nums) {
    int[] sorted = Arrays.copyOf(nums, nums.length);
    Arrays.sort(sorted);
    String[] ret = new String[nums.length];

        for (int i = 0; i < nums.length; i++) {
            int r = Arrays.binarySearch(sorted, nums[i]);
            if (r == nums.length-1) {
                ret[i] = "Gold Medal";
            }
            else if (r == nums.length-2) {
                ret[i] = "Silver Medal";
            }
            else if (r == nums.length -3) {
                ret[i] = "Bronze Medal";
            }
            else {
                ret[i] = Integer.toString(nums.length - r);
            }
        }
        return ret;
    }
    

    }

    elegant


Log in to reply
 

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