![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/984ad8ffaa837347058215f73787184f.png#pic_center)
- 数值的整数次方
public class A {
private static double Power(double base, int ex) {
double result;
if (String.valueOf(base).equals("0.0")) {
return 0;
}
if (ex == 0) {
return 1.0;
}
if (ex > 0) {
result = mutiply(base, ex);
} else {
result = mutiply(1 / base, -ex);
}
return result;
}
private static double mutiply(double base, int ex) {
double sum = 1;
for (int i = 0; i < ex; i++) {
sum *= base;
}
return sum;
}
public static void main(String[] args) {
System.out.println(Power(0, -3));
}
}
- 打印1到最大的n位数
public class B {
private static void printMax(int n) {
int[] array = new int[n];
if (n <= 0) {
return;
}
printArray(array, 0);
}
private static void printArray(int[] array, int n) {
for (int i = 0; i < 10; i++) {
if (n != array.length) {
array[n] = i;
printArray(array, n + 1);
} else {
boolean isFirst0 = false;
for (int value : array) {
if (value != 0) {
System.out.print(value);
if (!isFirst0)
isFirst0 = true;
} else {
if (isFirst0)
System.out.print(value);
}
}
System.out.println();
return;
}
}
}
public static void main(String[] args) {
printMax(2);
}
}
- 在O(1)时间删除链表结点
public class C {
private static class Node {
Node(int value) {
this.value = value;
}
int value;
Node next;
}
private static void deleteNode(Node head, Node del) {
if (head == null || del == null)
return;
if (head == del) {
head.value = -1;
} else if (del.next != null) {
del.value = del.next.value;
del.next = del.next.next;
} else {
Node n = head;
while (n.next.next != null) {
n = n.next;
}
n.next = null;
}
}
public static void main(String[] args) {
Node head = new Node(888);
Node tail = head;
for (int i = 0; i < 5; i++) {
Node node = new Node(i);
tail.next = node;
tail = node;
}
Node del = head.next.next.next.next.next;
deleteNode(head, del);
Node n = head;
while (n != null) {
System.out.println(n.value);
n = n.next;
}
}
}
- 调整数组顺序使奇数位于偶数前面
public class D {
private static void reOrderArray(int[] array) {
if (array == null || array.length == 0)
return;
int left = 0;
int right = array.length - 1;
int flag;
while (left < right) {
while (left<right && array[left] % 2 != 0)
left ++;
while (left<right && array[right] % 2 == 0)
right --;
if (left<right){
flag = array[right];
array[right] = array[left];
array[left] = flag;
}
}
System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
int[] array = new int[]{1,2,3,4,5,6,7};
reOrderArray(array);
}
}
- 链表中倒数第k个结点
public class E {
private static class Node {
Node(int value) {
this.value = value;
}
int value;
Node next;
}
private static Node findKfromTail(Node head, int k) {
if (head == null || k == 0)
return null;
Node before = head;
Node after = head;
while (k-- > 1) {
if (before.next == null)
return null;
before = before.next;
}
while (before.next != null) {
before = before.next;
after = after.next;
}
return after;
}
public static void main(String[] args) {
Node head = new Node(777);
Node tail = head;
for (int i = 0; i < 5; i++) {
Node node = new Node(i);
tail.next = node;
tail = node;
}
System.out.println(findKfromTail(head, 2));
}
}
- 反转链表
public class A {
private static class Node {
Node(int value) {
this.value = value;
}
int value;
Node next;
}
private static Node reverse(Node head) {
if (head == null)
return null;
Node pre = null;
Node node = head;
Node next;
while (node != null) {
next = node.next;
node.next = pre;
pre = node;
node = next;
}
return pre;
}
public static void main(String[] args) {
Node head = new Node(666);
Node tail = head;
for (int i = 0; i < 5; i++) {
Node node = new Node(i);
tail.next = node;
tail = node;
}
Node n = reverse(head);
while (n != null) {
System.out.println(n.value);
n = n.next;
}
}
}
- 合并两个排序的链表
public class B {
private static Node mergeTwoLists1(Node node1, Node node2) {
if (node1 == null)
return node2;
if (node2 == null)
return node1;
if (node1.value < node2.value) {
node1.next = mergeTwoLists1(node1.next, node2);
return node1;
} else {
node2.next = mergeTwoLists1(node1, node2.next);
return node2;
}
}
private static Node mergeTwoLists2(Node node1, Node node2) {
Node newHead = new Node();
Node pre = newHead;
while (node1 != null && node2 != null) {
if (node1.value < node2.value) {
pre.next = node1;
node1 = node1.next;
} else {
pre.next = node2;
node2 = node2.next;
}
pre = pre.next;
}
pre.next = node1 == null ? node2 : node1;
return newHead.next;
}
public static void main(String[] args) {
Node.show(mergeTwoLists1(Node.getNode1(10), Node.getNode2(10)));
Node.show(mergeTwoLists2(Node.getNode1(10), Node.getNode2(10)));
}
}
- 树的子结构
public class C {
private static boolean hasSubTree(Tree source, Tree target) {
if (target == null)
return true;
if (source == null)
return false;
if (doesRoot1HasRoot2(source, target))
return true;
return hasSubTree(source.left, target) || hasSubTree(source.right, target);
}
private static boolean doesRoot1HasRoot2(Tree source, Tree target) {
if (source == null && target == null)
return true;
if (source == null || target == null)
return false;
if (source.value != target.value)
return false;
return doesRoot1HasRoot2(source.left, target.left) && doesRoot1HasRoot2(source.right, source.right);
}
public static void main(String[] args) {
Tree source = Tree.creat(new Tree());
System.out.println("------------");
Tree target = Tree.creat(new Tree());
System.out.println("------------");
if (hasSubTree(source,target))
System.out.println("yes");
else
System.out.println("no");
}
}
- 二叉树的镜像
public class D {
private static void MirrorTree1(Tree root) {
if (root == null || (root.left == null && root.right == null))
return;
Tree temp = root.left;
root.left = root.right;
root.right = temp;
if (root.left != null)
MirrorTree1(root.left);
if ((root.right != null))
MirrorTree1(root.right);
}
public static void main(String[] args) {
Tree source = Tree.creat(new Tree());
MirrorTree1(source);
Tree.show(source);
}
}
- 顺时针打印矩阵
public class E {
private static List spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
if (matrix == null || matrix[0].length == 0)
return null;
int r1 = 0, r2 = matrix[0].length - 1;
int c1 = 0, c2 = matrix.length - 1;
while (r1 <= r2 && c1 <= c2) {
for (int i = c1; i <= c2; i++) {
result.add(matrix[r1][i]);
}
for (int j = r1+1; j <= r2; j++) {
result.add(matrix[j][c2]);
}
if (r1 < r2 && c1 < c2) {
for (int i = c2-1; i > c1; i--) {
result.add(matrix[r2][i]);
}
for (int j = r2; j > r1; j--) {
result.add(matrix[j][c1]);
}
}
c1++;
r1++;
c2--;
r2--;
}
return result;
}
public static void main(String[] args) {
int[][] array = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
System.out.println(spiralOrder(array).toString());
}
}