一、使用函数求最大值
创建方法求两个数的最大值max2,随后再写一个求3个数的最大值的函数max3。
要求:在max3这个函数中,调用max2函数,来实现3个数的最大值计算。
public class Test {
public static int maxTwo(int x1,int x2){
return Math.max(x1,x2);
}
public static int maxThree(int x1,int x2,int x3){
return Math.max(maxTwo(x1,x2),x3);
}
public static void main(String[] args) {
System.out.println(maxTwo(5, 7));
System.out.println(maxThree(5,7,9));
}
}
二、求N的阶乘
public class Test {
public static int N(int n){
int result = 1;
for (int i = 1; i < n+1; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println(N(4)); //4321
}
}
三、求阶乘和
public class Test {
public static void main(String[] args) {
System.out.println(NN(4));
}
public static int NN(int n){
int result = 0;
for (int i = 1; i < n+1; i++) {
result += N(i);
}
return result;
}
public static int N(int n){
int result = 1;
for (int i = 1; i < n+1; i++) {
result *= i;
}
return result;
}
}
四、斐波那契数列第N项
public class Test {
public static void main(String[] args) {
System.out.println(fb(5));
}
public static int fb(int n){
if (n == 1 || n == 2){
return 1;
}
int x1 = 1;
int x2 = 1;
int temp = 0;
for (int i = 0; i < n-1; i++) {
temp = x1 + x2;
x1 = x2;
x2 = temp;
}
return temp;
}
}
五、求最大值重载
public class Test {
public static void main(String[] args) {
System.out.println(max(5, 7));
System.out.println(max(5.1, 4.3, 6.8));
}
public static int max(int x1,int x2){
return Math.max(x1,x2);
}
public static double max(double x1,double x2,double x3){
return (x1>x2?x1:x2)>x3?(x1>x2?x1:x2):x3;
}
}
六、递归求解汉诺塔
class Solution{
ArrayList<String> result = new ArrayList<>();
public void hanno(String left,String mid,String right,int n){
if (n == 0){
return ;
}
hanno(left,right,mid,n-1); //1.把N-1个盘子从left借助right移动到mid上
String t = " move from " + left + " to " + right; //2.把第N个盘子从left移动到right上
result.add(t);
hanno(mid,left,right,n-1); //3.把mid上的N-1个盘子从mid借助right移动到left上
}
public ArrayList<String> getSolution(int n ){
hanno("left","mid","right",n);
return result;
}
}
public class Test {
public static void main(String[] args) {
Solution s = new Solution();
ArrayList<String> solution = s.getSolution(3);
for (String s1 : solution) {
System.out.println(s1);
}
}
}
七、奇数位于偶数之前
调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序。
如数组:[1,2,3,4,5,6]
调整后可能是:[1, 5, 3, 4, 2, 6]
public class Test2 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6};
System.out.println(Arrays.toString(oneToTwo(arr)));
}
public static int[] oneToTwo(int[] arr){
//双指针,从左从右遍历,左为偶停住,右为奇停住,交换
int i = 0;
int j = arr.length -1;
while (i < j){
if (arr[i] % 2 == 1){
i ++;
continue;
}
if (arr[j] % 2 == 0){
j --;
continue;
}
if (arr[i] % 2 == 0 && arr[j] % 2 == 1){
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
return arr;
}
}
八、二分查找
public class Test2 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10,11};
System.out.println(search(arr, 7));
}
public static int search(int[] arr,int k){
int i = 0;
int j = arr.length-1;
int mid = 0;
while (i < j){
mid = (i + j) / 2;
if (arr[mid] > k){
j = mid;
}else if (arr[mid] < k) {
i = mid;
}else {
return mid;
}
}
return mid;
}
}
九、两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
public class Test2 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10,11};
int target = 21;
System.out.println(Arrays.toString(twoAdd(arr, 21)));
}
public static int[] twoAdd(int[] arr,int target){
int[] result = new int[2];
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length ; j++) {
if (arr[i] + arr[j] == target){
result[0] = i;
result[1] = j;
return result;
}
}
}
return null;
}
}
十、只出现一次的数字
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
输入:[2,2,1]
输出:1
public class Test2 {
public static void main(String[] args) {
int[] arr = {1, 1, 3, 4, 4, 2, 2};
System.out.println(oneTimes(arr));
}
public static int oneTimes(int[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean judge = false;
for (int j = 0; j < arr.length; j++) {
if (i == j) {
continue;
}
if (arr[i] == arr[j]) {
judge = true;
break;
}
}
if (!judge) {
return arr[i];
}
}
return -1;
}
}
十一、多数元素
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
public class Test2 {
public static void main(String[] args) {
int[] arr = {1, 1, 1, 1, 4, 2, 2};
System.out.println(more(arr));
}
public static int more(int[] arr){
int middle = arr.length / 2;
Arrays.sort(arr);
return arr[middle];
}
}
十二、存在连续三个奇数的数组
public class Test2 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 7, 9,10};
System.out.println(isContinueOdd(arr));
}
public static boolean isContinueOdd(int[] arr){
int i = 0;
int j = 1;
for (int k = 2; k < arr.length; k++) {
if ((arr[i]%2==1) && (arr[j]%2==1) && (arr[k]%2==1)){
return true;
}
i ++;
j ++;
}
return false;
}
}