1 . 回文数
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
if(x==0)
return true;
queue<int> a;
long x1=x;
int i;
long re=0;
while(x1!=0)
{
i=x1%10;
a.push(i);
x1=x1/10;
}
while(!a.empty())
{
re=a.front()+re*10;
a.pop();
}
return x==re ? true : false;
}
};
2. 盛最多水的容器
//超出时间限制
class Solution {
public:
int maxArea(vector<int>& height) {
if(height.empty())
return 0;
int size=height.size();
int max=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(height[i]>=height[j])
{
max= ((j-i)*height[j])>=max ? ((j-i)*height[j]) : max;
}
else
max= ((j-i)*height[i])>=max ? ((j-i)*height[i]) : max;
}
}
return max;
}
};
class Solution {
public:
int maxArea(vector<int>& height) {
if(height.empty())
return 0;
int size=height.size();
int i=0;
int j=size-1;
int max=0;
while(j>=i)
{
if(height[i]>=height[j])
{
max= ((j-i)*height[j])>max ? ((j-i)*height[j]) : max;
j--;
}
else
{
max= ((j-i)*height[i])>max ? ((j-i)*height[i]) : max;
i++;
}
}
return max;
}
};
3.整数转罗马数字
class Solution {
public:
string intToRoman(int num) {
if(num==0 || num>3999)
return "error";
string str[][9]={{"I","II","III","IV","V","VI","VII","VIII","IX"},
{"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"M","MM","MMM"}};
int i=0;
string result;
int a;
while(num!=0)
{
if(num%10)
{
a=num%10;
result=str[i][a-1]+result;
}
i++;
num=num/10;
}
return result;
}
};
4.罗马数字转整数
class Solution {
public:
int romanToInt(string s) {
if(s.empty())
return 0;
unordered_map<string, int> hash= {{"I", 1}, {"IV", 4}, {"IX", 9}, {"V", 5}, {"X", 10}, {"XL", 40}, {"XC", 90}, {"L", 50}, {"C", 100}, {"CD", 400}, {"CM", 900}, {"D", 500}, {"M", 1000}};
int result=0;
for(int i=0;i<s.size();i++)
{
int one=hash[s.substr(i,1)];
int two=hash[s.substr(i,2)];
int p= 1 == 1+two ? one:two;
if(two)
i++;
result=result+p;
}
return result;
}
};
5.最长公共前缀
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty())
return "";
if (strs.size() == 1)
return strs[0];
string result = "";
int size = strs.size();
int min = strs[0].size();
int k = 0;
for (int i = 0; i < size; i++)
{
min = strs[i].size() > min ? min : strs[i].size();
}
int j = 0;
for (; j < min; j++)
{
for (int k = 0; k < size - 1; k++)
{
if (strs[k][j] == strs[k + 1][j])
continue;
else
goto label;
}
}
label: return strs[0].substr(0, j);
}
};
6.最接近的三数之和
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int size=nums.size();
int result= nums[0]+nums[1]+nums[2];
int gap=abs(nums[0]+nums[1]+nums[2]-target);
for(int i=0;i<size-2;i++)
{
int left=i+1;
int right=size-1;
while(right>left)
{
if(nums[left]+nums[i]+nums[right]-target>0)
{
result=abs(nums[left]+nums[i]+nums[right]-target) < abs(result-target) ? nums[left]+nums[i] +nums[right] : result;
right--;
}
else if(nums[left]+nums[i]+nums[right]-target<0)
{
result=abs(nums[left]+nums[i]+nums[right]-target) < abs(result-target) ? nums[left]+nums[i] +nums[right] : result;
left++;
}
else
return target;
}
}
return result;
}
7 四数之和
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result={};
if(nums.size()<4)
return result;
int size=nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<size-3;i++)
{
for(int j=i+1;j<size-2;j++)
{
int first=j+1;;
int second=size-1;
while(first<second)
{
if(nums[i]+nums[j]+nums[first]+nums[second]<target )
first++;
else if(nums[i]+nums[j]+nums[first]+nums[second]>target )
second--;
else
{
vector<int> tem;
tem.push_back(nums[i]);
tem.push_back(nums[j]);
tem.push_back(nums[first]);
tem.push_back(pnums[second]);
result.push_back(tem);
second--;
}
}
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(),result.end()),result.end());
return result;
}
8.电话号码的字母组合
vector<string> result={};
unordered_map<int, string> dig = { {2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
void auxletter(string digits,string str,int cen)
{
if(cen==digits.size())
{
result.push_back(str);
return;
}
int k=int(digits[cen])-'0';
string tem=dig[k];
for(int i=0;i<tem.size();i++)
auxletter(digits,str+tem[i],cen+1);
}
vector<string> letterCombinations(string digits) {
if(digits.empty())
return result;
auxletter(digits,{},0);
return result;
}
9.删除链表的倒数第N个节点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* result=NULL;
if(head==NULL || n<0)
return result;
ListNode* first=head;
ListNode* second=head;
ListNode* pre=NULL;
if(head->next==NULL&&n==1)
return NULL;
while(n-1)
{
second=second->next;
n--;
}
while(second->next!=NULL)
{
pre=first;
first=first->next;
second=second->next;
}
if(first==second)
{
ListNode* tem=first;
delete tem;
pre->next=NULL;
result=head;
}
if(first!=second)
{
if(first==head)
{
ListNode* tem=first;
result=tem->next;
delete tem;
}
else
{
ListNode* tem=first;
pre->next=tem->next;
delete tem;
result=head;
}
}
return result;
}
};
10.有效的括号
class Solution {
public:
bool isValid(string s) {
if(s=="")
return true;
if (s.size() < 2)
return false;
int size = s.size();
vector<int> a;
for (int i=0; i < size; i++)
a.push_back(s[i] - 0);
int k = 0;
while (k < a.size())
{
if (a[k] == a[k + 1] - 1 || a[k] == a[k + 1] - 2)
{
if (a.empty())
break;
a.erase(a.begin() + k, a.begin() + k + 2);
k = 0;
}
else {
k++;
if (k ==a.size() - 1)
break;
}
}
if (a.empty())
return true;
else
return false;
}
};