zoukankan      html  css  js  c++  java
  • leetcode 一个算法面试题相关的网站

      去年就在某牛人的博客上见到了,现在准备去玩玩。做题的方式跟Topcoder类似,刚刚去水了一题"Valid Palindrome"

     1 class Solution {
     2 public:
     3     bool isPalindrome(string s) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         string alnumstr;
     7         transform(s.begin(), s.end(), s.begin(), [](char& ch){ return ::toupper(ch);});
     8         copy_if(s.begin(), s.end(), back_inserter(alnumstr), [](char& ch){ return ::isalnum(ch);});
     9         
    10         string another;
    11         reverse_copy(alnumstr.begin(), alnumstr.end(), back_inserter(another));
    12         
    13         return another == alnumstr;
    14         
    15     }
    16 };
     1 class Solution {
     2 public:
     3     bool isPalindrome(string s) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6 
     7         const char* str = s.c_str();
     8         for (int i = 0, j = strlen(str); i < j; )
     9         {
    10             if (isalnum(str[i]) && isalnum(str[j]))
    11             {
    12                 if (toupper(str[i]) != toupper(str[j]))
    13                 {
    14                     return false;
    15                 }
    16                 else
    17                 {
    18                     ++i;
    19                     --j;
    20                 }
    21             }
    22             if (!isalnum(str[i])) ++i;
    23             if (!isalnum(str[j])) --j;
    24         }
    25         return true;
    26         
    27     }
    28 };
  • 相关阅读:
    poj 3278 catch that cow
    POJ 1028 Web Navigation
    poj 2643 election
    hdu 1908 double queues
    hdu_2669 Romantic(扩展欧几里得)
    0/1背包 dp学习~6
    校验码
    最长上升子序列(LIS经典变型) dp学习~5
    LCS最长公共子序列~dp学习~4
    最长上升子序列(LIS) dp学习~3
  • 原文地址:https://www.cnblogs.com/invisible/p/2950409.html
Copyright © 2011-2022 走看看