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; }
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