357-C++、Linux必备知识点 1.int a=4,则对于表达式++(a++)的结果为?答案:不是5!编译不通过,a++的结果为4,++4是不正确的,4是右值,如果改成++(b = a++);结果就是5了2.若变量a是int类型,并执行了语句a=’A’+1.6;则正确的结果是?a的值是字符’A’的ASCⅡ值加上13.decltype和auto都可以用来推断类型,但是二者有几处明显的差异1.auto忽略顶层const,decltype保留顶层const2.对引用操作,auto推断出原有类型,decltype推断出引用3.对解引
356-Leetcode 删除链表的倒数第N个节点 下面程序需要对如果删除的是第一个节点要进行判断,所以需要计算整个链表的长度struct ListNode{ int val; ListNode* next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode* next) : val(x), next(next) {}};class Solution{public: int.
355-Leetcode 外观数列 class Solution {public: string countAndSay(int n) { string st = "1"; for (int i = 2; i <= n; ++i) { string cur = ""; int start = 0; int pos = 0; while (pos < st.size()) { while (pos < st.size() && st[pos] == s.
354-Leetcode 四数之和 class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> vec; if (nums.size() < 4) { return vec; } sort(nums.begin(), nums.end()); int length = nums.s.
353-Leetcode 最接近的三数之和 class Solution {public: int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); int n = nums.size(); int best = 1e7; // 根据差值的绝对值来更新答案 auto update = [&](int cur) { if (abs(cur - target) < .
352-Leetcode 三数之和 class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); vector<vector<int>> ans; // 枚举 a for (int first = 0; first < n; ++first) .
351-Leetcode 整数反转 INT_MIN在标准头文件limits.h中定义#define INT_MAX 2147483647#define INT_MIN (-INT_MAX - 1)class Solution{public: int reverse(int x) { int count = 0; while (x != 0) { if (count < INT_MIN / 10 || count > INT_MAX / 10) { return 0; } .
350-Leetcode 括号生成 算法:如果左括号数量小于 n,我们可以放一个左括号。如果右括号数量小于左括号的数量,我们可以放一个右括号class Solution { void backtrack(vector<string>& ans, string& cur, int open, int close, int n) { if (cur.size() == n * 2) { ans.push_back(cur); return; } if (open < n).
347-Leetcode 多数元素 方法一:Boyer-Moore 投票算法如果我们把众数记为 +1,把其他数记为 −1,将它们全部加起来,显然和大于 0,从结果本身我们可以看出众数比其他数多class Solution {public: int majorityElement(vector<int>& nums) { int tmp = -1; int count = 0; for (auto& x : nums) { if (x == tmp) { ++coun.
345-Leetcode 赎金信 如果ransomNote的长度要比magazine要长,那肯定是错误的先遍历magazine,统计每个字符出现的次数,再遍历 ransomNote,每次将数组中对应的字符的个数减一,如果减一以后的值小于0了,说明数组中已经没有这个字符了,那么就是错误的class Solution {public: bool canConstruct(string ransomNote, string magazine) { int len1 = ransomNote.size(); int len2 .
344-Leetcode 二叉树的所有路径 算法:我们也可以用广度优先搜索来实现。我们维护一个队列,存储节点以及根到该节点的路径。一开始这个队列里只有根节点。在每一步迭代中,我们取出队列中的首节点,如果它是叶子节点,则将它对应的路径加入到答案中。如果它不是叶子节点,则将它的所有孩子节点加入到队列的末尾。当队列为空时广度优先搜索结束,我们即能得到答案struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode() : val(0), left(nullptr), r.
343-Leetcode 反转字符串中的元音字母 方法一:双指针class Solution {public: string reverseVowels(string s) { unordered_map<char, int> map { {'a',1}, { 'e',1 }, { 'i',1 }, { 'o',1 }, { 'u',1 }, { 'A',1 }, { 'E',1 }, { 'I',1}, { 'O',1 }, { 'U',1 }, }; .
342-Leetcode 字符串中的第一个唯一字符 方法一:使用哈希表存储频数class Solution {public: int firstUniqChar(string s) { unordered_map<char, int> map; for (auto& ch : s) { ++map[ch]; } int len = s.size(); for (int i = 0; i < len; ++i) { if (map[s[i]] == 1) { retu.
341-Linux 连接数据库 1.service mysql status 可以查看服务器(数据库)是否启动数据库在开机默认情况下就是启动的启动数据库:service mysql start重新启动数据库:service mysql restart停止数据库:service mysql stop本地连接数据库:mysql -uroot -p密码远程连接数据库:mysql -uroot -h192.168.0.0 -p密码2.show databases;可以显示都有哪些数据库3.root和stu2就是用户名,%表示远程登
340-Leetcode 有效的字母异位词 方法一:排序class Solution {public: bool isAnagram(string s, string t) { int len1 = s.size(); int len2 = t.size(); if (len1 != len2)return false; sort(s.begin(), s.end()); sort(t.begin(), t.end()); return s == t; }};int main(){ Solution A.
339-Leetcode 单词规律 class Solution {public: bool wordPattern(string pattern, string s) { unordered_map<char, string> ch2str; unordered_map<string, char> str2ch; int len = s.size(); int i = 0; for (auto ch : pattern) { if (i > len) { .
338-Leetcode 同构字符串 class Solution{public: bool isIsomorphic(string s, string t) { unordered_map<char, char> st; unordered_map<char, char> ts; int len = s.size(); for (int i = 0; i < len; ++i) { char x = s[i], y = t[i]; if ((st.count(x) &am.
337-Leetcode 电话号码的组合 方法一:回溯首先使用哈希表存储每个数字对应的所有可能的字母,然后进行回溯操作回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是不完整的)。该字符串初始为空。每次取电话号码的一位数字,从哈希表中获得该数字对应的所有可能的字母,并将其中的一个字母插入到已有的字母排列后面,然后继续处理电话号码的后一位数字,直到处理完电话号码中的所有数字,即得到一个完整的字母排列。然后进行回退操作,遍历其余的字母排列回溯算法用于寻找所有的可行解,如果发现一个解不可行,则会舍弃不.
336-Leetcode 整数转罗马数字 class Solution{public: string intToRoman(int num) { string s; while (num != 0) { while (num >= 1000) { s += 'M'; num -= 1000; } while (num >= 900) { s += 'C'; s += 'M'; num -= 900; } while (num >.