第1题:打印九九乘法表
/* 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */ public class ArrayDemo00 { public static void main(String[] args) { for (int i = 1; i <= 9 ; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i + "*" + j + "=" + i*j + "\t"); } System.out.println(); } } }View Code
第2题:冒泡排序
import java.util.Arrays; public class ArrayDemo01 { // 冒泡排序 public static void main(String[] args) { int[] arr = {30,29,18,40,33}; boolean falg = false; for (int i = 0; i < arr.length; i++) { flag = true; for (int j = 0; j < arr.length-1-i; j++) { if (arr[j] > arr[j+1]){ int t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t; flag = false; } } if (flag){ break; } } System.out.println(Arrays.toString(arr)); } }View Code
第3题:二分查找
public class ArrayDemo05 { public static void main(String[] args) { int[] array = {10,20,30,40,50,60,70}; int ele = 40; System.out.println(getIndexByEle(array,ele)); } public static int getIndexByEle(int[] arr,int ele){ int minIndex=0; int maxIndex=arr.length-1; int cenIndex=(minIndex+maxIndex)/2; while (minIndex <= maxIndex){ if (ele == arr[cenIndex]){ return cenIndex; } else if(ele > arr[cenIndex]){ minIndex = cenIndex + 1; } else if(ele < arr[cenIndex]){ maxIndex = cenIndex - 1; } cenIndex = (minIndex+maxIndex)/2; } return -1; } }View Code
第4题:求素数
public class SuShu { public static void main(String[] args) { boolean flag; int m = 100; for(int i = 2; i < m;i++){ flag=true; for (int j = 2; j <= Math.sqrt(i); j++) { if (i%j == 0){ flag = false; break; } } if (flag == true){ System.out.println(i + " "); } } } }View Code
第5题:合并两个有序数组
import java.util.Arrays; // 合并两个有序数组 public class Demo03 { public static void main(String[] args) { int[] arr1 = {1,2,3,6}; int[] arr2 = {5,7,8,9}; System.out.println(Arrays.toString(merge(arr1,arr2))); } static int[] merge(int[] arr1, int[] arr2){ int x = 0; int y = 0; int z = 0; int[] result = new int[arr1.length + arr2.length]; while (x<arr1.length&&y<arr2.length){ if (arr1[x] <= arr2[y]){ result[z] = arr1[x]; x++; } else { result[z] = arr2[y]; y++; } z++; } // 判断哪一个数组被遍历到尾部,则此处将另一个数组添加到汇总数组中即可; // 此时 i=x,并i++,将M数组中剩余值添加到result中; if (x != arr1.length){ for (int i = x; i < arr1.length; i++) { result[z] = arr1[i]; z++; } }else { for (int i = y; i < arr2.length; i++) { result[z] = arr2[i]; z++; } } return result; } }View Code
第6题:输入12345,按位输出1-2-3-4-5
import java.util.Scanner; public class Demo02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入正整数:"); int num = scanner.nextInt(); rePrint(num); } static void rePrint(int num){ if (num > 9){ rePrint( num / 10); } System.out.println(num % 10 + " "); } }View Code
第7题:数组反转
import java.util.Arrays; // 数组反转 public class ArrayDemo03 { public static void main(String[] args) { int[] arr = {10,20,30,40,50,60}; System.out.println(Arrays.toString(reArray(arr))); } public static int[] reArray(int[] arrays){ int[] result = new int[arrays.length]; for (int i = 0,j=result.length-1; i < arrays.length; i++,j--) { result[j] = arrays[i]; } return result; } }View Code
第8题:链表反转
public class Solution { public ListNode ReverseList(ListNode head) { ListNode p = new ListNode(0); p.next = null; while(head != null){ ListNode tmp = head.next; head.next = p.next; p.next = head; head = tmp; } return p.next; } }View Code
第9题:统计一个字符串中数字出现的个数
public class Que2 { public static void main(String[] args) { String s = "123004533"; char[] array = s.toCharArray(); int[] count = new int[10]; // 存放个数 for (int i = 48; i < 58; i++) { int a = 0; for (int j = 0; j < array.length; j++) { if (array[j] == i){ a++; } } count[i-48] = a; } for (int i = 0; i < count.length; i++) { System.out.println(i + "的个数是:"+count[i]); } } }View Code
第10题:字符串压缩
public class Solution3 { public static void main(String[] args) { String str = "aaabcdworr"; System.out.println(compressString(str));; } static String compressString(String S) { if (S.length() == 0) { // 空串处理 return S; } StringBuffer ans = new StringBuffer(); int cnt = 1; char ch = S.charAt(0); for (int i = 1; i < S.length(); ++i) { if (ch == S.charAt(i)) { cnt++; } else { ans.append(ch); ans.append(cnt); ch = S.charAt(i); cnt = 1; } } ans.append(ch); ans.append(cnt); if (ans.length() >= S.length()){ return S; } else { return ans.toString(); } // return ans.length() >= S.length() ? S : ans.toString(); } }View Code
第11题:判断两个字符串是否是旋转词
public class Rotation { public boolean chkRotation(String A, int lena, String B, int lenb) { // write code here if(lena!=lenb) return false; String AA = A + A; return AA.contains(B); } //测试 public static void main(String[] args) { String text = "PVNMCXZNZBETCM"; String pattern = "CNTZXMNZEBMVCd"; Rotation rotation = new Rotation(); System.out.println(rotation.chkRotation(text,text.length(),pattern,pattern.length())); } }View Code
持续更新中……