小编use*_*382的帖子

T(n-1)的时间复杂度

我很困惑解决这个时间复杂性问题.

T(n) = T(n-1)
Run Code Online (Sandbox Code Playgroud)

我知道快速排序最糟糕的情况 T(n) = T(n-1) + T(1) + n

评估为(n-1) + (n-2) + (n-3) + ... + 1&此几何序列等于O(n^2)

然而.我看到stackoverflow上的答案T(n) = T(n-1) + c = O(n).

当这也等于时(n-1) + (n-2) + (n-3) + ... + 1,这怎么可能呢?O(n^2)

有人可以解释一下.

algorithm big-o time-complexity data-structures

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

PostgreSQL:LIMIT + ORDER 返回不一致的结果

每当我运行同时包含 theLIMITORDER BY关键字的查询时,根据LIMIT子句中指定的项目数,我会得到不一致的结果:

例如,以下查询:

SELECT "people"."name" 
FROM "people" 
WHERE "people"."type" = 2 
ORDER BY score desc, number desc 
LIMIT 50
Run Code Online (Sandbox Code Playgroud)

结果给了我这个:

[John, Jack, Amy, Brad, Tom, ...]
Run Code Online (Sandbox Code Playgroud)

(约翰和杰克的scorenumber属性值相同。事实上,艾米、布拉德和汤姆也是如此。)

现在,当我运行时:

SELECT "people"."name" 
FROM "people" 
WHERE "people"."type" = 2 
ORDER BY score desc, number desc 
LIMIT 5
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

[Jack, John, Tom, Amy, Brad]
Run Code Online (Sandbox Code Playgroud)

sql postgresql

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

Rails:Dependent delete_all无法正常工作

我有5个型号.Server,Platform,Game,RetentionReport,DataReport.而我正在尝试使用:dependent => :delete_all,但它不会工作.这是我的模特.

class Game < ActiveRecord::Base
  attr_accessible :name

  has_many :platforms, :dependent => :delete_all
end

class Platform < ActiveRecord::Base
  attr_accessible :name, :game_id, :company_id

  belongs_to :game
  has_many :servers, :dependent => :delete_all
end

class Server < ActiveRecord::Base
  attr_accessible :name, :region, :device_type, :platform_id, :platform_server_id

  belongs_to :platform
  has_many :gm_data_reports, :dependent => :delete_all
  has_many :gm_retention_reports, :dependent => :delete_all

  delegate :company_id, :to => :platform

  validates :platform_server_id, :uniqueness => {:scope => :platform_id}
end

class DataReport …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails ruby-on-rails-3

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

Hibernate ManyToMany删除的对象将通过级联重新保存

出于某种原因,我无法删除属于多对多关系的对象.我收到以下错误:

Exception in thread "main" org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [edu.cs157b.hibernate.AppointmentRequest#11]
    at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1232)
Run Code Online (Sandbox Code Playgroud)

这是我的三个类,它们映射了多对多的关系.从本质上讲,Doctor有很多Patients通过AppointmentRequest,反之亦然.这是课程

医生

package edu.cs157b.hibernate;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name="DOCTOR_INFO")
@NamedQueries (
    {
        @NamedQuery(name = "Doctor.getAll", query = "from Doctor"),
        @NamedQuery(name = "Doctor.findByName", query = "from Doctor where name = :name")
    }
)
public class Doctor implements Person {

    private int id;
    private String name;
    private Specialty specialty;
    private List<AppointmentRequest> appointmentRequests …
Run Code Online (Sandbox Code Playgroud)

java eclipse hibernate jpa

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

什么是时间复杂度?

以下功能的时间复杂度是多少?

    for(int i = 0; i < a.size; i++) {
        for(int j = i; j < a.size; i++) {
            //
        }
    }
Run Code Online (Sandbox Code Playgroud)

我认为它不是大O n ^ 2因为我们没有迭代第二个for循环中的所有元素.我相信时间的复杂性是这样的:

n[ (n) + (n-1) + (n-2) + ... + (n-n) ]
Run Code Online (Sandbox Code Playgroud)

但是,当我解决这个公式时,它就会出现

n^2 - n + n^2 - 2n + n^2 - 3n + ... + n^2 - n^2
Run Code Online (Sandbox Code Playgroud)

这根本不正确.有人能告诉我究竟如何解决这个问题,以及我错在哪里.

algorithm big-o runtime-compilation time-complexity

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

Chrome CSS失败

嗨,我正在尝试更改select元素的CSS ,它在Firefox中工作正常,但它在谷歌浏览器中不起作用.当我试图使用Chrome开发者工具,我看到下面Styles/Matched CSS Rules的元素高度成功更改30px,但是当我去Computed Styleheight显示为18px,但是当我点击箭头以获得更多的信息,它表明高度应30px

#new-job select - 30px 
Run Code Online (Sandbox Code Playgroud)

我是html/css和chromes开发人员工具的新手,但它似乎应该对我有用.这是我的代码

<div id="new-job">  
  <form accept-charset="UTF-8" action="/jobs" class="new_job" id="new_job" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="?"><input name="authenticity_token" type="hidden" value="vfKmC+vnnIl/6icDIbDRfZghNR927qmEqTdB9I2OeMQ="></div>

    <div class="field">
      <label for="job_category">Category</label>
      <select id="job_category" name="job[category]"><option value=""></option>
                <option value="Bistro Card">Bistro Card</option>
                <option value="Dongle">Dongle</option>
                <option value="Return">Return</option>
                <option value="Catering">Catering</option>
                <option value="Other">Other</option></select><br><br>
    </div>
    <div class="field">
      <label for="job_location">Location</label><br>
      <input id="job_location" name="job[location]" size="30" type="text">
    </div>
    <div class="field">
      <label for="job_description">Description</label><br>
      <textarea cols="40" id="job_description" name="job[description]" rows="20"></textarea>
    </div>
    <div …
Run Code Online (Sandbox Code Playgroud)

html css html5 google-chrome

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

Rails:由不正确的控制器操作处理的URL

我有一个由错误的控制器操作处理的URL.在我的路线文件中,我有以下内容

post '/tweets' => 'tweets#create'
Run Code Online (Sandbox Code Playgroud)

每当我发布到这个网址时,它都会被处理tweets#index.

Started POST "/tweets" for 127.0.0.1 at 2014-03-27 20:04:25 -0700
Processing by TweetsController#index as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"CCN9rDQiSXkVK0sF/BGU19fIufJTpho/ocySPmu7Lsc=", "tweet"=>{"title"=>"sdfdsafdsa", "description"=>"sdfdsasdadsa", "user_id"=>"1"}, "commit"=>"Create Tweet"}
  Tweet Load (0.4ms)  SELECT `tweets`.* FROM `tweets` 
  Rendered tweets/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 42ms (Views: 40.6ms | ActiveRecord: 0.4ms)
Run Code Online (Sandbox Code Playgroud)

我试过重新启动我的rails服务器但它仍然无法正常工作.

这是结果 rake routes

tweeter: rake routes
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails rails-routing ruby-on-rails-3

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

社交网站如何处理通知

我正在实施一个小型社交网站,我正在尝试实施通知.

通知具有以下要求

  • 所有用户都会在他们关注的用户做某些事件时收到通知(更改个人资料图片,如帖子,创建帖子,发表评论等等...
  • 通知的状态为已读或未读(如facebook和stackoverflow).当用户的一组通知未读时,就像Facebook一样,用户将继续在其导航栏上看到阅读通知图标

以下是我想用MySQL实现它的方法:

通知表:type_of_event,event_id,message Notification_read表:user_id,notification_id,read

  • 每当用户执行触发通知发送给其所有关注者的事件时,该通知都存储在Notification表中
  • Notification_read表格将填写新创建的通知,其中包含新通知的ID和应接收通知的每个用户的ID(通知创建者的关注者)
  • 每当用户收到通知并读取通知时,通知都将标记为 read

这个解决方案对我来说似乎效率很低,因为每次发生通知时,通知都会notification_read多次写入表中(取决于用户拥有多少关注者)

有人能告诉我是否有更好的解决方案来解决这个问题

notifications facebook ruby-on-rails social-networking

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

C:pthread无法在套接字上侦听,绑定和接受

我正在尝试创建一个侦听套接字连接的进程.当我在main()函数中绑定,监听和等待接受时,它似乎有效.但是当我尝试创建一个新线程并在新线程上绑定,监听和接受时,它就失败了.这是我的代码.

void request_handler(int clientSock) {
    FILE *requestedFile = NULL;
    long fileSize = 0;
    struct stat st;
    long bytesRead;
    char buffer[1024];

    requestedFile = fopen("/PATH/book.txt", "rb");

    while(!feof(requestedFile)) {
        bytesRead = fread(buffer, 1, sizeof(buffer), requestedFile);
        send(clientSock, buffer, bytesRead, 0);
    }

}

void listener() {
    int server_sock_desc;
    struct sockaddr_in name;

    int client_sock_desc;
    struct sockaddr_in client_name;
    socklen_t addr_size;

    pthread_t handler_thread;

    printf("waiting");

    //connection setup
    server_sock_desc = socket(PF_INET, SOCK_STREAM, 0);


    if(server_sock_desc != -1) {
        memset(&name, 0, sizeof(name));
        name.sin_family = AF_INET;
        name.sin_port = htons(5000);
        name.sin_addr.s_addr = htonl(INADDR_ANY);
        int …
Run Code Online (Sandbox Code Playgroud)

c sockets multithreading network-programming

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