首页 > 其他 > 详细

LeetCode开心刷题四十九天——老子又回来了125. Valid Palindrome136. Single Number

时间:2019-10-15 20:51:04      阅读:78      评论:0      收藏:0      [点我收藏+]
too long to write code,so I choose some easy level,but fortunately I still find some really good jobs!
 
125. Valid Palindrome
Easy

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true

Example 2:

Input: "race a car"
Output: false
#include<string>
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
class Solution {
public:
    bool isPalindrome(string s) {
        int r=s.size()-1;
        int l=0;
        //equal also can judge
        while(l<=r)
        {
            //equal cannot add
            //isalnum judge whether is number or letter
            while(!isalnum(s[l])&&l<r) l++;
            while(!isalnum(s[r])&&l<r) r--;
            //judge
            //toupper change lower case to upper case
            if(toupper(s[l])!=toupper(s[r])) return false;
            l++,r--;
        }
        return true;
    }
};
int main()
{
    Solution s1;
    string s;
    cin>>s;
    cout<<s1.isPalindrome(s)<<endl;
}
136. Single Number
Easy

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4
XOR bit calculation is very good neat code ,this isn‘t quick but less space
#include<string>
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<vector>
using namespace std;
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int a=nums[0];
        for(int i=1;i<nums.size();i++)
        {
            a^=nums[i];
        }
        return a;
    }
};
int main()
{
    Solution s1;
    vector<int> nums {1,2,2,4,1,4};
    cout<<s1.singleNumber(nums)<<endl;
    return 0;
}

 

LeetCode开心刷题四十九天——老子又回来了125. Valid Palindrome136. Single Number

原文:https://www.cnblogs.com/Marigolci/p/11679948.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!