8.19 汇总

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在CentOS 7上安装MySQL 8.19,您可以按照以下步骤操作: 1. 首先,您需要下载MySQL 8.19的压缩包,并将其解压缩到/usr/local/目录下。您可以使用以下命令将解压得到的文件移动到/usr/local/目录,并重新命名为mysql: ``` mv mysql-8.0.19-linux-glibc2.12-x86_64 /usr/local/mysql ``` 引用 2. 接下来,您需要修改/usr/local/mysql/目录的权限,以确保MySQL能够正常运行。您可以使用以下命令更改目录的所有者和组为mysql: ``` chown -R mysql.mysql /usr/local/mysql/ ``` 引用 3. 之后,您可以将MySQL的启动脚本复制到/etc/init.d/目录中,以便MySQL可以作为服务启动和停止。您可以使用以下命令执行此操作: ``` cp ../support-files/mysql.server /etc/init.d/mysqld ``` 引用 完成上述步骤后,您就成功安装了MySQL 8.19。您可以使用适当的命令启动、停止和管理MySQL服务。请记住,这只是安装MySQL的基本步骤,您可能需要根据您的具体需进行其他配置和设置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [centos7安装MySQL Community Server 8.0.19](https://blog.csdn.net/resuper0/article/details/104442933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值