小编ksh*_*har的帖子

JavaScript - ReferenceError:未定义 WebSocket

仅关注客户端 API(因为每个服务器端语言都有自己的 API),以下代码段打开一个连接,为连接、断开连接和消息事件创建事件侦听器,将消息发送回服务器,然后关闭使用 WebSocket 连接。

// Create a socket instance
var socket = new WebSocket('ws://localhost:3000');

// Open the socket
socket.onopen = function(event) {

    // Send an initial message
    socket.send('I am the client and I\'m listening!');

    // Listen for messages
    socket.onmessage = function(event) {
        console.log('Client received a message',event);
    };

    // Listen for socket closes
    socket.onclose = function(event) {
        console.log('Client notified socket has closed',event);
    };

    // To close the socket....
    socket.close()

};
Run Code Online (Sandbox Code Playgroud)

但是我在执行上面的代码片段时遇到错误:

参考错误:未定义 WebSocket

我已经浏览了各种链接,如https://davidwalsh.name/websocket,关于如何实现 WebSockets。但是他们都没有导入任何npm package。 …

javascript websocket node.js npm

21
推荐指数
3
解决办法
3万
查看次数

查找数组中的重复项 - 时间复杂度 < O(n^2) 且常量额外空间 O(1)。(亚马逊采访)

下面给出问题陈述和解决方案。我无法掌握解决方案背后的逻辑。

问题陈述:
给定一个包含 n + 1 个整数的数组 nums,其中每个整数都在 1 到 n(含)之间,证明至少必须存在一个重复的数字。假设只有一个重复的数字,找出重复的数字。

注意:您不得修改该数组(假设该数组是只读的)。您必须仅使用常量 O(1) 额外空间。您的运行时复杂度应小于 O(n2)。数组中只有一个重复的数字,但它可以重复多次。

示例输入:[3 4 1 4 1] 输出:1

leetcode上发布的问题的解决方案是:

class Solution(object):
    def findDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        low = 1
        high = len(nums)-1

        while low < high:
            mid = low+(high-low)/2
            count = 0
            for i in nums:
                if i <= mid:
                    count+=1
            if count <= mid:
                low = mid+1
            else:
                high = mid
        return low
Run Code Online (Sandbox Code Playgroud)

上述代码的解释(根据作者):该解决方案基于二分搜索。

首先,搜索空间是 1 到 n …

python arrays algorithm

3
推荐指数
1
解决办法
2521
查看次数

C:qsort功能的时间复杂度是多少?

C中标准库的qsort函数的复杂性是多少?如果可能,请提供参考.

   #include <stdlib.h>
     void qsort(void *base, size_t nmemb, size_t size,
     int (*compar)(const void *, const void *));
Run Code Online (Sandbox Code Playgroud)

c sorting

-4
推荐指数
1
解决办法
4009
查看次数

标签 统计

algorithm ×1

arrays ×1

c ×1

javascript ×1

node.js ×1

npm ×1

python ×1

sorting ×1

websocket ×1