Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you're looking for <em>which</em> columns contain ones and now <em>how many</em> ones each column contains, looping through them seems like the best idea.</p> <p>If you implement your loops with some <strong>short-circuit</strong> logic, then you'll get a better average running time.</p> <p>Something like:</p> <pre><code>for (int column = 0; column &lt; width; column++) { for (int row = 0; row &lt; height; row++) { if (array[column][row] == 1) { list.append(column); break; // move on to the next column because we don't care what's // left in this column as we already found our '1' </code></pre> <p>With this code, you'll get a <strong>worst-case</strong> (if every <code>bit</code> is <code>0</code>) running time of <code>n</code> (<code>n</code> being the total number of <code>bits</code>) which is pretty good.</p> <p>But unless you're extremely unlucky, you'll get a much better running time due to the <strong>short-circuit</strong> logic.</p> <p>The above works for arrays of bits, however, <code>BitSets</code> have some useful functionality of their own.</p> <p>A <code>BitSet</code> contains the functions: <code>length()</code> which returns the highest index with a set bit + 1 (or <code>0</code> if there are no set bits) and <code>nextSetBit(index)</code> which returns the index of the next set bit from <code>index -&gt; end</code> inclusive.</p> <p>So you're code could easily be something like:</p> <pre><code>int index = 0; while (index &lt; BitSet.length()) { index = BitSet.nextSetBit(index); list.append(index); index++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload