Java 爪哇蝙蝠问题
返回一个数组,该数组包含与给定数组完全相同的数字,但需要重新排列,以便将所有零分组在数组的开头。非零数的顺序并不重要。所以{1,0,0,1}变成了{0,0,1,1}。您可以修改并返回给定数组或创建新数组Java 爪哇蝙蝠问题,java,arrays,Java,Arrays,返回一个数组,该数组包含与给定数组完全相同的数字,但需要重新排列,以便将所有零分组在数组的开头。非零数的顺序并不重要。所以{1,0,0,1}变成了{0,0,1,1}。您可以修改并返回给定数组或创建新数组 zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1} zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1} zeroFront({1, 0}) → {0, 1} 这就是我所拥有的,但我知道这是难以置信的错误 public int[]
zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1}
zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zeroFront({1, 0}) → {0, 1}
这就是我所拥有的,但我知道这是难以置信的错误
public int[] zeroFront(int[] nums) {
for (int index = 0; index < nums.length; index ++)
{
int [] array1 = new int [index];
if (nums [index] == 0)
{
array1 = nums [index];
}
}
for (int index = 0; index < nums.length; index ++)
{
int [] array2 = new int [nums.length-index];
if (nums [index] != 0)
{
array2 = nums [index];
}
}
return array1 + array2;
}
public int[]zeroFront(int[]nums){
对于(int index=0;index
排序方法不适用于负数,而另一种方法不适用于某些情况(请参阅注释)。
最简单(也是最短)的方法是对数组进行排序(只适用于非负数,谢谢Tom):
另一种方法是在找到非0时交换值:
int lastNonZero = -1;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0 && lastNonZero!=-1) {
int tmp = nums[i];
nums[i] = nums[lastNonZero];
nums[lastNonZero] = tmp;
lastNonZero = -1;
}
else lastNonZero = i;
}
int lastNonZero=-1;
对于(int i=0;i
未测试此功能,但应能正常工作:
public int[] zeroFront(int[] nums) {
if (nums == null) {
return null;
}
int[] result = new int[nums.length];
int zeroesPos = 0;
int othersPos = result.length - 1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) {
result[zeroesPos] = 0;
++zeroesPos;
} else {
result[othersPos] = nums[i];
--othersPos;
}
}
return result;
}
请注意,您不必在创建临时值的地方进行常规交换,因为您知道该值为0
希望这有帮助。也没有测试过,但这应该简单得多
public int[] zeroFront(int[] nums) {
if (nums == null) {
return null;
}
int zerosPos = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) { //swap zerosPos with current position
num[i]=num[zerosPos];
num[zerosPos]=0;
++zerosPos;
}
}
return num;
}
public int[]zeroFront(int[]nums){
如果(nums==null){
返回null;
}
int zerosPos=0;
对于(int i=0;i
希望这有帮助
RDJ对于所示的示例数据,可以简单地对数组进行排序 如果它们不具有代表性,那么编写一个比较器来比较Math.abs(number)而不是number,并使用它进行排序
public int[]zeroFront(int[]nums){
public int[] zeroFront(int[] nums) {
int t = 0;
for(int i=0 ; i< nums.length ; i++)
{
for(int j=i+1; j<nums.length; j++)
if(nums[j]==0 && nums[i]!=0)
{
t=nums[i];
nums[i]=nums[j];
nums[j]=t;
}
}
return nums;
}
int t=0;
对于(int i=0;ipublic int[]zeroFront(int[]nums){
int k=0;
int narr[]=新int[nums.length];
对于(int i=0;i
public int[]zeroFront(int[]nums){
对于(int j=0;j0&&nums[i]==0){
如果(nums[i-1]!=0){
int temp=nums[i];
nums[i]=nums[i-1];
nums[i-1]=温度;
}
}
继续;
}
}
返回nums;
}
public int[]zeroFront(int[]nums){
堆栈=新堆栈();
int[]nums2=新的int[nums.length];
对于(int i=0;i
以下是我的答案:
public int[] zeroFront(int[] nums) {
//Create an ArrayList to add the iterations where zero is.
ArrayList<Integer> zeroPlaces = new ArrayList<Integer>();
//Get the place where the zeros are and add it to the ArrayList.
for (int i = 0; i < nums.length; i++)
{
if (nums[i] == 0)
{
zeroPlaces.add(i);
}
}
//Create a return array to manipulate, set initally to nums.
int[] ret = nums;
//Sort nums by using the ArrayList.
for (int i = 0; i < zeroPlaces.size(); i++)
{
int temp = ret[i];
ret[i] = ret[zeroPlaces.get(i)];
ret[zeroPlaces.get(i)] = temp;
}
//Finally return ret;
return ret;
}
public int[]zeroFront(int[]nums){
//创建一个ArrayList以添加0为的迭代。
ArrayList zeroPlaces=新的ArrayList();
//获取零所在的位置并将其添加到ArrayList。
对于(int i=0;i
看看我的答案
公共int[]zeroFront(int[]nums){
int fp=0;//开始时的指针
int lp=nums.length-1;//指针位于末尾
布尔值isZero=false;//找到零
布尔值isOne=false;//找到一个
而(fp
这是家庭作业还是什么?无法想象一个用例。:)我假设array1+array2是伪代码。这实际上可以在数组中一次完成…如果需要,甚至可以在原始数组中存储结果。嘿,bob…我知道你是stackoverflow新手…但你应该“接受”这两种方法都有效,您发现最有用的答案是:-)。是的,如果您切换到0而不是直接设置它,那么我认为您可以在原始数组中进行“排序”。这是“荷兰国旗问题”的简化版本.当然,在两个分支上都交换。我可以发誓,我最初写这篇文章时,最后一段是不存在的
public int[] zeroFront(int[] nums) {
int t = 0;
for(int i=0 ; i< nums.length ; i++)
{
for(int j=i+1; j<nums.length; j++)
if(nums[j]==0 && nums[i]!=0)
{
t=nums[i];
nums[i]=nums[j];
nums[j]=t;
}
}
return nums;
}
public int[] zeroFront(int[] nums) {
int k=0;
int narr[] = new int[nums.length];
for(int i=0 ; i< nums.length ; i++)
if(nums[i]==0)
{
narr[k]=nums[i];
k=k+1;
}
for(int i=0 ; i< nums.length ; i++)
if(nums[i]!=0)
{
narr[k]=nums[i];
k=k+1;
}
return narr;
}
public int[] zeroFront(int[] nums) {
for (int j = 0; j < nums.length; j++) {
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == 0) {
if (nums[i - 1] != 0) {
int temp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = temp;
}
}
continue;
}
}
return nums;
}
public int[] zeroFront(int[] nums) {
Stack stack = new Stack();
int[] nums2 = new int[nums.length];
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
stack.push(nums[i]);
}
}
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) {
stack.push(nums[i]);
}
}
for(int i = 0; i < nums.length; i++) {
nums2[i] = (Integer) stack.pop();
}
return nums2;
}
public int[] zeroFront(int[] nums) {
//Create an ArrayList to add the iterations where zero is.
ArrayList<Integer> zeroPlaces = new ArrayList<Integer>();
//Get the place where the zeros are and add it to the ArrayList.
for (int i = 0; i < nums.length; i++)
{
if (nums[i] == 0)
{
zeroPlaces.add(i);
}
}
//Create a return array to manipulate, set initally to nums.
int[] ret = nums;
//Sort nums by using the ArrayList.
for (int i = 0; i < zeroPlaces.size(); i++)
{
int temp = ret[i];
ret[i] = ret[zeroPlaces.get(i)];
ret[zeroPlaces.get(i)] = temp;
}
//Finally return ret;
return ret;
}
int fp = 0;// pointer at start
int lp = nums.length-1; // pointer at end
boolean isZero = false; // found zero
boolean isOne = false; // found one
while(fp < lp){ // until start is less than end
if(nums[lp] == 0){
// is zero found
isZero = true;
}
if(nums[fp] != 0){
// is other than zero found
isOne = true;
}
if(isZero && isOne){
// swap if both found
int temp = nums[fp];
nums[fp] = nums[lp];
nums[lp] = temp;
isZero = false;
isOne = false;
}
if(isOne){
// decrease end pointer
lp--;
continue;
}
if(isZero){
// increase start pointer
fp++;
continue;
}
// increase start pointer and decrease end pointer
fp++;
lp--;
}
return nums;
}