算法题总结
1,两数之和
题目:输入数组a[],目标值target,找到数组a中的两个数a[i],a[j],使得a[i]+a[j]=target.返回i,j
private static int[] towNumSum(int[] a,int target) { int result[] = new int[2]; Map<Integer,Integer> map = new HashMap<>(); for (int i=0;i<a.length;i++) { int another = target-a[i]; //如果map中没有想要的数,就将该数放入map if (map.get(another)==null) { map.put(a[i],i); }else { result[0]=i; result[1]=map.get(another); } } return result; }
2,二叉树的最大深度
public static int getMaxDepth(TreeNode root) { if (root==null) return 0; else return Math.max(getMaxDepth(root.getRightChild()),getMaxDepth(root.getLeftChild()))+1; } public static int getMaxDepth1(TreeNode root) { if (root==null) return 0; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); int size = 0; int depth = 0; while (!queue.isEmpty()) { size = queue.size(); while (size>0) { TreeNode node = queue.poll(); if (node.getLeftChild()!=null) { queue.add(node.getLeftChild()); } if (node.getRightChild()!=null) { queue.add(node.getLeftChild()); } size--; } depth++; } return depth; }
3,二叉树的先序遍历
private static void preOrder(TreeNode root) { if (root==null) { return; } System.out.printf(root.getValue()+" "); preOrder(root.getLeftChild()); preOrder(root.getRightChild()); } private static void preOrder1(TreeNode root) { Deque<TreeNode> stack = new LinkedList<>(); while (!stack.isEmpty()||root!=null) { while (root!=null)//如果根节点不为null,就将值打印,并将该节点入栈,继续遍历左孩子 { System.out.printf(root.getValue()+" "); stack.push(root); root=root.getLeftChild(); } //当根节点及其左孩子节点处理完毕后,从栈中取出元素,遍历右孩子 root=stack.pop(); root=root.getRightChild(); } }
4,二叉树中序遍历
private static void middleOrder(TreeNode root) { if (root==null) { return; } middleOrder(root.getLeftChild()); System.out.printf(root.getValue()+" "); middleOrder(root.getRightChild()); } private static void middleOrder1(TreeNode root) { Deque<TreeNode> stack = new LinkedList<>(); while (!stack.isEmpty()||root!=null) { while (root!=null)//寻找最左节点 { stack.push(root); root=root.getLeftChild(); } //将左孩子出栈,然后打印 root=stack.pop(); System.out.printf(root.getValue()+" "); root=root.getRightChild(); } }
5,二叉树后序遍历
private static void afterOrder(TreeNode root) { if (root==null) { return; } afterOrder(root.getLeftChild()); afterOrder(root.getRightChild()); System.out.printf(root.getValue()+" "); } private static void afterOrder1(TreeNode root) { Deque<TreeNode> stack = new LinkedList<>(); TreeNode preAcccess=null; while (!stack.isEmpty()||root!=null) { while (root!=null)//寻找最左节点 { stack.push(root); root=root.getLeftChild(); } //将左孩子出栈,然后打印 root=stack.pop(); //如果没有右节点,或者右节点已经访问过了,就打印 if (root.getRightChild()==null||root.getRightChild()==preAcccess) { System.out.printf(root.getValue()+" "); preAcccess = root; root=null; }else { stack.push(root); root=root.getRightChild(); } } }
6,二叉树层序遍历
private static void order(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); System.out.printf(node.getValue()+" "); if (node.getRightChild()!=null) { queue.add(node.getLeftChild()); } if (node.getLeftChild()!=null) { queue.add(node.getRightChild()); } } }
7,删除排序列表的重复元素
题目:给一个升序链表,去除重复的元素
1,2,3,4,5,5,6,6——>1,2,3,4,5,6
private static void clearRepetitionNum(LinkedNode list) { LinkedNode currentNode=list; while (currentNode.getNext()!=null) { if (currentNode.getValue()==currentNode.getNext().getValue()) { //将下下一个节点,作为下一个节点 currentNode.setNext(currentNode.getNext().getNext()); }else { //该节点向下遍历 currentNode=currentNode.getNext(); } } }
8,删除排序数组的重复元素
题目:给一个数组,去除重复的元素,返回去重后数组的长度
1,2,3,4,5,5,6,6——>1,2,3,4,5,6
private static int clearRepetitionNum(int[] arr) { int pre=0; for (int after=1;after<arr.length;after++) { if (arr[pre]!=arr[after]) { pre++; arr[pre]=arr[after]; } } return pre+1; }
9,反转链表
public static LinkedNode reverse(LinkedNode head) { if (head==null) return null; LinkedNode pre = new LinkedNode(); LinkedNode nodeA=head,nodeB=head; while (nodeB!=null) { nodeB=nodeA.getNext(); nodeA.setNext(pre.getNext()); pre.setNext(nodeA); nodeA=nodeB; } return pre.getNext(); }
10,合并两个有序数组
题目:输入有序数组a[],有序数组b[],和元素个数m,n,返回合并后的有序数组
从后往前遍历,选择大的数进行填充
private static int[] marge(int[] a,int m,int[] b,int n) { int k=m+n; for (int index=k-1,num1Index=m-1,num2Index=n-1;index>=0;index--) { if (num1Index<0) //数组a的元素取完了 { a[index]=b[num2Index--]; }else if (num2Index<0)//数组b的元素取完了 { break; } else if (a[num1Index]>b[num2Index])//数组a的元素大 { a[index]=a[num1Index--]; } else//数组b的元素大 { a[index]=b[num2Index--]; } } return a; }
11,合并两个有序链表
private static LinkedNode marge(LinkedNode l1, LinkedNode l2) { if (l1==null) return l2; if (l2==null) return l1; LinkedNode result = new LinkedNode(); LinkedNode pre=result; while (l1!=null&&l2!=null) { if (l1.getValue()<=l2.getValue()) { pre.setNext(l1); l1=l1.getNext(); }else { pre.setNext(l2); l2=l2.getNext(); } pre = pre.getNext(); } if (l1 !=null) pre.setNext(l1); if (l2 !=null) pre.setNext(l2); return result.getNext(); }