java
public class Solution {
public String[] findRelativeRanks(int[] nums) {
String[] result=new String[nums.length];
int[] res = nums;
Arrays.sort(res);
int a;
for(int i=0;i<nums.length;i++){
for(int j=0;j<nums.length;j++){
if(nums[i] == res[j]){
a=nums.length-j;
switch(a) {
case 1:
result[i]="Gold Medal";
break;
case 2:
result[i]="Silver Medal";
break;
case 3:
result[i]="Bronze Medal";
break;
default:
result[i]=String.valueOf(a);
break;
}
break;
}
}
}
return result;
}
}
C++
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
vector<int> arr = nums;
sort(arr.begin(), arr.end());
vector<string> result(nums.size());
int a;
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < nums.size(); j++) {
if (nums[i] == arr[j]) {
a=nums.size()-j;
switch(a) {
case 1: result[i] = "Gold Medal"; break;
case 2: result[i] = "Silver Medal"; break;
case 3: result[i] = "Bronze Medal"; break;
default: result[i] = to_string(a); break;
}
break;
}
}
}
return result;
}
};
网友评论