-2.比较两个字符串ascii
package com.ke.inter;
//比较两个字符串ascii
public class interTest {
public static void main(String[] args) throws Exception {
String str1 = "0bcZ";
String str2 = "1bcc";
int res = get(str1, str2);
System.out.println(res);
}
private static int get(String str1, String str2) throws Exception {
if (str1 == null || str2 == null) {
throw new Exception("str1或str2为空");
}
if (str1.length() > str2.length()) {
return 1;
} else if (str1.length() < str2.length()) {
return -1;
}
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
int l = 0;
while (l<str1.length()){
if (chars1[l] > chars2[l]) {
System.out.println("ASCII:"+ (int) chars1[l]);
return 1;
} else if (chars1[l] < chars2[l]) {
System.out.println("ASCII:"+ (int) chars1[l]);
return -1;
}
l++;
}
return 0;
}
}
-1.与运算(&)、或运算(|)、异或运算(^)
一:与运算符(&)
预算规则:
0&0=0;0&1=0;1&0=0;1&1=1
即:两个同时为1,结果为1,否则为0
例如:3&5
十进制3转为二进制的3:0000 0011
十进制5转为二进制的5:0000 0101
------------------------结果:0000 0001 ->转为十进制:1
即:3&5 = 1
二:或运算(|)
运算规则:
0|0=0; 0|1=1; 1|0=1; 1|1=1;
即 :参加运算的两个对象,一个为1,其值为1。
例如:3|5 即 00000011 | 0000 0101 = 00000111,因此,3|5=7。
三:异或运算符(^)
运算规则:0^0=0; 0^1=1; 1^0=1; 1^1=0;
即:参加运算的两个对象,如果两个位为“异”(值不同),则该位结果为1,否则为0。
例如:3^5 = 0000 0011 | 0000 0101 =0000 0110,因此,3^5 = 6
0.二叉树
https://blog.csdn.net/u011212394/article/details/86750285
ArrayDeque
https://www.cnblogs.com/mfrank/p/9600137.html
求二叉树的最长路径非递归算法
https://blog.csdn.net/rekeless/article/details/82663767
1.排序
1.1 冒泡排序
package sort;
/**
* Created by david on 2018/8/16
* 冒泡排序
*/
public class BubbleSort {
private static int[] bubbleSort(int[] a) {
int len = a.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1-i; j++) {
if (a[j + 1] < a[j]) {
swap(a, j + 1, j);
}
}
}
return a;
}
//交换方法
private static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
//测试
public static void main(String[] args) {
int[] a = {66, 4, 6, 8, 99, 9, 2, 99};
int[] sort = bubbleSort(a);
for (int s : sort) {
System.out.print(s + " ");
}
}
}
1.2快速排序
package sort;
/**
* Created by david on 2018/8/16
* 快速排序
* 不稳定,时间复杂度 最理想 O(nlogn) 最差时间O(n^2)
*/
public class QuickSort {
private static int[] quickSort(int[] a, int low, int high) {
//中心点
int mid = 0;
if (low < high) {
mid = partition(a, low, high);
quickSort(a, low, mid - 1);
quickSort(a, mid + 1, high);
}
return a;
}
private static int partition(int[] a, int low, int high) {
int b = a[low];
while (low < high) {
while (low < high && a[high] >= b) {
high--;
}
a[low] = a[high];
while (low < high && a[low] <= b) {
low++;
}
a[high] = a[low];
}
a[low] = b;
return low;
}
//测试
public static void main(String[] args) {
int[] a = {1, 14, 6, 8, 99, 9, 2, 99};
int[] sort = quickSort(a, 0, 7);
for (int s : sort) {
System.out.print(s + " ");
}
}
}
1.3 二分查找
package sort;
/**
* Created by david on 2018/8/16
* 查找前的数据必须是已经排好序的, 然后得到数组的开始位置start和结束位置end,
* 取中间位置mid的数据a[mid]跟待查找数据key进行比较, 若 a[mid] > key, 则取end = mid - 1;
* 若 a[mid] < key, 则取start = mid + 1; 若 a[mid] = key 则直接返回当前mid为查找到的位置.
* 依次遍历直到找到数据或者最终没有该条数据
*/
public class BinarySearch {
public static int binarySearch(int[] a, int key) {
int start = 0;
int end = a.length - 1;
int mid = -1;
while (start <= end) {
mid = (start + end) / 2;
if (a[mid] == key) {
return mid;
} else if (a[mid] > key) {
end = mid - 1;
} else if (a[mid] < key) {
start = mid + 1;
}
}
return -1;
}
// 测试
public static void main(String[] args) {
int[] a = {1, 4, 6, 8, 99};
int i = binarySearch(a, 99);
System.out.println(i);
}
}
1.3 String与Array转换
import java.util.Arrays;
/**
* Created by david on 2018/8/16
* String/Array转换
*/
public class Convert {
public static void main(String[] args) {
String str = "we are family";
//String转成Array
char[] chars = str.toCharArray();
//排序
Arrays.sort(chars);
//转成String
String s = Arrays.toString(chars);
//根据index获得char
char c = str.charAt(7);
//长度
str.length();
int length = chars.length;
//子串
String substring1 = str.substring(1, 4);
String substring2 = str.substring(3);
//int转string
Integer integer = Integer.valueOf("3");
//string转int
String value = String.valueOf(3);
}
}
1.4 单链表反转
public class Node {
//为了方便,这两个变量都使用public,而不用private就不需要编写get、set方法了。
//存放数据的变量,简单点,直接为int型
public int data;
//存放结点的变量,默认为null
public Node next;
//构造方法,在构造时就能够给data赋值
public Node(int data){
this.data = data;
}
}
package link;
/**
* Created by david on 2018/8/16
* 单链表反转
*/
public class NodeRe {
public static void main(String[] args) {
Node head = new Node(0);
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
head.setNext(node1);
node1.setNext(node2);
node2.setNext(node3);
// 调用反转方法
head = reverse(head);
// 打印反转后的结果
while (null != head) {
System.out.print(head.getData() + " ");
head = head.getNext();
}
}
public static Node reverse(Node head){
// head看作是前一结点,head.getNext()是当前结点,
// reHead是反转后新链表的头结点
if(head == null || head.getNext() == null){
// 若为空链或者当前结点在尾结点,则直接还回
return head;
}
Node reHead = reverse(head.getNext());
// 将当前结点的指针域指向前一结点
head.getNext().setNext(head);
// 前一结点的指针域令为null;
head.setNext(null);
// 反转后新链表的头结点
return reHead;
}
}
1.5 双向链表反转
1.6 多线程