码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】未分类(tag里面没有)(共题)

时间:2018-11-25 20:25:53      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:alt   lap   xpl   you   isp   who   positive   题解   convert   

【504】Base 7 (2018年11月25日)

Given an integer, return its base 7 string representation.

Example 1:
Input: 100
Output: "202"

Example 2:
Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

题解:直接按照进制转换

技术分享图片
 1 class Solution {
 2 public:
 3     string convertToBase7(int num) {
 4         bool negative = false;
 5         if (num < 0) {
 6             negative = true;
 7             num = -num;
 8         }
 9         string ret = "";
10         while (num != 0) {
11             int mod = num % 7;
12             num = num / 7;
13             ret = to_string(mod) + ret;
14         }
15         ret = negative ? "-" + ret : ret;
16         ret = ret == "" ? "0" : ret;
17         return ret;
18     }
19 };
View Code

 

【506】Relative Ranks(2018年11月25日)

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

Example 1:
Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won‘t exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.
技术分享图片
 1 class Solution {
 2 public:
 3     vector<string> findRelativeRanks(vector<int>& nums) {
 4         const int n = nums.size();
 5         map<int, int, greater<int>> mp;
 6         for (int i = 0; i < nums.size(); ++i) {
 7             int score = nums[i];
 8             mp[score] = i;
 9         }
10         vector<string> ret(n);
11         int cnt = 1;
12         for (auto e : mp) {
13             int idx = e.second;
14             if (cnt <= 3) {
15                 if (cnt == 1) {
16                     ret[idx] = "Gold Medal";
17                 }
18                 if (cnt == 2) {
19                     ret[idx] = "Silver Medal";
20                 }
21                 if (cnt == 3) {
22                     ret[idx] = "Bronze Medal";
23                 }
24                 ++cnt;
25             } else {
26                 ret[idx] = to_string(cnt++);
27             }
28         }
29         return ret;
30     }
31 };
View Code

 

【LeetCode】未分类(tag里面没有)(共题)

标签:alt   lap   xpl   you   isp   who   positive   题解   convert   

原文地址:https://www.cnblogs.com/zhangwanying/p/10016508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!