为什么我的字符数组就地递归代码不适用于所有测试用例?我的代码有什么问题吗

时间:2019-06-24 17:40:59

标签: java recursion

   {!! Form::open(['method' => 'POST', 'route' => ['tests.store'], 'class' => 'content__page test__content' ] ) !!}

            <div class="hamb__icon">
                <img src="{{ asset('images/menu.png') }}" alt="menu">
            </div>
            <div class="test__block">
                <div class="test__bottom">
                    <a href="#">Start the test</a>
                  </div>
            </div>

            <div class="test__wrap" style="display:none;">

                <h6 class="test__count">30</h6>
                @php
                    $i = 0;
                    $q = 1;
                @endphp

                @foreach($questions as $question)

                @if ($i == 0)
                    <div class="test__elem active__test-elem">
                    @php
                        $i++;
                    @endphp                        
                @elseif ($i != 0)
                <div class="test__elem" style="display:none;">
                @endif

                    <h2>Let's see what you <br> learned</h2>
                    <div class="test__elem-question">
                        <p> - </p>
                        <h6>{!! nl2br($question->question_text) !!}</h6>
                    </div>
                    <div class="test__elem-answers">

                        @foreach($question->options as $option)
                        <div class="test__elem-answer" data-value="{{ $option->id }}">
                            <p> - </p>
                            <h6>{{ $option->option }}</h6>
                            <input
                            type="hidden" name="questions[{{ $q }}]" value="{{ $question->id }}" id="questions-count">
                        </div>
                        @endforeach
                        <input type="hidden" name="answers[{{ $question->id }}]" value="{{ $question->id }}" id="questions-count" class="question__input">
                    </div>
                </div>
                @php
                    $q++;
                @endphp  
                @endforeach                

                <div class="test__elem last__test" style="display:none;">
                    <h3>Congratulations!</h3>
                    <p>In order to finish the test, please pres the button</p>
                    <div class="science__block_submit">
                        {!! Form::submit('End Test') !!}
                    </div>
                </div>
            </div>
        {!! Form::close() !!}

为什么我的字符数组就地递归代码不适用于所有测试用例?我的代码有问题吗?

1 个答案:

答案 0 :(得分:0)

我建议使用以下内容,而不是使用必须实例化的类来反转数组:

class Solution {

   public static void reverseString(char[] str) {
      reverseStringImpl(str, 0);
   }

   private static void reverseStringImpl(char[] s, int k) {
      // details here
   }
}

我省略了细节(但是数组和起始位置足以完成此操作)。这仍然是递归的,但是它隐藏了实现并且不需要任何instance variables来维护state。也不要求用户将0的初始开始位置传递给该方法。