美文网首页
506. Relative Ranks为啥java和C++用相同

506. Relative Ranks为啥java和C++用相同

作者: Yuu_CX | 来源:发表于2017-02-13 22:14 被阅读0次

    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;
        }
    };
    

    相关文章

      网友评论

          本文标题:506. Relative Ranks为啥java和C++用相同

          本文链接:https://www.haomeiwen.com/subject/wnitwttx.html