1.给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
#include <bits/stdc++.h>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int x)
{
val=x;
next=NULL;
}
};
ListNode* cycle(ListNode* head)
{
ListNode* fast=head;
ListNode* slow=head;
while(fast!=NULL&&fast->next!=NULL)
{
fast=fast->next->next;
slow=slow->next;
if(fast==slow)
{
ListNode* index1=fast;
ListNode* index2=head;
while(index1!=index2)
{
index1=index1->next;
index2=index2->next;
}
return index1;
}
}
return NULL;
}
int main()
{
ListNode* head=new ListNode(23);
head->next=new ListNode(78);
head->next->next=new ListNode(89);
head->next->next->next=new ListNode(12);
ListNode* temp= head->next->next->next;
temp->next=new ListNode(90);
temp->next->next=new ListNode(45);
temp->next->next->next=new ListNode(56);
temp->next->next->next->next=new ListNode(80);
temp->next->next->next->next->next=temp;
cout<<cycle(head)->val;
return 0;
}
思路:使用双指针法,定义fast指针和slow指针,fast每次移动2个位置,slow每次移动1个位置,在while循环下,要求fast!=NULL&&fast->next!=NULL,直到fast与slow相遇,设index1指针指向此时的位置,设index2指针指向head节点。index1和index2同时出发遍历,每次移动1个位置,直到两者相遇,相遇的位置节点即为环形入口点。
2.给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
#include <bits/stdc++.h>
using namespace std;
bool isAnagram(string s,string t)
{
int record[26]={0};
for(int i=0;i<s.size();i++)
{
record[s[i]-'a']++;
}
for(int i=0;i<t.size();i++)
{
record[t[i]-'a']--;
}
for(int i=0;i<26;i++)
{
if(record[i]!=0)
{
return false;
}
}
return true;
}
int main()
{
string str1="abcd";
string str2="dcab";
if(isAnagram(str1,str2))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
return 0;
}
使用数组实现,实际上数组就是哈希表。思路:根据已给的2个字符串,先创建一个数组能够存下26个字母,接着开始遍历第一个字符串,该字符串中有的字母会在数组对应字母下标下进行标记(进行加一操作),遍历结束后,继续遍历第二个字符串,方法类似,操作变为减一,最终遍历改变后的数组,如果发现某些下标值不为零,那么证明这两个字符串的字母不不全一致,如果均为零,则字母均一致。
3.给定两个数组,编写一个函数来计算它们的交集。
数组方法:
#include <bits/stdc++.h>
using namespace std;
vector<int> intersection(vector<int> &nums1,vector<int> &nums2)
{
unordered_set<int> result_set;
int hash[1005]={0};
for(int num:nums1)
{
hash[num]=1;
}
for(int num:nums2)
{
if(hash[num]==1)
{
result_set.insert(num);
}
}
return vector<int>(result_set.begin(),result_set.end());
}
int main()
{
vector<int> str1={45,33,2,1};
vector<int> str2={77,2,1,49};
vector<int> result=intersection(str1,str2);
for(int num:result)
{
cout<<num<<" ";
}
return 0;
}
思想:给定的2个数组,对于第一个数组,先进行遍历,出现了哪些元素,就在这些元素对应数组下标的值设为1,然后遍历第二个数组,如果发现在遍历过程中发现了有些下标位置的值等于1,那么说明这些元素与第一个数组中的某些一致,输出即可。这些元素便是交集元素。
这里undordered_set就是为了去重。
set方法:
#include <bits/stdc++.h>
using namespace std;
vector<int> intersection(vector<int> &nums1,vector<int> &nums2)
{
unordered_set<int> result_set;
unordered_set<int> nums_set(nums1.begin(),nums1.end());
for(int num:nums2)
{
if(nums_set.find(num)!=nums_set.end())
{
result_set.insert(num);
}
}
return vector<int> (result_set.begin(),result_set.end());
}
int main()
{
vector<int> str1={1,2,45,32};
vector<int> str2={56,45,3,2};
vector<int> result=intersection(str1,str2);
for(int num:result)
{
cout<<num<<" ";
}
return 0;
}
思想:使用unordered_set,完成去重,创建一个新的哈希表 result_set,用于存放交集元素。将数组nums1转化为哈希表即nums_set,然后在该哈希表里面遍历寻找是否含有与数组nums2相同的元素,如果此存在就将其放到哈希表result_set中,最后将哈希表result_set转化为数组输出,输出的元素即为交集元素。