开始各种笔试了,从明天起会高频不定期的更新各种题,java的,flag在这里立起来!一起加油!
1、分解质因数
import java.util.Scanner;
public class Prime_Factorization {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
prime_factorization(n);
in.close();
}
public static void prime_factorization(int n) {
if (n < 2) {
throw new RuntimeException("参数错误");
}
for(int i = 2;i * i < n;i++) {
while(n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if(n != 1) {
System.out.println(" " + n);
}
}
}
2、输出1~n之间的所有素数
import java.util.Scanner;
public class Print_Prime {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
print_prime(n);
in.close();
}
public static void print_prime(int n) {
if(n < 2) {
return;
}
System.out.print(2 + " ");
for(int i = 3; i < n;i += 2) {
int j;
for(j = 2;j < Math.sqrt(i);j++) {
if(i % j == 0) {
break;
}
}
if(j == (int)Math.sqrt(i) + 1) {
System.out.print(i + " ");
}
}
}
}
3、快速排序
public class QuickSort {
public static void main(String[] args) {
int[] arr = {11,55,3,4,7,99,5};
new QuickSort().quickSort(arr,0,arr.length - 1);
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i] + " ");
}
}
public void quickSort(int[] arr,int L,int R) {
int i = L;
int j = R;
int pivot = arr[(L + R) >> 1];
while(i <= j) {
while(arr[i] < pivot) {
i++;
}
while(arr[j] > pivot) {
j--;
}
if(i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if(j > L) {
quickSort(arr, L, j);
}
if (i < R) {
quickSort(arr, i, R);
}
}
}
4、插入排序
public class InsertSort {
public static void main(String[] args) {
int[] arr = {11,55,3,4,7,99,5};
new InsertSort().insertSort(arr);
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i] + " ");
}
}
public void insertSort(int[] arr) {
for(int i = 1;i < arr.length;i++) {
int value = arr[i];
int j;
for(j = i - 1;j >= 0;j--) {
if (value < arr[j]) {
arr[j + 1] = arr[j];
}else if(value >= arr[j]) {
break;
}
}
arr[j + 1] = value;
}
}
}
5、选择排序
public class SelectSort {
public static void main(String[] args) {
int[] arr = {11,55,3,4,7,99,5};
new SelectSort().selectSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public void selectSort(int[] arr) {
for(int i = 0;i < arr.length - 1;i++) {
int minIndex = i;
for(int j = i + 1;j < arr.length;j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
6、冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {11,55,3,4,7,99,5};
new BubbleSort().bubbleSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public void bubbleSort(int[] arr) {
for(int i = 0;i < arr.length - 1;i++) {
for(int j = 0;j < arr.length - i - 1;j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
7、二分查找(递归与非递归)
public class BinarySearch{
public static void main(String[] args) {
int[] arr = {3,4,5,7,11,55,99};
int n1 = new BinarySearch().binarySearch1(arr,55);
int n2 = new BinarySearch().binarySearch2(arr,0,arr.length,99);
System.out.println(n1);
System.out.println(n2);
}
public int binarySearch1(int[] arr,int k) {
int i = 0;
int j = arr.length - 1;
while(i <= j) {
int mid = (i + j) >> 1;
if (arr[mid] == k) {
return mid;
}else if (arr[mid] < k) {
i = mid + 1;
}else {
j = mid - 1;
}
}
return -1;
}
public int binarySearch2(int[] arr,int left,int right,int k) {
if (left > right) {
return -1;
}
int mid = (left + right) >> 1;
if(arr[mid] == k) {
return mid;
}else if(arr[mid] < k) {
return binarySearch2(arr, mid + 1, right, k);
}else {
return binarySearch2(arr, left, mid - 1, k);
}
}
}
8、堆排序
public class HeapSort {
public static void main(String[] args) {
int[] arr = {11,55,3,4,7,99,5};
heapSort(arr);
}
public static void heapSort(int[] arr) {
for(int i = (arr.length - 1) / 2;i >= 0;i--) {
heapOne(arr,arr.length,i);
}
int n = arr.length;
while(n > 0) {
System.out.print(arr[0] + " ");
arr[0] = arr[n - 1];
n--;
heapOne(arr, n, 0);
}
}
public static void heapOne(int[] arr,int n,int k) {
int k1 = 2 * k + 1;
int k2 = 2 * k + 2;
if(k1 >= n && k2 >= n) {
return;
}
int a1 = Integer.MAX_VALUE;
int a2 = Integer.MAX_VALUE;
if(k1 < n) {
a1 = arr[k1];
}
if(k2 < n) {
a2 = arr[k2];
}
if (arr[k] <= a1 && arr[k] <= a2) {
return;
}
if (a1 < a2) {
int temp = arr[k];
arr[k] = arr[k1];
arr[k1] = temp;
heapOne(arr, n, k1);
}else {
int temp = arr[k];
arr[k] = arr[k2];
arr[k2] = temp;
heapOne(arr, n, k2);
}
}
}
9、反转单链表
class Node{
int value;
Node next;
public Node(int data) {
this.value = data;
}
}
public class ReverseSignleList {
public static void main(String[] args) {
Node node = new Node(5);
node.next = new Node(4);
node.next.next = new Node(3);
node.next.next.next = new Node(2);
node.next.next.next.next = new Node(1);
Node head = reverse(node);
while(head != null) {
System.out.print(head.value + " ");
head = head.next;
}
}
public static Node reverse(Node head) {
Node cur = null;
Node next = null;
while(head != null) {
next = head.next;
head.next = cur;
cur = head;
head = next;
}
return cur;
}
}
10、反转双向链表
class DoubleNode{
int value;
DoubleNode pre;
DoubleNode next;
public DoubleNode(int data) {
this.value = data;
}
}
public class ReverseDoubleList {
public static void main(String[] args) {
DoubleNode node = new DoubleNode(5);
node.next = new DoubleNode(4);
node.pre = null;
node.next.next = new DoubleNode(3);
node.next.pre = node;
node.next.next.next = new DoubleNode(2);
node.next.next.pre = node.next;
node.next.next.next.next = new DoubleNode(1);
node.next.next.next.pre = node.next.next;
node.next.next.next.next.next = null;
DoubleNode head = reverse(node);
DoubleNode tail = null;
while(head != null) {
System.out.print(head.value + " ");
tail = head;
head = head.next;
}
System.out.println();
while(tail != null) {
System.out.print(tail.value + " ");
tail = tail.pre;
}
}
public static DoubleNode reverse(DoubleNode head) {
DoubleNode pre = null;
DoubleNode next = null;
while(head != null) {
next = head.next;
head.next = pre;
head.pre = next;
pre = head;
head = next;
}
return pre;
}
}
11、用一个栈实现另一个栈的排序
import java.util.Stack;
public class SortStackByStack {
Stack<Integer> stackData = null;
Stack<Integer> stackHelp = null;
public SortStackByStack() {
stackData = new Stack<>();
stackHelp = new Stack<>();
stackData.push(3);
stackData.push(4);
stackData.push(5);
stackData.push(2);
stackData.push(6);
stackData.push(7);
}
public static void main(String[] args) {
SortStackByStack s = new SortStackByStack();
s.sortStackByStack();
while(!s.stackData.isEmpty()) {
System.out.print(s.stackData.pop() + " ");
}
}
public void sortStackByStack() {
while(!stackData.isEmpty()) {
int cur = stackData.pop();
if (stackHelp.isEmpty() || cur >= stackHelp.peek()) {
stackHelp.push(cur);
}else {
while(!stackHelp.isEmpty() && stackHelp.peek() > cur) {
stackData.push(stackHelp.pop());
}
stackHelp.push(cur);
}
}
while(!stackHelp.isEmpty()) {
stackData.push(stackHelp.pop());
}
}
}
12、设计一个具有getMin()功能的栈(两种方式)
import java.util.Stack;
public class StackWithGetMin {
public static void main(String[] args) {
MyStack1 stack1 = new MyStack1();
stack1.push(5);
stack1.push(6);
stack1.push(2);
stack1.push(3);
stack1.push(7);
stack1.push(1);
System.out.println("当前最小值:" + stack1.getMin());
System.out.println("弹出:" + stack1.pop());
System.out.println("当前最小值" + stack1.getMin());
MyStack1 stack2 = new MyStack1();
stack2.push(5);
stack2.push(6);
stack2.push(2);
stack2.push(3);
stack2.push(7);
stack2.push(1);
System.out.println("当前最小值:" + stack2.getMin());
System.out.println("弹出:" + stack2.pop());
System.out.println("当前最小值" + stack2.getMin());
}
}
class MyStack1{
Stack<Integer> stackData = null;
Stack<Integer> stackMin = null;
public MyStack1() {
stackData = new Stack<>();
stackMin = new Stack<>();
}
public void push(int data) {
if (stackMin.isEmpty()) {
stackMin.push(data);
}else if (data < stackMin.peek()) {
stackMin.push(data);
}
stackData.push(data);
}
public int pop() {
if (stackData.isEmpty()) {
throw new RuntimeException("栈为空");
}
int value = stackData.pop();
if (value == stackMin.peek()) {
stackMin.pop();
}
return value;
}
public int getMin() {
return stackMin.peek();
}
}
class MyStack2{
Stack<Integer> stackData = null;
Stack<Integer> stackMin = null;
public MyStack2() {
stackData = new Stack<>();
stackMin = new Stack<>();
}
public void push(int data) {
if (stackMin.isEmpty()) {
stackMin.push(data);
}else if (data < stackMin.peek()) {
stackMin.push(data);
}else if(data > stackMin.peek()) {
stackMin.push(stackMin.peek());
}
stackData.push(data);
}
public int pop() {
if (stackData.isEmpty()) {
throw new RuntimeException();
}
stackMin.pop();
return stackData.pop();
}
public int getMin() {
return stackMin.peek();
}
}
13、单例设计模式
class SignleTon{
private static SignleTon instance = null;
private SignleTon() {}
public static SignleTon getInstance() {
if (instance == null) {
instance = new SignleTon();
}
return instance;
}
}
public class Signleton {
private static Signleton instance = new Signleton();
private Signleton() {}
public static Signleton getInstance() {
return instance;
}
}
class Signleton1{
private static Signleton1 instance = null;
private Signleton1() {}
public static Signleton1 getInstance() {
if (instance == null) {
instance = new Signleton1();
}
return instance;
}
}
class Signleton2{
private static Signleton2 instance = null;
private Signleton2() {}
public static synchronized Signleton2 getInstance() {
if (instance == null) {
instance = new Signleton2();
}
return instance;
}
}
class Signleton3{
private static Signleton3 instance = null;
private Signleton3() {}
public static Signleton3 getInstance() {
if (instance == null) {
synchronized (Signleton3.class) {
if (instance == null) {
instance = new Signleton3();
}
}
}
return instance;
}
}
class Signleton4{
private static class SignletonHolder{
private static final Signleton4 INSTANCE = new Signleton4();
}
private Signleton4() {}
public static final Signleton4 getInstance() {
return SignletonHolder.INSTANCE;
}
}