小编Jon*_*Jon的帖子

使用迭代而不是递归的拓扑排序

我最初有一个用于拓扑排序的递归解决方案:

void dfs(int v, bool visited[], node* adjList[], std::stack<int> &myStack) {
    visited[v] = true;
    node* curr = adjList[v];

    while (curr != NULL) {
        if(!visited[curr->val]) {
            dfs(curr->val, visited, adjList, myStack);
        }
        curr = curr->next;
    }
    myStack.push(v);
}
Run Code Online (Sandbox Code Playgroud)

它似乎对我的目的有用。但我无法将其转化为迭代解决方案。这是我的尝试:

void dfs(int v, bool visited[], node* adjList[], std::stack<int> &myStack) {
    std::stack<int> recursion;
    visited[v] = true;

    recursion.push(v);

    while( !recursion.empty() ) {
        v = recursion.top();
        recursion.pop();
        node* curr = adjList[v];

        myStack.push(v);

        while (curr != NULL) {
            if(!visited[curr->val]) {
                visited[curr->val] = true;
                recursion.push(curr->val);
            }
            curr = …
Run Code Online (Sandbox Code Playgroud)

c++ recursion graph depth-first-search topological-sort

5
推荐指数
1
解决办法
1808
查看次数

如何从Flask服务器(Python)向HTML客户端发送消息?

为了练习,我试图让Flask服务器不断地将"Hello"打印到HTML页面的控制台.我现在迷失了如何继续.

目前,我的服务器代码看起来像这样

app = Flask(__name__)
 app.config['SECRET_KEY'] = 'secret!'
 socketio = SocketIO(app)

@socketio.on('message')
 def handle_message(message):
    print('Message: ' + message)
     send(message)

@socketio.on('json')
 def handle_json(json):
    send(json, json=True)

@socketio.on('my event')
  def handle_my_custom_event(json):
     emit('my response', json, broadcast=True)

def hello():
    emit('message', {'hello':"Hello"})

@app.route('/')
 def index():
    return render_template('index.html')

if __name__ == '__main__':
     socketio.run(app)
     while True:
          hello()
Run Code Online (Sandbox Code Playgroud)

虽然我的客户端代码如下所示:

<head>
      <script type="text/javascript" charset="utf-8">
            var socket = io.connect('http://' + document.domain + ':' + location.port);
            socket.on('connect', function() {
                 socket.emit('connected');
            });
            socket.on('message', function(data) {
                 console.log(data);
            });
      </script>
</head> …
Run Code Online (Sandbox Code Playgroud)

python web-applications send websocket flask

2
推荐指数
1
解决办法
5010
查看次数

使用try-finally块处理输入流有什么好处?

这是我在网上找到的一个Java示例:

try{
      //use buffering
      InputStream file = new FileInputStream("quarks.ser");
      InputStream buffer = new BufferedInputStream(file);
      ObjectInput input = new ObjectInputStream (buffer);
      try{
        //deserialize the List
        List<String> recoveredQuarks = (List<String>)input.readObject();
        //display its data
        for(String quark: recoveredQuarks){
          System.out.println("Recovered Quark: " + quark);
        }
      }
      finally{
        input.close();
      }
} catch(ClassNotFoundException ex){
      //some exception handling
}
Run Code Online (Sandbox Code Playgroud)

在上面,使用try-finally块在关闭输入之前使用输入执行某些处理有什么好处?换句话说,上面的代码对这样的东西有什么好处:

try{
      //use buffering
      InputStream file = new FileInputStream("quarks.ser");
      InputStream buffer = new BufferedInputStream(file);
      ObjectInput input = new ObjectInputStream (buffer);
      List<String> recoveredQuarks = (List<String>)input.readObject();
      for(String quark: recoveredQuarks){
          System.out.println("Recovered Quark: …
Run Code Online (Sandbox Code Playgroud)

java file-io inputstream try-finally

0
推荐指数
1
解决办法
377
查看次数