6. Moves the first element of an array to the end of the array, which we call the rotation of the array.
A) use ArrayList to store elements
public class Solution {
public static int Minnumberarray (int[] array) {
int min = array[0];
for (int i = 0; i < Array.Length; i++) {
if (min > Array[i]) {
min = Array[i];
}
}
return min;
}
Public ArrayList minnumberinrotatearray (int [] array) {
ArrayList list = new ArrayList ();
int minnum = Minnumberarray (array);
int i;
for (i=0; i < array.length; i++) {
if (Minnum = = Array[i]) {
Break
}
}
Put the elements after the minimum and minimum numbers into the list
for (int j = i; J < Array.Length; J + +) {
List.add (Array[j]);
}
Put the elements in front of the minimum number in the list
for (int k = 0;k < I; k++) {
List.add (Array[k]);
}
return list;
}
}
A) use an array to hold the rotated array
public class Solution {
public static int Minnumberarray (int[] array) {
int min = array[0];
for (int i = 0; i < Array.Length; i++) {
if (min > Array[i]) {
min = Array[i];
}
}
return min;
}
Public int[] Minnumberinrotatearray (int [] array) {
int[] arr = new Int[array.length];
int minnum = Minnumberarray (array);
int i;
for (i=0; i < array.length; i++) {
if (Minnum = = Array[i]) {
Break
}
}
int l = 0;
Put the elements after the minimum and minimum numbers into the list
for (int j = i; j < Array.Length; j++,l++) {
ARR[L] = Array[j];
}
Put the elements in front of the minimum number in the list
for (int k = 0;k < I; k++,l++) {
ARR[L] = array[k];
}
return arr;
}
public static void Main (string[] args) {
Solution s = new solution ();
int[] arr1 = new int[]{3,4,5,1,2};
int[] arr2 = S.minnumberinrotatearray (arr1);
for (int i = 0; i < arr2.length; i++) {
System.out.print (Arr2[i]);
}
}
}
7. We all know that the Fibonacci sequence now requires you to enter an integer n, and you output the nth of the Fibonacci sequence. n<=39.
A) in the title, the value of n is less than or equal to 39, so we can define an array of length 39 so that it does not exceed the size. It is also much more efficient to use arrays to hold Fibonacci numbers than to use recursive methods. The program does not determine whether the input number is greater than 39, this is very simple.
public class Solution {
public int Fibonacci (int n) {
int flag;
int[] arr = new int[39];
Arr[0] = 1;
ARR[1] = 1;
if (n<=0) {
Flag = 0;
}else if (n = = 1) {
flag = 1;
}else{
for (int i = 2; i < n; i++) {
Arr[i] = Arr[i-1] + arr[i-2];
}
flag = arr[n-1];
}
return flag;
}
public static void Main (string[] args) {
Solution s = new solution ();
System.out.println (S.fibonacci (20));
}
}
b) Another way is to use recursion, the code of this method is very simple, but the time is a bit long, not through the online compiler test.
public class Solution {
public int Fibonacci (int n) {
int flag;
if (n<=0) {
Flag = 0;
}else if (n = = 1) {
flag = 1;
}else{
Flag = Fibonacci (n-1) +fibonacci (n-2);
}
return flag;
}
public static void Main (string[] args) {
Solution s = new solution ();
System.out.println (S.fibonacci (20));
}
}
8. A frog can jump up to 1 steps at a time, or jump up to level 2. Ask the frog to jump on an n-level step with a total number of hops.
A) This is a Fibonacci sequence problem
It can be concluded that when n equals 1 o'clock to 1,n equals 2 o'clock to 2,n equals 3 o'clock to 3,n equals 4 o'clock to 5, it is possible to observe that when N is greater than 2 o'clock, the result equals n-1 result plus n-2 result, so it can be implemented directly using recursive return.
public class Solution {
public int Jumpfloor (int target) {
if (target <= 0) {
return-1;
} else if (target = = 1) {
return 1;
} else if (target = = 2) {
return 2;
} else{
Return (Jumpfloor (target-1) +jumpfloor (target-2));
}
}
public static void Main (string[] args) {
Solution s = new solution ();
System.out.println (S.jumpfloor (3));
}
}
9. A frog can jump up to 1 steps at a time, or jump up to level 2 ... It can also jump on n levels. Ask the frog to jump on an n-level step with a total number of hops.
A) The problem-solving steps are similar to the previous one, as well as the Fibonacci sequence.
public class Solution {
public int Jumpfloor (int target) {
if (target <= 0) {
return-1;
} else if (target = = 1) {
return 1;
} else if (target = = 2) {
return 2;
} else{
Return (Jumpfloor (target-1) +jumpfloor (target-2));
}
}
public static void Main (string[] args) {
Solution s = new solution ();
System.out.println (S.jumpfloor (3));
}
}
10. We can use the small rectangle of 2*1 to cover the larger rectangle horizontally or vertically. What is the total number of ways to cover a large rectangle of 2*n with n 2*1 small rectangles without overlapping?
A) This question is similar to the previous two questions, the following code is given directly
public class Solution {
public int rectcover (int target) {
if (target <= 0) {
return 0;
} else if (target = = 1) {
return 1;
} else if (target = = 2) {
return 2;
} else{
Return Rectcover (target-1) +rectcover (target-2);
}
}
public static void Main (string[] args) {
Solution s = new solution ();
System.out.println (S.rectcover (4));
}
}
Java Algorithm applet (2)