The fibonacci sequence is a famous bit of mathematics, and it happens to have a recursive definition. The first two values in the sequence are 0 and 1 (essentially 2 base cases). Each subsequent value is the sum of the previous two values, so the whole sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. Define a recursive fibonacci(n) method that returns the nth fibonacci number, with n=0 representing the start of the sequence.
Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
Given an array of ints, compute recursively the number of times that the value 11 appears in the array. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.
array11([1, 2, 11], 0) → 1
array11([11, 11], 0) → 2
array11([1, 2, 3, 4], 0) → 0
Why use index, how to delete an element in an array?
FUNCTION Factorial(n : INTEGER) RETURNS INTEGER
IF n = 0
THEN
Result ← 1 // This is the base case
ELSE
Result ← n * Factorial(n – 1) // This is the general case
ENDIF
RETURN Result
ENDFUNCTION
public int factorial(int n) {
if (n == 1){
return n;
}
return n * factorial(n - 1);
}
public int fibonacci(int n) {
if (n == 2){
return 1;
}
else if (n == 1){
return 1;
}
else if (n ==0){
return 0;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
public int count7(int n) {
if (n <= 1){
return 0;
}
if (n % 10 == 7){
return 1 + count7(n/10);
} else{
return count7(n/10);
}
}
public String changeXY(String str) {
if (str.length() == 0){
return "";
}
if (str.startsWith("x")){
return "y" + changeXY(str.substring(1));
}else{
return str.substring(0,1) + changeXY(str.substring(1));
}
}
public int count11(String str) {
if (str.length() <= 1){
return 0;
}
if (str.startsWith("11")){
return 1 + count11(str.substring(2));
}else {
return count11(str.substring(1));
}
}
public int array11(int[] nums, int index) {
if (nums.length == index){
return 0;
}
if (nums[index] == 11){
return 1 + array11(nums, index + 1);
}else{
return array11(nums, index + 1);
}
}
import java.util.Arrays;
public class DeleteElementInArray {
public static int[] deleteElement(int[] arr, int index){
int[] arr_new = new int[arr.length-1];
for(int i=0;i<arr_new.length;i++){
// 判断元素是否越界
if (index < 0 || index >= arr.length) {
throw new RuntimeException("元素越界... ");
}
if(i<index) {
arr_new[i] = arr[i];
}
else {
arr_new[i] = arr[i+1];
}
}
return arr_new;
}
public static void main(String[] args) {
int[] arr = {1,2,2,3,4,5,6,7};
System.out.println(Arrays.toString(deleteElement(arr, 2)));
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Count11ArrayList {
public static int count11(ArrayList<Integer> arr){
if (arr.size() == 0){
return 0;
}
if (arr.get(0).equals(11)){
arr.remove(0);
return 1 + count11(arr);
}else{
arr.remove(0);
return count11(arr);
}
}
public static void main(String[] args) {
int[] x = {1,2,11,5,11,11};
List<Integer> xx = Arrays.stream(x).boxed().collect(Collectors.toList());
System.out.println(count11(new ArrayList<Integer>(xx)));
}
}