算法题总结
1,两数之和
题目:输入数组a[],目标值target,找到数组a中的两个数a[i],a[j],使得a[i]+a[j]=target.返回i,j
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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,二叉树的最大深度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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,二叉树的先序遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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,二叉树中序遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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,二叉树后序遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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,二叉树层序遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
1 2 3 4 5 6 7 8 9 10 11 12 | 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,反转链表
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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,返回合并后的有序数组
从后往前遍历,选择大的数进行填充
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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,合并两个有序链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本