![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/62506020e34b4b8628337e078d1b5fd0.png)
- 略
- 设计一个类,我们只能生成该类的一个实例。
public class B {
private static class BHodler{
private static final B INSTANCE = new B();
}
private static B getInstance(){
return BHodler.INSTANCE;
}
}
- 二维递增数组中的查找
public class C {
private static void find(int[][] array, int target) {
if (array == null || array.length == 0) {
return;
}
int row = 0;
int column = array[0].length - 1;
while (row < array.length && column >= 0) {
if (array[row][column] == target)
System.out.println(row + " " + column);
if (array[row][column] > target)
column--;
else
row++;
}
}
public static void main(String[] args) {
int[][] array = new int[][]{
{1, 2, 8, 9},
{2, 4, 9, 12},
{4, 7, 10, 13},
{6, 8, 11, 15}
};
find(array,10);
}
private void show(int[][] array) {
for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}
}
}
- 替换字符串中的空格
public class D {
private static void replaceSpace(String str) {
if (str == null)
return;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (String.valueOf(str.charAt(i)).equals(" ")) {
sb.append("%20");
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb);
}
public static void main(String[] args) {
replaceSpace("Hello My World");
}
}
- 从尾到头打印链表
public class E {
private static class Node {
Node(int value) {
this.value = value;
}
int value;
Node next;
}
private static void printListFromTail(Node head) {
if (head == null) {
return;
}
Stack<Node> stack = new Stack<>();
Node n = head;
while (n != null) {
stack.push(n);
n = n.next;
}
while (!stack.empty()) {
System.out.println(stack.pop().value);
}
}
private static void printListReverse(Node node) {
if (node != null) {
if (node.next != null) {
printListReverse(node.next);
}
System.out.println(node.value);
}
}
public static void main(String[] args) {
Node head = new Node(999);
Node tail = head;
for (int i = 0; i < 5; i++) {
Node node = new Node(i);
tail.next = node;
tail = node;
}
printListReverse(head);
}
}
- 重建二叉树
public class A {
private static class Node {
Node(int value) {
this.value = value;
}
int value;
Node left;
Node right;
}
private static Node constructTree(int[] pre, int[] in) {
if (pre == null || in == null || pre.length == 0 || in.length == 0)
return null;
if (pre.length != in.length)
return null;
Node root = new Node(pre[0]);
for (int i = 0; i < pre.length; i++) {
if (in[i] == pre[0]) {
root.left = constructTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
root.right = constructTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
}
}
return root;
}
public static void main(String[] args) {
int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
Node node = constructTree(pre, in);
show(node);
}
private static void show(Node node) {
if (node == null)
return;
show(node.left);
System.out.print(node.value + " ");
show(node.right);
}
}
- 用两个栈实现队列
public class B {
private static Stack<Integer> stack1 = new Stack<>();
private static Stack<Integer> stack2 = new Stack<>();
private static Integer pop() {
if (stack1 == null && stack2 == null)
return null;
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
private static void push(Integer i) {
stack1.push(i);
}
public static void main(String[] args) {
push(1);
push(2);
push(3);
push(4);
System.out.println(pop());
push(5);
System.out.println(pop());
push(6);
push(7);
System.out.println(pop());
System.out.println(pop());
System.out.println(pop());
System.out.println(pop());
System.out.println(pop());
}
}
- 旋转数组的最小数字
public class C {
private static int printMin(int[] array) {
if (array == null || array.length == 0) {
return -1;
}
if (array[0] < array[array.length - 1]) {
return array[0];
}
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] > array[mid + 1]) {
return array[mid + 1];
}
if (array[mid] < array[mid - 1]) {
return array[mid];
}
if (array[mid] > array[0]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
private static int printMin2(int[] array) {
if (array == null || array.length == 0) {
return -1;
}
int left = 0;
int right = array.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (array[mid] > array[right])
left = mid + 1;
else
right = mid;
}
return array[left];
}
public static void main(String[] args) {
int[] array = {1, 0, 1, 1, 1};
System.out.println(printMin(array));
System.out.println(printMin2(array));
}
}
- 斐波那契数列
public class D {
private static int fibonacci(int n) {
if (n <= 1)
return n <= 0 ? -1 : 1;
int result = 0;
int preone = 1;
int pretwo = 0;
for (int i = 2; i <= n; i++) {
result = preone + pretwo;
pretwo = preone;
preone = result;
}
return result;
}
private static int gua(int n) {
if (n <= 0)
return -1;
if (n < 2)
return n;
int result = 0;
int preone = 2;
int pretwo = 1;
for (int i = 3; i <= n; i++) {
result = preone + pretwo;
pretwo = preone;
preone = result;
}
return result;
}
private static int cover(int n) {
if (n <= 1)
return n <= 0 ? -1 : 1;
int number = 1;
int sum = 1;
while (n-- >= 2) {
sum += number;
number = sum - number;
}
return sum;
}
public static void main(String[] args) {
System.out.println(fibonacci(3));
System.out.println(gua(3));
System.out.println(cover(3));
}
}
- 二进制中1的个数
public class E {
private static int numberOf1(int num) {
int count = 0;
while (num != 0) {
count++;
num = num & (num - 1);
}
return count;
}
public static void main(String[] args) {
System.out.println(numberOf1(11));
}
}