leetcode
youfuwenn
所有内容的主要目的是自我学习
展开
-
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 3
class Solution {public: int lengthOfLongestSubstring(string s) { if (s.size() == 0) { return 0; } int res = 0;//记录结果 unordered_set<char> temp;//用来存储字符 int i = 0; int k = 0;//s的下标 int len = s.size(); for (i = 0; i<len; i++)/.原创 2021-12-03 14:12:34 · 468 阅读 · 0 评论 -
给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。49
class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { //这个问题实际上是给一个字符串数组然后叫你把他按一定的规则把这个数组分组。 unordered_map<string, vector<string>> mp; for (string& str : strs) { string key.原创 2021-11-25 15:29:07 · 975 阅读 · 0 评论 -
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。27
class Solution {public: int removeElement(vector<int>& nums, int val) { if (nums.size() == 0){ //nums不是指针,不能写nums == NULL return 0; } int l = 0; int r = nums.size() - 1; while (l<r)//l等于r不用交换。l>r说明已经遍历完了,l前面的都不为val,r后面的都为va.原创 2021-11-24 22:44:39 · 531 阅读 · 0 评论 -
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 24题
递归class Solution {public: ListNode* swapPairs(ListNode* head) { //使用了递归,每次做了两个结点的交换并且连接下一个头结点。 if(head == nullptr || head->next == nullptr) return head; ListNode* next = head->next; head->next = swapPa原创 2021-11-24 21:51:43 · 621 阅读 · 0 评论 -
给定一个非负整数 x ,计算并返回 x 的平方根,即实现 int sqrt(int x) 函数。正数的平方根有两个,只输出其中的正数平方根。如果平方根不是整数,输出只保留整数的部分,小数部分将。072
class Solution {public: int mySqrt(int x) { if(x == 0) return 0; int left = 1; int right = x/2+1;//一个数的平方根的范围是在1到x/2之间。 int index = -1; int mid = 0; while(left<=right)//返回的是整数 { .原创 2021-11-24 15:11:22 · 951 阅读 · 0 评论 -
给你一个整数数组 nums ,请计算数组的 中心下标 。数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。724题
class Solution {public: int pivotIndex(vector<int>& nums) { int total = 0; int i = 0; int sum = accumulate(nums.begin(), nums.end(), 0); for (i = 0; i<nums.size(); i++) { total += nums[i]; if (total == sum) { return i.原创 2021-11-23 22:57:20 · 404 阅读 · 0 评论 -
街上有 n 栋房子整齐地排成一列,每栋房子都粉刷上了漂亮的颜色。给你一个下标从 0 开始且长度为 n 的整数数组 colors ,其中 colors[i] 表示第 i 栋房子的颜色。2078.
class Solution {public: int maxDistance(vector<int>& colors) { int i = 0; int j = 0; int res = 0; for (i = 0; i<colors.size(); i++) { for (j = i + 1; j<colors.size(); j++) { if (colors[i] != colors[j]) { res .原创 2021-11-23 15:04:09 · 678 阅读 · 0 评论 -
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。206.反转链表。
1 递归法class Solution {public: ListNode* reverseList(ListNode* head) { //递归用于可以把大问题化成小问题的问题,并且大小问题的性质相同,不断重复的问题。 //为什么要从后面开始反转?若从后面开始反转在前面的结点是可以记住后面的。 if(head == nullptr||head->next == nullptr) { return head;原创 2021-11-23 09:49:15 · 1575 阅读 · 0 评论 -
547.省份数量
void dfs(int i, bool* visted, vector<vector<int> >& isConnected, int len){ visted[i] = true; int j = 0; for (j = 0; j<len; j++) { if (isConnected[i][j] == 1 && visted[j] == false)//如果有关联并且不属于一个省份。 { dfs(j, visted, is.原创 2021-11-22 21:28:31 · 318 阅读 · 0 评论 -
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 合并两个有序链表。
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == nullptr) return l2; if (l2 == nullptr) return l1; if (l1->val <= l2->val) { l1->next = mergeTwoLists(l1->next, l2); return l1; }.原创 2021-11-21 11:21:51 · 793 阅读 · 0 评论 -
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。两数之和。
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int>B(2,-1); map<int,int>A; int i = 0; for(i = 0;i<nums.size();i++) A.insert(pair<int,int>(nums[i],i)); fo.原创 2021-11-21 08:16:12 · 951 阅读 · 0 评论 -
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。两数相加。
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: ListNode* addTwoNumbers(ListNode* l1, Lis.原创 2021-11-20 17:51:17 · 1260 阅读 · 0 评论 -
给定一个只包括 ‘(‘,‘)‘,‘{‘,‘}‘,‘[‘,‘]‘ 的字符串 s ,判断字符串是否有效。有效括号。
class Solution {public: bool isValid(string s) { stack<char> st;//为什么要使用栈?栈是先进后出的,一一对应,与括号的匹配类似 unordered_map<char, int>m{ { '(', 1 }, { '{', 2 }, { '[', 3 }, { ')', 4 }, { '}', 5 }, { ']', 6 } };//用无序的map,查找效率更高。若不用map,一个一个来检查是什么符号的话 .原创 2021-11-20 17:34:53 · 342 阅读 · 0 评论