GVKun编程网logo

[Swift]LeetCode436. 寻找右区间 | Find Right Interval

2

如果您对[Swift]LeetCode436.寻找右区间|FindRightInterval感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于[Swift]LeetCode43

如果您对[Swift]LeetCode436. 寻找右区间 | Find Right Interval感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于[Swift]LeetCode436. 寻找右区间 | Find Right Interval的详细内容,并且为您提供关于book.find() 操作 `books.find()` 缓冲在 10000 毫秒后超时 Heroku 错误、c – 为什么std :: find(s.begin(),s.end(),val)对于一个s来说比s.find(val)慢1000?、C++ find(STL find)查找算法详解、css – “margin-left”和“left”(或“margin-right”和“right”)之间的差异的有价值信息。

本文目录一览:

[Swift]LeetCode436. 寻找右区间 | Find Right Interval

[Swift]LeetCode436. 寻找右区间 | Find Right Interval

Given a set of intervals,for each of the interval i,check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i,which can be called that j is on the "right" of i.

For any interval i,you need to store the minimum interval j‘s index,which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn‘t exist,store -1 for the interval i. Finally,you need output the stored value of each interval as an array.

Note:

  1. You may assume the interval‘s end point is always bigger than its start point.
  2. You may assume none of these intervals have the same start point. 

Example 1:

Input: [ [1,2] ]

Output: [-1]

Explanation: There is only one interval in the collection,so it outputs -1. 

Example 2:

Input: [ [3,4],[2,3],[1,2] ]

Output: [-1,1]

Explanation: There is no satisfied "right" interval for [3,4].
For [2,the interval [3,4] has minimum-"right" start point;
For [1,2],the interval [2,3] has minimum-"right" start point. 

Example 3:

Input: [ [1,[3,4] ]

Output: [-1,2,-1]

Explanation: There is no satisfied "right" interval for [1,4] and [3,4] has minimum-"right" start point.

给定一组区间,对于每一个区间 i,检查是否存在一个区间 j,它的起始点大于或等于区间 i 的终点,这可以称为 j 在 i 的“右侧”。

对于任何区间,你需要存储的满足条件的区间 j 的最小索引,这意味着区间 j 有最小的起始点可以使其成为“右侧”区间。如果区间 j 不存在,则将区间 i 存储为 -1。最后,你需要输出一个值为存储的区间值的数组。

注意:

  1. 你可以假设区间的终点总是大于它的起始点。
  2. 你可以假定这些区间都不具有相同的起始点。

示例 1:

输入: [ [1,2] ]
输出: [-1]

解释:集合中只有一个区间,所以输出-1。

示例 2:

输入: [ [3,2] ]
输出: [-1,1]

解释:对于[3,4],没有满足条件的“右侧”区间。
对于[2,3],区间[3,4]具有最小的“右”起点;
对于[1,2],区间[2,3]具有最小的“右”起点。

示例 3:

输入: [ [1,4] ]
输出: [-1,-1]

解释:对于区间[1,4]和[3,4]有最小的“右”起点。
5324ms
 1 /**
 2  * DeFinition for an interval.
 3  * public class Interval {
 4  *   public var start: Int
 5  *   public var end: Int
 6  *   public init(_ start: Int,_ end: Int) {
 7  *     self.start = start
 8  *     self.end = end
 9  *   }
10  * }
11  */
12 class Solution {
13     func findRightInterval(_ intervals: [Interval]) -> [Int] {
14         var res:[Int] = [Int]()
15         var v:[Int] = [Int]()
16         var m:[Int:Int] = [Int:Int]()
17         for i in 0..<intervals.count
18         {
19             m[intervals[i].start] = i
20             v.append(intervals[i].start)
21         }
22         v = v.sorted(by:>)
23         for a in intervals
24         {
25             var i:Int = 0
26             while(i < v.count)
27             {
28                 if v[i] < a.end
29                 {
30                     break
31                 }
32                 i += 1                
33             }
34             res.append((i > 0) ? m[v[i - 1]]! : -1)
35         }
36         return res
37     }
38 }

book.find() 操作 `books.find()` 缓冲在 10000 毫秒后超时 Heroku 错误

book.find() 操作 `books.find()` 缓冲在 10000 毫秒后超时 Heroku 错误

如何解决book.find() 操作 `books.find()` 缓冲在 10000 毫秒后超时 Heroku 错误

在本地主机上一切正常,但是当我打开服务器时,我在 Heroku 上遇到了这个错误

2021-06-01T22:42:01.580808+00:00 app[web.1]: MongooseError: Operation books.find() 缓冲在 10000 毫秒后超时

2021-06-01T22:42:01.580809+00:00 app[web.1]:超时。 (/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20)

2021-06-01T22:42:01.580809+00:00 app[web.1]: at listOnTimeout (node:internal/timers:557:17)

2021-06-01T22:42:01.580809+00:00 app[web.1]: at processtimers (node:internal/timers:500:7)

这是我的代码, 路由器

  1. router.get("/",async (req,res) => {
  2. const books = await bookModel.find();
  3. if (!books) {
  4. res.status(500).json({ success: false });
  5. }
  6. res.send(books);
  7. });
  8. router.get("/:id",res) => {
  9. const books = await bookModel.findById(req.params.id);
  10. if (!books) {
  11. res.status(500).json({ success: false });
  12. }
  13. res.send(books);
  14. });

型号

  1. const mongoose = require("mongoose");
  2. const bookSchema = mongoose.Schema({
  3. name: String,price: Number,image: String,});
  4. exports.bookModel = mongoose.model("Book",bookSchema);

app.js

  1. const express = require("express");
  2. const mongoose = require("mongoose");
  3. const app = express();
  4. const booksRouter = require("./routes/books");
  5. app.use(express.json());
  6. app.use("/books",booksRouter);
  7. app.get("/",(req,res) => {
  8. res.send("Hello");
  9. });
  10. mongoose
  11. .connect(
  12. "mongodb+srv://*****:*******@cluster0.f0bzw.mongodb.net/**********?retryWrites=true&w=majority",{
  13. dbname: "*********",useNewUrlParser: true,useUnifiedTopology: true,}
  14. )
  15. .then(console.log("Connected"))
  16. .catch((err) => console.log(err));
  17. let port = process.env.PORT || 3000;
  18. app.listen(port);

c – 为什么std :: find(s.begin(),s.end(),val)对于一个s来说比s.find(val)慢1000?

c – 为什么std :: find(s.begin(),s.end(),val)对于一个s来说比s.find(val)慢1000?

我最近开始重新学习C,因为我已经十多年没有用C编码了.我很少使用STL,即使我在sgi工作,我也想掌握它.我订购了一本书,目前正在运行不同的在线教程.

一个教程介绍了std :: find(begin(),end(),value),我对我写的测试代码有多慢感到震惊.在做了一些试验和错误之后,我发现s.find(value)显然是我应该使用的.

为什么代码中的第一个查找速度如此之慢?

set<int> s;

for (int i = 0; i < 100000; i++)
    s.insert(rand());

for (int i = 0; i < 10000; i++) {
    int r = rand();

    //first find is about 1000x slower than the next one
    auto iter1 = std::find(s.begin(),s.end(),r);
    auto iter2 = s.find(r);
}

编辑:添加计时实验结果

@juanchopanza在评论中询问时间,所以我在Set,List,Vector和set.find()上定时std :: find()
(我只测量了发现 – 运行之间的差异低于10%)

Vector比List或Set执行得更好,但是set中的专门查找会获得大数据集.

Elements  Vector     List      Set    | Set.Find()
      10   0.0017    0.0017    0.0020  |  0.0017
     100   0.0028    0.0051    0.0120  |  0.0019
    1000   0.0105    0.0808    0.1495  |  0.0035
   10000   0.0767    0.7486    2.7009  |  0.0068
  100000   0.2572    2.4700    6.9636  |  0.0080
 1000000   0.2674    2.5922    7.0149  |  0.0082
10000000   0.2728    2.6485    7.0833  |  0.0082

解决方法

std :: find是一个通用算法,给定一对迭代器可以找到一个值.如果所有它都是一对迭代器,那么找到一个值的最好方法就是线性搜索它,即O(n).

set :: find是std :: set的成员函数,因此它知道它搜索的数据结构,因此可以优化搜索.排序,平衡的树具有出色的搜索行为O(log(n))

C++ find(STL find)查找算法详解

C++ find(STL find)查找算法详解

find() 为在输入迭代器所定义的范围内查找单个对象的算法,它可以在前两个参数指定的范围内查找和第三个参数相等的第一个对象。

find 算法会返回一个指向被找到对象的迭代器,如果没有找到对象,会返回这个序列的结束迭代器。下面展示了如何使用 find():
std::vector<int> numbers {5,46,-5,-6,23,17,5,9,6,5};
int value {23};
auto iter = std::find(std::begin(numbers),std::end(numbers),value);
if (iter != std:: end (numbers))
    std::cout << value << " was found. \n";
这段代码会输出一条在 numbers 中找到 23 的消息,当然,可以反复调用 find() 来找出这个序列中所有给定元素的匹配项:
size_t count {};
int five {5};
auto start_iter = std::begin(numbers);
auto end_iter = std::end(numbers);
while((start_iter = std::find(start_iter,end_iter,five)) != end_iter)
{
    ++count;
    ++start_iter;
}
std::cout << five << " was found " << count << " times." << std::endl; // 3 times
在 while 循环中,count 变量会通过自增来记录 five 在 vector 容器 numbers 中的发现次数。循环表达式调用 find(),在 start_iter 和 end_iter 定义的范围内查找 five。find() 返回的迭代器被保存在 start_ter 中,它会覆盖这个变量先前的值。最初,find() 会搜索 numbers 中的所有元素,因此 find() 会返回一个指向 five 的第一个匹配项的迭代器。

每次找到 five,循环体中的startjter都会自增,因此它会指向被找到元素的后一个元素。所以,下一次遍历搜索的范围是从这个位置到序列末尾。当不再能够找到 five 时,find() 会返回 end_iter,循环结束。

css – “margin-left”和“left”(或“margin-right”和“right”)之间的差异

css – “margin-left”和“left”(或“margin-right”和“right”)之间的差异

有什么区别
margin-left:10px;和位置:相对;左:10px;?

它似乎产生了相同的结果

解决方法

当您使用position:relative移动某个东西时,您实际上并没有移动它在页面上占据的空间,只是在它显示的位置.

测试这个的简单方法是使用这样的简单结构:

<div id = "testBox">
  <img src = "http://www.dummyimage.com/300x200" id = "first">
  <img src = "http://www.dummyimage.com/300x200" id = "second">
</div>

使用以下CSS:

img{ display:block; }
#first{ margin-top:50px; }

与这个CSS:

img{display:block;}
#first{position:relative; top:50px;}

您将看到第一个将所有内容向下移动50px而第二个仅向下移动第一个图像(这意味着它将与第二个图像重叠).

编辑:你可以在这里查看它:http://www.jsfiddle.net/PKqMT/5/

评论这个位置:相对;和顶部:100px;线条和取消注释边缘顶线,你会看到差异.

关于[Swift]LeetCode436. 寻找右区间 | Find Right Interval的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于book.find() 操作 `books.find()` 缓冲在 10000 毫秒后超时 Heroku 错误、c – 为什么std :: find(s.begin(),s.end(),val)对于一个s来说比s.find(val)慢1000?、C++ find(STL find)查找算法详解、css – “margin-left”和“left”(或“margin-right”和“right”)之间的差异的相关知识,请在本站寻找。

本文标签: