Example code 1:
The length calculation is placed outside the loop body and the result is only defeated by 26% of the Leetcode when running.
public void Movezeroes (int[] nums) {int counter = 0;for (int i = 0; i < nums.length; i++) {if (nums[i]! = 0) Nums[counte r++] = nums[i];} for (int j = counter; J < Nums.length; J + +) {Nums[j] = 0;}}
Example code 2:
The length calculation is placed outside the loop body, with a variable size to save the length of the array, in the Leetcode run, the results defeated 89% of the people, visible a small change, a great increase.
public void Movezeroes (int[] nums) {int size = Nums.length;int counter = 0;for (int i = 0; i < size; i++) {SYSTEM.OUT.P Rintln ("I:" + i + ", Counter:" + counter); if (nums[i]! = 0) nums[counter++] = nums[i];} for (int j = counter; j < size; J + +) {Nums[j] = 0;}}
In Java, the length calculation of an array is placed outside the loop body to increase the speed of operation