LeetCode——字节跳动系列题目

转至:https://blog.csdn.net/Uupton/article/details/84640146

今天登陆leetcode发现探索区多了字节跳动的专栏,特意用了一下午去刷,有些是之前刷过的。但题目不错,就当是复习一遍吧,这里记录一下我会的以及自己觉得不错的题目。

原题链接请点击题目

一:挑战字符串

3. 无重复字符的最长子串

分析:这题要求连续的不重复的最长子序列的长度,注意这里是需要连续,利用这个特性,我们可以维护一个窗口,窗口装的是无重复的字符串,一开始窗口的左边在起点(即下标为0处)。我们用 i 遍历字符串,如果当前字符没有出现过或者当前字符虽然出现过但不在当前的窗口,则窗口向右扩张。而当当前字符出现在窗口内,则窗口的左边收缩到当前字符前一个出现的位置。详见代码


 
 
  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. //维护一个滑动窗口,最大的窗口大小即为结果
  5. int hash[ 128] = { 0};
  6. int left = 0,res = 0;
  7. for( int i = 0;i<s.size();i++)
  8. {
  9. if(hash[s[i]] == 0||hash[s[i]]<left) //1:没出现过 2:出现过但没在窗口内
  10. res = max(res,i-left+ 1); //不断更新res值
  11. else
  12. left = hash[s[i]]; //窗口内出现重复,缩小左边窗口
  13. hash[s[i]] = i+ 1;
  14. }
  15. return res;
  16. }
  17. };

14. 最长公共前缀

简单题,以第一个字符串作为模版,逐个拿出模版的每个字符,然后其余的字符串也逐个拿出相对应位置的字符比较是否相同。注意字符串长度的问题即可。


 
 
  1. class Solution {
  2. public:
  3. string longestCommonPrefix(vector<string>& strs) {
  4. if(strs.empty()) return "";
  5. string res = "";
  6. for( int i = 0;i<strs[ 0].size();i++)
  7. {
  8. char c = strs[ 0][i]; //逐个拿出模版字符串的字符
  9. for( int j = 1;j<strs.size();j++) //后面的字符串
  10. {
  11. if(i>=strs[j].size()||strs[j][i]!=c) //当i已经超过字符串的长度或者字符不相同时直接返回
  12. return res;
  13. }
  14. res+=c;
  15. }
  16. return res;
  17. }
  18. };

567. 字符串的排列

分析:对s2全排再一一跟s1对比肯定会超时。所以我们可以维护两个窗口,一个窗口装s1,另一个窗口装s2。假设s1长度为len1,s2长度为len2。开始先分别装s1和s2的前lne1个字符进各自的窗口。如果此时两个窗口相等则直接返回true,如果不等则s2的窗口从len1开始装s2的字符,同时窗口的左边要删除一个元素,因为两个窗口要保持大小,期间如果两个窗口相等则返回true


 
 
  1. class Solution {
  2. public:
  3. bool checkInclusion(string s1, string s2) {
  4. //v1、v2维护一个大小相同的窗口,先计算出len1前的字符出现的次数,如果相等直接返回TURE,如果不等则操作v2继续往后走,后面的字符添上,窗口左边的字符删除
  5. int len1 = s1.size(),len2 = s2.size();
  6. vector< int> v1( 128, 0),v2( 128, 0);
  7. for( int i = 0;i<len1;i++)
  8. {
  9. v1[s1[i]]++;
  10. v2[s2[i]]++;
  11. }
  12. if(v1==v2) return true;
  13. for( int i = len1;i<len2;i++) //v2从len1位置开始装
  14. {
  15. v2[s2[i]]++; //装新的字符
  16. v2[s2[i-len1]]--; //删除早装入的字符
  17. if(v1 == v2)
  18. return true;
  19. }
  20. return false;
  21. }
  22. };

43. 字符串相乘

分析:这里主要开了三个数组来做,一个数组存第一个字符串,另一个数组存第二个字符串,最后一个数组存结果。保存两个字符串的时候要反着顺序存,因为我们平时做乘法的时候也是从数字的最后一位向前乘的,所以其实这道题主要是模拟了平时在纸上做的乘法。


 
 
  1. class Solution {
  2. public:
  3. string multiply(string num1, string num2) {
  4. int x[ 120] = { 0},y[ 120] = { 0},z[ 250] = { 0};
  5. int len1 = num1.size(),len2 = num2.size();
  6. for( int i = len1 -1,k = 0;i>= 0;i--)
  7. x[k++] = num1[i]- '0';
  8. for( int i = len2 -1,k = 0;i>= 0;i--)
  9. y[k++] = num2[i]- '0';
  10. for( int i = 0;i<len1;i++) //在这里进行相乘,但没进位
  11. {
  12. for( int j = 0;j<len2;j++)
  13. z[i+j] += (x[i]*y[j]);
  14. }
  15. for( int i = 0;i< 249;i++) //现在进位
  16. {
  17. if(z[i]> 9)
  18. {
  19. z[i+ 1] += z[i]/ 10;
  20. z[i]%= 10;
  21. }
  22. }
  23. int i;
  24. for(i = 249;i>= 0;i--)
  25. if(z[i] != 0)
  26. break;
  27. string res = "";
  28. for(;i>= 0;i--)
  29. res+=(z[i]+ '0');
  30. if(res == "") return "0";
  31. return res;
  32. }
  33. };

 

151. 翻转字符串里的单词

分析:主要做法就是先把整个字符串反转,然后开始遍历字符串,每遍历完一个单词(注意不是一个字符)的时候将这个单词再反转。


 
 
  1. class Solution {
  2. public:
  3. void reverseWords(string &s) {
  4. int index = 0,n = s.size();
  5. reverse(s.begin(),s.end()); //反转整个字符串
  6. for( int i = 0;i<n;i++)
  7. {
  8. if(s[i]!= ' ') //遇到非空格的字符
  9. {
  10. if(index!= 0)
  11. s[index++] = ' ';
  12. int j = i; //令j = i 进行下面的操作
  13. while(j<n&&s[j]!= ' ') //遍历完整一个单词
  14. s[index++] = s[j++];
  15. reverse(s.begin()+index-(j-i),s.begin()+index); //对刚才遍历的单词进行反转
  16. i = j;
  17. }
  18. }
  19. s.resize(index);
  20. }
  21. };

93. 复原IP地址

分析:一般题目问字符串有多少种可能的排列,十有八九都是用递归做的。我们需要先写一个函数判断一个字符串是否符合ip其中一个结点,符合的标准:1、1到3位长度的字符串。2、长度大于一的话首位不能为0。3、整数大小要在0~255的范围内。

接着就可以递归做正式工作。这里对字符串分别截取一位、二位、三位。。。判断是否能构成ip的一个结点,如果能的话就截断这部分,让剩余的部分递归下去继续做判断。

具体看代码


 
 
  1. class Solution {
  2. public:
  3. vector< string> restoreIpAddresses( string s) {
  4. vector< string> res;
  5. // string out = "";
  6. helper(res,s, "", 4);
  7. return res;
  8. }
  9. void helper(vector<string>& res,string s,string out,int k)
  10. {
  11. if(k== 0)
  12. {
  13. if(s.empty()) //注意点一,原字符串s应该要为空了
  14. res.push_back(out);
  15. }
  16. else
  17. {
  18. for( int i = 1;i<= 3;i++)
  19. {
  20. //截取某部分进行判断,如果合法则进入下一个递归
  21. if(s.size()>=i&&isValid(s.substr( 0,i))) //注意点二,越界判断
  22. {
  23. if(k== 1) //k==1代表当前ip再添加多一个结点就够四个了
  24. helper(res,s.substr(i),out+s.substr( 0,i),k -1);
  25. else
  26. helper(res,s.substr(i),out+s.substr( 0,i)+ '.',k -1);
  27. }
  28. }
  29. }
  30. }
  31. //判断是否合法
  32. bool isValid(string s)
  33. {
  34. if(s.empty()||s.size()> 3||(s.size()> 1&&s[ 0]== '0'))
  35. return false;
  36. int num = atoi(s.c_str());
  37. return num>= 0&&num<= 255;
  38. }
  39. };

二、数组与排序

15. 三数之和


 
 
  1. class Solution {
  2. public:
  3. vector< vector< int>> threeSum( vector< int>& nums) {
  4. vector< vector< int>> res;
  5. sort(nums.begin(),nums.end());
  6. for( int i = 0;i<nums.size();i++)
  7. {
  8. if(nums[i]> 0) break;
  9. if(i> 0&&nums[i] == nums[i -1])
  10. continue;
  11. int target = 0-nums[i];
  12. int j = i+ 1,k = nums.size() -1;
  13. while(j<k)
  14. {
  15. if(nums[j]+nums[k] == target)
  16. {
  17. res.push_back({nums[i],nums[j],nums[k]});
  18. while(j<k&&nums[j+ 1] == nums[j]) j++;
  19. while(j<k&&nums[k -1] == nums[k]) k--;
  20. j++;k--;
  21. }
  22. else if(nums[j]+nums[k]<target)
  23. j++;
  24. else
  25. k--;
  26. }
  27. }
  28. return res;
  29. }
  30. };

 

674. 最长连续递增序列


 
 
  1. class Solution {
  2. public:
  3. int findLengthOfLCIS(vector<int>& nums) {
  4. int res = 0,cnt = 0;
  5. int cur = INT_MAX;
  6. for( int num:nums)
  7. {
  8. if(num>cur)
  9. cnt++;
  10. else cnt = 1;
  11. res = max(res,cnt);
  12. cur = num;
  13. }
  14. return res;
  15. }
  16. };

 

三、链表与树

2. 两数相加


 
 
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  12. int add = 0;
  13. ListNode* res = new ListNode( 0);
  14. ListNode* head = res;
  15. while(l1||l2||add)
  16. {
  17. int num = (l1?l1->val: 0)+(l2?l2->val: 0)+add;
  18. head->next = new ListNode(num% 10);
  19. head = head->next;
  20. add = num/ 10;
  21. l1 = l1?l1->next:l1;
  22. l2 = l2?l2->next:l2;
  23. }
  24. return res->next;
  25. }
  26. };

 

148. 排序链表


 
 
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* sortList(ListNode* head) {
  12. if(!head||!head->next) return head;
  13. ListNode* slow = head,*fast = head,*pre = head;
  14. while(fast&&fast->next)
  15. {
  16. pre = slow;
  17. slow = slow->next;
  18. fast = fast->next->next;
  19. }
  20. pre->next = NULL;
  21. return merge(sortList(head),sortList(slow));
  22. }
  23. ListNode* merge(ListNode* a,ListNode* b)
  24. {
  25. ListNode* head = new ListNode( 0);
  26. ListNode* node = head;
  27. while(a&&b)
  28. {
  29. if(a->val<b->val)
  30. {
  31. node->next = a;
  32. a = a->next;
  33. }
  34. else
  35. {
  36. node->next = b;
  37. b = b->next;
  38. }
  39. node = node->next;
  40. }
  41. if(a) node->next = a;
  42. if(b) node->next = b;
  43. return head->next;
  44. }
  45. };

 

142. 环形链表 II


 
 
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *detectCycle(ListNode *head) {
  12. ListNode* fast = head;
  13. ListNode* slow = head;
  14. while(fast&&fast->next)
  15. {
  16. slow = slow->next;
  17. fast = fast->next->next;
  18. if(slow == fast)
  19. break;
  20. }
  21. if(!fast||!fast->next) return NULL;
  22. slow = head;
  23. while(fast != slow)
  24. {
  25. slow = slow->next;
  26. fast = fast->next;
  27. }
  28. return fast;
  29. }
  30. };

 

236. 二叉树的最近公共祖先


 
 
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  13. if(!root||p==root||q==root) return root;
  14. TreeNode* left = lowestCommonAncestor(root->left,p,q);
  15. TreeNode* right = lowestCommonAncestor(root->right,p,q);
  16. if(left&&right) //p和q分别位于左右子树中
  17. return root;
  18. return left?left:right;
  19. }
  20. };

 

四、动态或贪心

121. 买卖股票的最佳时机


 
 
  1. class Solution {
  2. public:
  3. int maxProfit(vector<int>& prices) {
  4. int res = 0,buy = INT_MAX;
  5. for( auto c:prices)
  6. {
  7. buy = min(buy,c);
  8. res = max(res,c-buy);
  9. }
  10. return res;
  11. }
  12. };

 

122. 买卖股票的最佳时机 II


 
 
  1. class Solution {
  2. public:
  3. int maxProfit(vector<int>& prices) {
  4. int len = prices.size();
  5. if(len<= 1)
  6. return 0;
  7. vector< int> have(len);
  8. vector< int> unhave(len);
  9. have[ 0] = -prices[ 0];
  10. int res = 0;
  11. for( int i = 1;i<len;i++)
  12. {
  13. unhave[i] = max(unhave[i -1],have[i -1]+prices[i]);
  14. have[i] = max(have[i -1],unhave[i -1]-prices[i]);
  15. res = max(res,unhave[i]);
  16. }
  17. return res;
  18. }
  19. };

 

221. 最大正方形


 
 
  1. class Solution {
  2. public:
  3. int maximalSquare(vector<vector<char>>& matrix) {
  4. if(matrix.empty()||matrix[ 0].empty()) return 0;
  5. int m = matrix.size(),n = matrix[ 0].size();
  6. vector< vector< int>> dp(m, vector< int>(n, 0));
  7. int res = 0;
  8. for( int i = 0;i<m;i++)
  9. {
  10. for( int j = 0;j<n;j++)
  11. {
  12. if(i == 0||j == 0)
  13. dp[i][j] = matrix[i][j]- '0';
  14. else if(matrix[i][j] == '1')
  15. dp[i][j] = min(min(dp[i -1][j -1],dp[i -1][j]),dp[i][j -1])+ 1;
  16. res = max(res,dp[i][j]);
  17. }
  18. }
  19. return res*res;
  20. }
  21. };

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值