一、集合去重
使用HashMap,效率最高,原因是HasnMap的原理实现。
二、链表
1、定义链表节点
public class Node {
public String data;
public Node next;
}
2、删除链表指定节点
思路:判断临界点,是不是头尾节点,需要特殊处理一下
正常节点,删除当前节点的下一个节点,需要把下一个节点的值赋给当前节点
public void deleteNode(Node head, Node node) {
if (head == node) {//node是头结点
head = null;
} else if (node.next == null) {//node是尾节点
while (head.next != node) {
head = head.next;
}
head.next = null;
} else {
Node temp = node.next;
node.data = temp.data;
node.next = temp.next;
}
}
3、单链表删除重复节点
public void deleteDouble(Node head){
if(head==null||head.next==null){
return;
}
Node pre=head;
Node cur=head.next;
Set<String> keySet=new HashSet<>();
while(cur!=null){
if(keySet.contains(cur.data)){
pre.next=cur.next;
}else{
keySet.add(cur.data);
pre=cur;
}
cur=cur.next;
}
}
4、单链表反转
public Node reverseList(Node head){
if(head==null||head.next==null){
return head;
}
Node cNode=head;
Node preNode=null;
while(cNode.next!=null){
Node temp=cNode.next;
cNode.next=preNode;
preNode=cNode;
cNode=temp;
}
return preNode;
}
5、回文
public boolean isPalindroml(Node head) {
if (head == null) {
return false;
}
Stack<Node> stack = new Stack<>();
Node cNode = head;
while (cNode != null) {
stack.push(cNode);
cNode = cNode.next;
}
while (head.next != null) {
if (head.next.data != stack.pop().data) {
return false
}
head = head.next;
}
return true;
}
三、用栈实现队列
public class linkedQueue{
Stack<Node> stack1=new Stack();
Stack<Node> stack2=new Stack();
public void push(Node node){
stack1.push(node);
}
public void pop(){
if(!stack2.isEmpty()){
stack2.pop();
}else{
if(!stack1.isEmpty()){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
}
}
}
四、遍历view的所有view(遍历文件)
1、递归
public void traverse(View view) {
if (view == null) {
return;
}
ArrayList list=new ArrayList();
if (view instanceof ViewGroup) {
list.add(view);
ViewGroup temp = (ViewGroup) view;
for (int i = 0; i < temp.getChildCount(); i++) {
traverse(temp.getChildAt(i));
}
} else {
list.add(view);
}
}
2、广度搜索(queue辅助)
/**
* 广度搜索
*
* @param view
*/
public void traverseGuang(View view) {
if (view == null) {
return;
}
View temp = null;
ViewGroup tempViewGroup = null;
LinkedList<View> myQueue = new LinkedList<>();
myQueue.push(view);
while (!myQueue.isEmpty()) {
temp = myQueue.poll();
list.add(temp);
if (temp instanceof ViewGroup) {
tempViewGroup = (ViewGroup) temp;
for (int i = 0; i < tempViewGroup.getChildCount(); i++) {
myQueue.push(tempViewGroup.getChildAt(i));
}
}
}
}
五、字符串
(1)查看一个字符串中最大回文的长度
public static int getNum(String str){
if(str==null){
return 0;
}
int max=0;
for(int i=0;i<str.length()-1;i++){
//回文是单数
for(int j=0;i-j>=0&&i+j<str.length();j++){
//i-j代表靠近左边距离为j的字符,i+j同理。
if(str.charAt(i-j)!=str.charAt(i+j)){
break;
}else{
max=j*2+1>max?j*2+1:max;
}
}
for(int j=0;i-j>=0&&i+j+1<str.length();j++){
if(str.charAt(i-j)!=str.charAt(i+j+1)){
break;
}else{
max=j*2+2>max?j*2+2:max;
}
}
}
System.out.println(""+max);
return max;
}
六、排序
排序算法 | 平均速度 | 最坏情况 | 是否稳定 |
冒泡排序 | O( n^2) | O( n^2) | 稳定 |
快速排序 | O(nlogn) | O( n^2) | 不稳定 |
选择排序 | O( n^2) | O( n^2) | 不稳定 |
插入排序 | O( n^2) | O( n^2) | 稳定 |
堆排序 | O(nlogn) | O(nlogn) | 不稳定 |
Shell排序 | O( n^(3/2) ) | O( n^2) | 不稳定 |
合并排序 | O(nlogn) | O(nlogn) | 稳定 |
(1)冒泡排序
通过相邻的两个数的比较, 根据需要决定是否将两个数互换位置, 然后将比较往前(或往后)推进
public static void order(int[] arr){
if(arr==null || arr.length==0){
return;
}
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;
}
}
}
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
(2)选择排序
"选择排序"就是第0个逐步和后面全部的比,比完0位置就得到最小的数,紧接着再从1位置对比后面的元素,以此类推,逐步得到从小到大的值.
public static void order(int[] arr){
if(arr==null || arr.length==0){
return;
}
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
(3)快速排序
思路:一个数组,先确定基准数,一般是数组的第一个元素;定义变量j,先从右往左找一个小于基准数的数(j--),定义变量i,再从左往右找一个大于基准数的数(i++),然后交换他们,直至i=j,然后交换基准数和下标为i位置的数。这个数组就分成了两部分有序的,再依次遍历。
public static void order(int[] arr,int left,int right){
if(left>=right){
return;
}
int key=arr[left];
int i=left;
int j=right;
while(i<j){
while(arr[j]>=key&&i<j){
j--;
}
while(arr[i]<=key&&i<j){
i++;
}
if(i<j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
arr[left]=arr[i];
arr[i]=key;
order3(arr,left,i-1);
order3(arr,i+1,right);
}
(4)折半查找
public static int getIndex(int[] arr,int num){
if(arr==null || arr.length==0){
return -1;
}
int middle=0;
int max=arr.length-1;
int min=0;
while(min<=max){
middle=(min+max)/2;
if(num>arr[middle]){
min=middle+1;
}else if(num<arr[middle]){
max=middle-1;
}else{
System.out.println(""+middle);
return middle;
}
}
return -1;
}
(5)数组倒叙
public static void reverse(int[] arr){
if(arr==null||arr.length==0){
return;
}
int length=arr.length;
for(int i=0;i<legnth/2;i++){
int temp=arrp[i];
arr[i]=arr[length-i-1];
arr[length-i-1]=temp;
}
}