zoukankan      html  css  js  c++  java
  • 【LeetCode】水题(刚开始重新刷题找感觉用的)

    [9] Palindrome Number [Easy]

    给一个数字,用不转化成字符串的方式判断它是否是回文。

    先求数字长度,然后把数字的后半段做翻转(就是不断地取模,除10这种方式),然后判断前后半段是否相等

    有特殊情况,负数,10的倍数, 和零 

     1 class Solution {
     2 public:
     3     bool isPalindrome(int x) {
     4         if (x < 0 || x % 10 == 0 && x != 0) {
     5             return false;
     6         }
     7         //=============[1]先求数字长度
     8         int len = 1;
     9         int tmpx = x;
    10         while (tmpx / 10 != 0) {
    11             tmpx /= 10;
    12             len++;
    13         }
    14         cout << len << endl;
    15         
    16         //=============[2]然后翻转后面一半数字
    17         int reverse = 0;
    18         tmpx = x;
    19         for (int i = 1; i < len / 2 + 1; ++i) {
    20             reverse = reverse * 10 + (tmpx % 10);
    21             tmpx /= 10;
    22         }
    23         cout << tmpx << "   " << reverse << endl;
    24         
    25         //=============[3]根据长度的奇偶性判断是否需要再除以10
    26         if (len % 2 == 1) {
    27             tmpx /= 10;
    28         } 
    29         if (tmpx != reverse) {
    30             return false;
    31         }
    32         return true;
    33     }
    34 };
    View Code

     

    [157] Read N Characters Given Read4 [Easy] 

    给个read4的api,每次能从文件里面读四个字符,最多读N个字符,考虑到文件里面的字符数目可能比N小,所以不能直接返回N。

    每次应该 加上 min(read4(buf + k),  n); 这种表达, 代码如下:

     1 // Forward declaration of the read4 API.
     2 int read4(char *buf);
     3 
     4 class Solution {
     5 public:
     6     /**
     7      * @param buf Destination buffer
     8      * @param n   Maximum number of characters to read
     9      * @return    The number of characters read
    10      */
    11     int read(char *buf, int n) {
    12         int res = 0, k = 0;
    13         while (n > 0) {
    14             int readN = min(read4(buf + k*4), n);
    15             n = n - 4;
    16             ++k;
    17             res += readN;
    18         }
    19         return res;
    20     }
    21 };
    View Code

     

    [168] Excel Sheet Column Title [Easy] 

    给个数字N,返回对应excel上面的列名。 比如1返回A,2返回B,27返回AA

    26进制换算,有个N--原因需要注意。

     1 class Solution {
     2 public:
     3     string convertToTitle(int n) {
     4         string ans = "";
     5         while(n > 0) {
     6             n--; //在这里N--的原因是A对应1,Z对应26
     7             unsigned int  mod = n % 26;
     8             char  ch = 'A' + mod;
     9             ans = ch + ans;
    10             n = n / 26;
    11         }
    12         return ans ;
    13     }
    14 };
    View Code

     

    [266] Palindrome Permutation [Easy] 

    给个字符串,判断用它的字母能不能组成个回文串,秒杀

     1 class Solution {
     2 public:
     3     bool canPermutePalindrome(string s) {
     4         map<char, int> mapCnt;
     5         for (auto ele : s) {
     6             mapCnt[ele]++;
     7         }
     8         int oddCnt = 0;
     9         for (auto ele : mapCnt) {
    10             if(ele.second % 2) {
    11                 oddCnt++;   
    12                 if (oddCnt >= 2) {
    13                     return false;
    14                 }
    15             }
    16         }
    17         return true;
    18     }
    19 };
    View Code

      

    [500] Keyboard Row [Easy]

    给N个单词,判断哪些单词的字母在键盘的同一行,输出这些单词。

     1 class Solution {
     2 public:
     3     set<char> setLine1{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'}, 
     4             setLine2{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
     5             setLine3{'z', 'x', 'c', 'v', 'b', 'n', 'm'};
     6 
     7     int findGroup(char lowerChar) {
     8         if (setLine1.find(lowerChar) != setLine1.end()) {
     9             return 1;
    10         } else if (setLine2.find(lowerChar) != setLine2.end()) {
    11             return 2;
    12         } else if (setLine3.find(lowerChar) != setLine3.end()){
    13             return 3;
    14         }
    15         return -1;
    16     }
    17     
    18     vector<string> findWords(vector<string>& words) {
    19         vector<string> answer;
    20         for(auto word : words) {
    21             int groupId = 0;
    22             bool ansIn = true;
    23             groupId = isupper(word[0]) ? findGroup(tolower(word[0])) : findGroup(word[0]);
    24             for (auto i = 1; i < word.size(); ++i) {
    25                 int tmpGroupId = 0;
    26                 tmpGroupId = isupper(word[i]) ? findGroup(tolower(word[i])) : findGroup(word[i]);
    27                 if (tmpGroupId != groupId) {
    28                     ansIn = false;
    29                     break;
    30                 }
    31             }
    32             if (ansIn) {
    33                 answer.push_back(word);
    34             }
    35         }
    36         return answer;
    37     }
    38 };
    View Code

     

  • 相关阅读:
    ajax是什么? ajax的交互模型? 同步和异步的区别? 如何解决跨域问题?
    集锦 比较好
    集锦
    伊甸园日历游戏
    晴天小猪历险记之Hill
    求无向图最小环算法
    旅行商简化版
    十字绣
    破坏石油运输系统问题
    强墙
  • 原文地址:https://www.cnblogs.com/zhangwanying/p/9255749.html
Copyright © 2011-2022 走看看