Lectcode手撕算法
两数之和
删除数组中的重复项
移除元素
整数反转
回文数
最长公共前缀
合并两个有序链表
有效括号
- lectcode-1:两数之和:
题目:https://leetcode-cn.com/problems/two-sum/
解答:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int length=nums.size();
int i,j;
for(int i=0;i<length-1;i++)
{
for(int j=i+1;j<length;j++)
{
if(target==nums[i]+nums[j])
return {i,j};
}
}
return {i,j};
}
};
- lectcode-26:删除数组中的重复项
题目:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
解答:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len=nums.size();
if(len==0)
return 0;
int j=0;
for(int i=1;i<len;i++)
{
if(nums[i]!=nums[j])
{
j++;
nums[j]=nums[i];
}
}
return j+1;
}
};
- lectcode-27 移除元素
题目:https://leetcode-cn.com/problems/remove-element/
解答:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int len=nums.size();
if(len==0)
return 0;
int j=0;
for(int i=0;i<len;i++)
{
if(nums[i]!=val)
{
nums[j]=nums[i];
j=j+1;
}
}
return j;
}
};
- lectcode-7:整数反转
题目:https://leetcode-cn.com/problems/reverse-integer/
解答:通过循环将数字x的每一位拆开,在计算新值时每一步都判断是否溢出。
溢出条件有两个,一个是大于整数最大值MAX_VALUE,另一个是小于整数最小值MIN_VALUE,设当前计算结果为ans,下一位为pop。
参考链接:https://leetcode-cn.com/problems/reverse-integer/solution/hua-jie-suan-fa-7-zheng-shu-fan-zhuan-by-guanpengc/
class Solution {
public:
int reverse(int x)
{
long y=0;
int a;
while(x)
{
a=x%10;
x=x/10;
if(y>INT_MAX/10 || y==INT_MAX/10&&a>7)
return 0;
if(y<INT_MIN/10 || y==INT_MIN/10&&a<-8)
return 0;
y=y*10+a;
}
return y;
}
};
- lectcode-9:回文数
题目:https://leetcode-cn.com/problems/palindrome-number/
解答:
class Solution {
public:
bool isPalindrome(int x) {
bool flag=false;
long k=0;
long a=0;
int b=x;
if(x<0)
return flag;
else if(x>=0)
{
while(x != 0)
{
k=x%10;
a=k+a*10;
x=x/10;
}
if(a==b)
flag=true;
}
return flag;
}
};
- lecrcode-14:最长公共前缀
题目:https://leetcode-cn.com/problems/longest-common-prefix/
解答:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans="";
if(strs.size()==0)
return ans;
if(strs.size()==1)
return strs[0];
string min=strs[0];
for(int i=0; i<strs.size();i++)
{
if(strs[i].empty())
{
return ans;
cout<<ans<<endl;
}
}
for(int i=0;i<min.size();++i)
{
//if(min==strs[i])
// return ans;
//char temp=strs[0][i];
for(int j=0;j<strs.size();j++)
{
if(min[i]!=strs[j][i])
return ans;
}
ans=ans+min[i];
}
return ans;
}
};
- lectcode-21:合并两个有序链表
题目:https://leetcode-cn.com/problems/merge-two-sorted-lists/
解答:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummypHead=new ListNode(0);
ListNode* k=dummypHead;
while(l1!=NULL && l2!=NULL)
{
if(l1->val < l2->val)
{
k->next=l1;
k=k->next;
l1=l1->next;
}
else
{
k->next=l2;
k=k->next;
l2=l2->next;
}
}
if (l1!=NULL)
{
k->next=l1;
}
if(l2!=NULL)
{
k->next=l2;
}
return dummypHead->next;
}
};
- lectcode-20:有效括号
题目:https://leetcode-cn.com/problems/valid-parentheses/
解答:hash表,建立map;
class Solution {
public:
bool isValid(string s) {
if(s.length()==0)
return true;
if(s.length()%2!=0)
return false;
map<char,char> map={{')','('},{'}','{'},{']','['}};
stack<char> sp;
for(char& c : s)
{
if(c=='('||c=='{'||c=='[')
{
sp.push(c);
}
if(c==')'||c=='}'||c==']')
if(!sp.empty()&&sp.top()==map[c])
sp.pop();
else
return false;
}
return sp.empty();
}
};