Java solution using ArayList


  • 0
    A
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            ArrayList<Integer> a = new ArrayList<>();
            for (int i=0; i< nums.length; i++){
                a.add(nums[i]);
            }
            Collections.sort(a);
            Collections.reverse(a);
            String[] s = new String[nums.length];
    
            for (int i = 0; i< nums.length; i++){
                if(nums[i] == a.get(0))
                    s[i] = "Gold Medal";
                else if(nums[i] == a.get(1))
                    s[i] = "Silver Medal";
                else if(nums[i] == a.get(2))
                    s[i] = "Bronze Medal";
                else
                    s[i] = Integer.toString(a.indexOf(nums[i])+1);
            }
            return s;
        }
    }
    

Log in to reply
 

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