小编sam*_*101的帖子

这个函数的时间复杂度/大O是不变的吗?

是这个程序的时间复杂度O(1)?

f1(int n){
  int temp = n;
  while (temp /= 2))
  {
    len++; result+=m;
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我们将int temp改为double temp,那么时间复杂度是否也会改变,或者它会保持不变?

f2(int n){
  double temp = (double)n;
  while (temp /= 2))
  {
    len++; result+=m;
  }
}
Run Code Online (Sandbox Code Playgroud)

c complexity-theory big-o loops time-complexity

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

导轨中的调试器与 byebug 不工作

当我尝试在带有 byebug 的 rails 中使用调试器时遇到问题......我安装了 byebug gem 没有任何问题......在 gemfile 中:

group :development, :test do
  gem 'sqlite3'
  gem 'byebug'
end
Run Code Online (Sandbox Code Playgroud)

将调试器放在我的控制器中:

class ArticlesController < ApplicationController
  before_action :set_article, only: [:edit, :update, :show, :destroy]

  def new
      @article = Article.new
  end

  def create
      debugger
      @article = Article.new(article_params)
      # @article.user = User.first
    if @article.save
      flash[:success] = "Article was successfully created"
      redirect_to article_path(@article)
    else
      render 'new'
    end
  end

  def show
  end

  def index
    @articles = Article.all
  end

  def edit
  end

  def update
    if @article.update(article_params)
      flash[:success] = …
Run Code Online (Sandbox Code Playgroud)

debugging rubygems ruby-on-rails ruby-on-rails-4 byebug

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

这个函数"循环"的复杂性/大O是多少

void mystery2 (int n)
{
  int i;
 for (i = 1; i <= n; i++) {
   double x = i;
   double delta = 1 / (double)i;
   while ( x > 0 )
     x -= delta;
  }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么是BIG O,这个函数的时间复杂度是O(n ^ 3)而不是O(n ^ 2)?

我做的是当i = 1 ==> 1次迭代时,i = 2 ==> 2iterations(in while)i = 3 ==> 3次迭代........ i = n ==> n次迭代,如果我们总结所有迭代,我们得到1 + 2 + 3 + 4 .... + n = n*(n + 1)/ 2.所以我在这里错过了什么?

c complexity-theory big-o loops time-complexity

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

为什么此代码中的时间复杂度为O(n ^ 2)?

我只是没有得到它,为什么时间复杂度是O(n ^ 2)而不是O(n*logn)?第二个循环每次递增2,所以不是O(logn)?

void f3(int n){
  int i,j,s=100;
  int* ar = (int*)malloc(s*sizeof(int));

  for(i=0; i<n; i++){
    s=0;
    for(j=0; j<n; j+=2){
      s+=j;
      printf("%d\n", s);
  }
  free(ar);
}
Run Code Online (Sandbox Code Playgroud)

c complexity-theory big-o loops time-complexity

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

为什么在这段代码中时间复杂度为O(n)而不是O(n ^ 2)?

为什么时间复杂度不是O(n ^ 2)而是O(n)?不是第一个循环是n时间,而第二个循环是相同的,所以它变成了O(n*n),这里有什么问题?

void f(int n){

     for( ; n>0; n/=2){
         int i;
         for(i=0; i<n; i++){
             printf("hey");
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

c complexity-theory big-o loops time-complexity

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