[日常刷题]leetcode D40 D41

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83217691

479. Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers.

Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

Input: 2

Output: 987

Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Note:

The range of n is [1,8].

Solution in C++:

关键点:

  • 逆向思维

思路:

  • 开始拿到的时候没什么思路,连暴力自己都不想做了,后来看了解析加自己缕了下思路,感觉也不是很难。因为n-digit,所以范围是有限的,然后就是先构造当前n-digit最大的回文数字,再循环判断是否有满足条件的即可。
    int largestPalindrome(int n) {
        if (n == 1) return 9;
        int upper = pow(10, n) - 1;
        int lower = pow(10, n-1);
        for (int i = upper; i >= lower; i--) {
            long cand = buildPalindrome(i);
            for (long j = upper; j*j >= cand; j--) {
                if (cand % j == 0 && cand / j <= upper) {
                    return cand % 1337;
                }
            }
        }
        return -1;
    }
    
    long buildPalindrome(int n) {
        string s = to_string(n);
        reverse(s.begin(), s.end());
        return stol(to_string(n) + s);
    }

504. Base 7

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].

Solution in C++:

关键点:

  • 注意边界

思路:

  • 这就是个很简单的进制转换问题,只要注意一下正负数的处理以及边界值0即可。
string convertToBase7(int num) {
        if (num == 0)
            return "0";
        
        string result;
        bool flag = false;
        if (num < 0) 
            flag = true;
        num = abs(num);
        
        while(num){
            result = to_string(num%7) + result;
            num /= 7;
        }
       
        if (flag)
            return '-'+result;
        else
            return result;
    }

506. Relative Ranks

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.

Solution in C++:

关键点:

  • 分数与名词映射关系

思路:

  • 这里我的主要想法就是,将数组排序,然后使其数值大小作为map中的key,value就是其对应的名次即可。注意不要直接修改原来的数组值。
vector<string> findRelativeRanks(vector<int>& nums) {
        size_t size = nums.size();
        vector<string> result;
        if (size == 0)
            return result;
        
        vector<int> tmp = nums;
        sort(tmp.begin(), tmp.end());
        map<int, string> maps;
        for(int i = 0; i < size; ++i){
            if (size-i == 1)
                maps[tmp[i]] = "Gold Medal";
            else if (size-i == 2)
                maps[tmp[i]] = "Silver Medal";
            else if (size-i == 3)
                maps[tmp[i]] =  "Bronze Medal";
            else
                maps[tmp[i]] = to_string(size-i);
        }
        
        for(auto num : nums){
            result.push_back(maps[num]);
        }
        
        return result;
    }

507. Perfect Number

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:

Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14

Note: The input number n will not exceed 100,000,000. (1e8)

Solution in C++:

关键点:

  • 不要包括自身

思路:

  • 就是很简单的计算自己的因子的方式,累加然后再判断。
bool checkPerfectNumber(int num) {
        int sum = 0;
        if (num == 0)
            return false;
        
        for(int i = 1; i <= sqrt(num); ++i){
            if (num % i == 0){
                if (i != num)
                    sum += i;
                if (num / i != num)
                    sum += num / i;
            }
        }
        if (sum == num)
            return true;
        else
            return false;
    }

520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like “USA”.
  2. All letters in this word are not capitals, like “leetcode”.
  3. Only the first letter in this word is capital if it has more than one letter, like “Google”.
    Otherwise, we define that this word doesn’t use capitals in a right way.
    Example 1:
Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

Solution in C++:

关键点:

思路:

  • 主要根据几个case,然后遍历字符串判断这题就出来了。
bool detectCapitalUse(string word) {
        if (word == "")
            return false;
 
        bool startCapital = isupper(word[0]);
        int capital = 0;
        int lower = 0;
        size_t size = word.size();
        for(int i = 1; i < size; ++i){
            if (isupper(word[i]))
                ++capital;
            else
                ++lower;
        }
        
        if (startCapital){
            // case 1 全大写 case 3 只有开头大写其他都为小写
            if (capital == size - 1 || lower == size - 1)
                return true;
            else
                return false;
        } else{
            // case 2 全小写
            if (lower == size - 1)
                return true;
            else
                return false;
        }
    }

小结

昨天懒了一下,额,所以今天标题就相当于补了昨天的,方便以后数据分析自身的情况。今天好像没有什么大的收获。

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83217691