
LeetCode
jwy2014
这个作者很懒,什么都没留下…
展开
-
841. 钥匙和房间
示例1就可以访问所有的房间,因为通过房间里的key将房间连在了一起。示例2中,就不能访问所有房间,从图中就可以看出,房间2是一个孤岛,我们从0出发,无论怎么遍历,都访问不到房间2。认清本质问题之后,就知道孤岛问题,使用 广度优先搜索(bfs) 还是 深度优先搜索(dfs) 都是可以的。代码如下:## BFS C++代码class Solution {bool bfs(const vector<vector<int>>& rooms) { v...原创 2021-03-28 19:54:00 · 131 阅读 · 0 评论 -
滑动窗口的最值
问题描述#给定一个大小为n≤106的数组。有一个大小为k的滑动窗口,它从数组的最左边移动到最右边。您只能在窗口中看到k个数字。每次滑动窗口向右移动一个位置。以下是一个例子:该数组为[1 3 -1 -3 5 3 6 7],k为3。窗口位置 最小值 最大值 [1 3 -1] -3 5 3 6 7 -1 -3 1 [3 -1 -3] 5 ...原创 2019-12-29 19:58:24 · 231 阅读 · 0 评论 -
二叉树的所有路径
class Solution {public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if(root==nullptr) return res; binaryTreePaths(root,re...原创 2019-11-10 22:34:59 · 93 阅读 · 0 评论 -
bubble_sort
// 你必须定义一个 `main()` 函数入口。#include <vector>#include <iostream>using namespace std;void buble_sort(vector<int>& vec){ int len=vec.size(); bool flag=true; int j; for(in...原创 2019-11-06 22:41:39 · 165 阅读 · 0 评论 -
77组合
给定两个整数n和k,返回 1 ...n中所有可能的k个数的组合。示例:输入:n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]最容易想到的应该是回溯,更多题目思路及代码见:https://github.com/CallmeZhouxiaolun/leetcode/tree...原创 2019-05-26 09:56:24 · 13699 阅读 · 0 评论 -
回文数长度
#include <iostream>using namespace std;string longestPalindrome(string &s){ int len = s.size(); //字符串长度 int maxlen = 1; //最长回文字符串长度 int ...原创 2018-10-28 11:15:10 · 217 阅读 · 0 评论 -
How would you print just the 10th line of a file?
How would you print just the 10th line of a file?For example, assume that file.txt has the following content:Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10Your script shou...转载 2018-12-25 23:02:25 · 150 阅读 · 0 评论