Skip to content

Commit

Permalink
🚚 rename pkg and add <Fold an array>.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonGravel committed Jan 21, 2020
1 parent 2e43dee commit 540a8fa
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion java/BitCounting.java → javasolutions/BitCounting.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName BitCounting
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

import java.util.HashMap;
import java.util.Map;
Expand Down
2 changes: 1 addition & 1 deletion java/FindUniq.java → javasolutions/FindUniq.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName FindUniq
Expand Down
38 changes: 38 additions & 0 deletions javasolutions/FoldArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package javasolutions;

import java.util.Arrays;

/**
* @ClassName FoldArray
* @Description: ref: https://www.codewars.com/kata/57ea70aa5500adfe8a000110/train/java
* @Author gravel
* @Date 2020/1/21
* @Version V1.0
**/
public class FoldArray {
public static int[] foldArray(int[] array, int runs) {
if (array == null || array.length < 2) return array;
if (runs == 0) return array;
int l = 0;
int r = array.length - 1;
int len = array.length;
int[] copyArr = Arrays.copyOf(array,len);
while (runs > 0) {
while (l < r) {
copyArr[l] = copyArr[l] + copyArr[r];
l++;
r--;
}
double c = ((double) len) / 2;
len = (int) Math.ceil(c);
runs--;
l = 0;
r = len - 1;
}
return Arrays.copyOf(copyArr, len);
}

public static void main(String[] args) {
System.out.println(Arrays.toString(foldArray(new int[]{1, 2, 3, 4, 5}, 3)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName Multiples3or5
Expand Down
2 changes: 1 addition & 1 deletion java/Multiply.java → javasolutions/Multiply.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName Multiply
Expand Down
2 changes: 1 addition & 1 deletion java/SortArray.java → javasolutions/SortArray.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName SortArray
Expand Down
2 changes: 1 addition & 1 deletion java/Square.java → javasolutions/Square.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName Square
Expand Down
2 changes: 1 addition & 1 deletion java/TwoSum.java → javasolutions/TwoSum.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

import java.util.HashMap;
import java.util.Map;
Expand Down
2 changes: 1 addition & 1 deletion java/ValidatePin.java → javasolutions/ValidatePin.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package java;
package javasolutions;

/**
* @ClassName ValidatePin
Expand Down

0 comments on commit 540a8fa

Please sign in to comment.