Finding unique triplets adding up to 0
I don't have a solution better than yours (you can google for java 3Sum O(n^2) to find some examples), but I have some small comments on your code:

While loops

I would remove all the while loops. They make your code harder to read and don't really speed it up (in my tests, they actually slowed it down).

Minor performance improvements


array access costs time, if you save num[i], etc in local variables, it will slightly speed your code up.
move quicker checks to the beginning: nj &gt; 0 &amp;&amp; ni + nj &gt; 0 instead of ni + nj &gt; 0 &amp;&amp; nj &gt; 0 should be quicker, but only marginally so.


Style


use correct indentation
in Java, the opening curly bracket commonly goes on the same line
use curly brackets, even for one-line statements
use more spaces (for example while(k+1&lt;num.length &amp;&amp; num[k]==num[k+1]) would be while (k + 1 &lt; num.length &amp;&amp; num[k] == num[k + 1])