1.两数之和是target
vector<vector<int>> select(vector<int> &nums, int target){
vector<vector<int>> res;
unordered_map<int,int> hash;
for(int i=0;i<nums.size();i++){
int another=target-nums[i];
if(hash.count(another)){
vector<int> tmp{nums[i],another};
res.push_back(tmp);
}
hash[nums[i]]=i;
}
return res;
}
2.单链表判断是否是回文串
bool judge(ListNode head){
ListNode prev = null;
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
ListNode next = slow.next;
slow.next = prev;
prev = slow;
slow = next;
}
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
if (slow.val != prev.val) {
return false;
}
slow = slow.next;
prev = prev.next;
}
return true;
}
https://www.jianshu.com/p/462fa3e4cd43
另一种方法:
bool isPalindrome(ListNode* head) {
vector<int> store;
ListNode* current = head;
while(current != nullptr)
{
store.push_back(current -> val);
current = current -> next;
}
int left=0, right=store.size()-1;
while(left < right)
{
if(store[left] != store[right]) return false;
++ left;
-- right;
}
return true;
}
3.快速排序
void quick_sort(vector<int> &q, int l, int r){
if(left>right)
return;
int left=l-1,right=r+1;
int mid=(left+right)/2;
while(left<right){
do ++left;while(q[left]<q[mid]);
do --right;while(q[right]>q[mid]);
if(left<right)
swap(q[left],q[right]);
}
quick_sort(q,l,left);
quick_sort(q,left+1,r);
}
4.字符串按单词逆序打印
class Solution {
public:
string reverseWords(string s) {
// reverse whole string
reverse(s.begin(), s.end());
// reverse each word
int i = 0,j = 0, storedIndex = 0;
while (i < s.size()){
// put i to the first letter of a word
while (i < s.size() && s[i] == ' ') ++i;
// add a space if this is not the first and last word
if (i < s.size() && storedIndex != 0) s[storedIndex++] = ' ';
j = i;
// put j to the end of the word
while (j < s.size() && s[j] != ' ') ++j;
// reverse this word. There may be spaces in front of the word, and after reverse it goes to the end.
// if s contains only space, i == j, reverse will do nothing
reverse(s.begin()+storedIndex, s.begin()+j);
// put stored index to the end of the word
storedIndex += j - i;
// put i to the start of next search
i = j;
}
// finally, storedIndex will point to the letter next to the last word
s.erase(s.begin()+storedIndex, s.end());
return s;
}
};
5.如何判断链表有环
bool exitLoop(Node *head)
{
Node *fast, *slow ;
slow = fast = head ;
while (slow != NULL && fast -> next != NULL)
{
slow = slow -> next ;
fast = fast -> next -> next ;
if (slow == fast)
return true ;
}
return false ;
}
6.多线程编程,如:
//notify_one()(随机唤醒一个等待的线程)
//notify_all()(唤醒所有等待的线程)
//Create By@herongwei 2019/09/10
#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
using namespace std;
std::mutex data_mutex;//互斥锁
std::condition_variable data_var;//条件变量
bool flag = true;
void printfA() {
int i = 1;
while (i <= 100) {
//休息1秒
//std::this_thread::sleep_for(std::chrono::seconds(1));
std::unique_lock<std::mutex> lck(data_mutex);
data_var.wait(lck, [] {return flag; });//等待flag=true才打印奇数
std::cout << "A " << i << endl;
i += 2;
flag = false;
data_var.notify_one();
}
}
void printfB() {
int i = 2;
while (i <= 100) {
std::unique_lock<std::mutex> lck(data_mutex);
data_var.wait(lck, [] {return !flag; });//等待flag=false才打印偶数
std::cout << "B " << i << endl;
i += 2;
flag = true;
data_var.notify_one();
}
}
int main() {
// freopen("in.txt","r",stdin);
std::thread tA(printfA);
std::thread tB(printfB);
tA.join();
tB.join();
return 0;
}
7.给定一个数组nums,求连续的子数组的和为target的数量
int subarraySum(vector<int>& nums, int target) {
unordered_map<int, int> mp;
int sum=0,ans=0;
mp[0]=1;
for(int i=0;i<nums.size();i++){
sum+=nums[i];
if(mp.count(sum-target))
ans+=mp[sum-target];
++mp[sum];
}
return ans;
}
8.最长连续无重复字符串
int lengthOfLongestSubstring(string s) {
unordered_set<char> S;
int rk=-1,ans=0;
for(int i=0;i<s.length();i++){
if(i>0) S.erase(s[i-1]);
while(rk+1<s.length() && !S.count(s[rk+1])){
S.insert(s[rk+1]);
++rk;
}
ans=std::max(ans,S.size());
}
return ans;
}
9.数组所有数都是两个,只有一个数出现一次,找出这个数
int singleNumber(vector<int>& nums) {
int ans=0;
for(int i=0;i<nums.size();i++)
ans^=nums[i];
return ans;
}
10.链表排序
ListNode* sort(ListNode* head){
ListNode* pre,q=head->next,t;
ListNode* h=new ListNode(-1);
h->next=head;
head->next=NULL:
while(q){
for(pre=h;pre->next && q->val<=pre->next->val; pre=pre->next);
t=q->next;
q->next=pre->next;
pre->next=q;
p->next=t;
}
return h->next;
}
11.实现一个矩阵乘法
在这里插入代码片
12.类的深拷贝和浅拷贝
在这里插入代码片
13.反转单链表
TreeNode* InvertList(TreeNode* head){
TreeNode* pre,phead,temp;
phead = head; //将phead指向链表头,做游标使用
pre = NULL; //pre为头指针之前的节点
while(phead != NULL){
temp = pre;
pre = phead;
phead = phead->next;
pre->next = temp; //pre接到之前的节点
}
return pre;
}