![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法刷题
陈皮tang
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 68 - II. 二叉树的最近公共祖先
class Solution { /** * 二叉树的最近公共祖先 * 思路: * 三种情况: * 1、p q 一个在左子树 一个在右子树 那么当前节点即是最近公共祖先 * 2、p q 都在左子树 * 3、p q 都在右子树 * @param root * @param p * @param q * @return */ public TreeNode...原创 2021-11-24 11:35:31 · 317 阅读 · 0 评论 -
剑指 Offer 67. 把字符串转换成整数
class Solution { public int strToInt(String str) { List<Character> list = new ArrayList<>(); boolean flag = true;//1 + 0 - boolean start = false;//截取已经开始 for(int i=0;i<str.length();i++){ ...原创 2021-11-24 10:49:04 · 100 阅读 · 0 评论 -
最长和谐子序列
class Solution { public int findLHS(int[] nums) { Arrays.sort(nums); int begin = 0; int res = 0; for (int end = 0; end < nums.length; end++) { while (nums[end] - nums[begin] > 1) { ...原创 2021-11-20 10:58:13 · 191 阅读 · 0 评论 -
面试题50. 第一个只出现一次的字符(哈希表 / 有序哈希表,清晰图解)
class Solution { public char firstUniqChar(String s) { Map<Character, Boolean> dic = new LinkedHashMap<>(); char[] sc = s.toCharArray(); for(char c : sc) dic.put(c, !dic.containsKey(c)); for...原创 2021-11-18 11:44:21 · 223 阅读 · 0 评论 -
剑指 Offer 49. 丑数
方法一:最小堆初始化一个空堆,先将最小丑数1放入优先队列(堆顶元素最小),取出堆顶元素x(最小),将2x,3x,5x压入堆,使用set进行去重操作,2x,3x,5x在set中不存在时,将值压入priorityQueue,和set,第n次取的是第n个丑数。class Solution { public int nthUglyNumber(int n) { PriorityQueue<Long> heap = new PriorityQueue<Long>原创 2021-11-18 10:24:47 · 47 阅读 · 0 评论 -
剑指 Offer 38. 字符串的排列(回溯法,清晰图解)Krahets发布于 2020-03-30192.3k
class Solution { List<String> res = new LinkedList<>(); char[] c; public String[] permutation(String s) { c = s.toCharArray(); dfs(0); return res.toArray(new String[res.size()]); } void df...原创 2021-11-17 15:04:43 · 127 阅读 · 0 评论 -
剑指 Offer 36. 二叉搜索树与双向链表(中序遍历,清晰图解)
class Solution { private Node head ; private Node pre ; public Node treeToDoublyList(Node root) { if(root==null) return null; pre = null; dfs(root); head.left = pre; pre.right = head; ...原创 2021-11-14 17:27:58 · 196 阅读 · 0 评论 -
剑指 Offer 35. 复杂链表的复制(哈希表 / 拼接与拆分,清晰图解)
class Solution {public: Node* copyRandomList(Node* head) { Node* cur = head; Node* dum = new Node(0), *pre = dum; while(cur != nullptr) { Node* node = new Node(cur->val); // 复制节点 cur pre->...原创 2021-11-13 12:03:07 · 273 阅读 · 0 评论 -
剑指 Offer 24. 反转链表(迭代 / 递归,清晰图解)
class Solution { public ListNode reverseList(ListNode head) { ListNode cur = head, pre = null; while(cur != null) { ListNode tmp = cur.next; // 暂存后继节点 cur.next cur.next = pre; // 修改 next 引用指向 ...原创 2021-11-07 16:55:53 · 67 阅读 · 0 评论 -
剑指 Offer 17. 打印从1到最大的n位数
输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。class Solution { public int[] printNumbers(int n) { int MAX_VALUE = 0; for(int i=0;i<n;i++){ MAX_VALUE = MAX_VALUE*10+9; } int[] resu.原创 2021-11-06 15:02:48 · 43 阅读 · 0 评论 -
347. 前 K 个高频元素
class Solution { public int[] topKFrequent(int[] nums, int k) { HashMap<Integer,Integer> map = new HashMap<>();//数--频率 for(int i=0;i<nums.length;i++){ Integer count = map.get(nums[i]); if(count==.原创 2021-09-15 14:52:10 · 39 阅读 · 0 评论 -
18. 四数之和
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for(int i=0;i<=nums.length-1;i++){ //这...原创 2021-09-15 09:54:48 · 45 阅读 · 0 评论 -
15. 三数之和
解题思路: 暴力法搜索为 O(N3) 时间复杂度,可通过双指针动态消去无效解来优化效率。 双指针法铺垫: 先将给定 nums 排序,复杂度为 O(NlogN)。 双指针法思路: 固定 3 个指针中最左(最小)数字的指针 k,双指针 i,j 分设在数组索引 (k,len(nums))(k, len(nums))(k,len(nums)) 两端,通过双指针交替向中间移动,记录对于每个固定指针 k 的所有满足 nums[k] + nums[i] + nums[j] == 0 的...原创 2021-09-15 09:21:01 · 45 阅读 · 0 评论 -
383. 赎金信
ransomNote中的字符一定在magazine中,如果不在一定false;magazine中的某个字符只能用一次,但是可以有重复;遍历magazine,建立一个hashtable,(数组),位置是对应hash值,值是出现次数。遍历ransomNote,如果(>0)在hashtable中,对应值减一,如果不在(<=0)false;遍历万都在就是true。class Solution { public boolean canConstruct(String r...原创 2021-09-13 17:17:32 · 43 阅读 · 0 评论 -
454. 四数相加 II
// 本题解题步骤: // 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。 // 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。 // 定义int变量count,用来统计a+b+c+d = 0 出现的次数。 // 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 // 最后返回统计值 co...原创 2021-09-13 16:57:16 · 46 阅读 · 0 评论 -
剑指 Offer 48. 最长不含重复字符的子字符串
class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() <= 0) { return 0; } int leftIndex = -1; int result = 0; Map<Character, Integer> leftIndexMap = n.原创 2021-09-12 09:37:36 · 45 阅读 · 0 评论 -
82. 删除排序链表中的重复元素 II
// 具体地,我们从指针 cur 指向链表的哑节点,随后开始对链表进行遍历。如果当前 cur.next与 cur.next.nextcur.next.next 对应的元素相同,那么我们就需要将 cur.next以及所有后面拥有相同元素值的链表节点全部删除。我们记下这个元素值 x,随后不断将 cur.next 从链表中移除,直到 cur.next为空节点或者其元素值不等于 x为止。此时,我们将链表中所有元素值为 x 的节点全部删除。// 如果当前 cur.next 与 cur.next.nex...原创 2021-09-12 08:16:20 · 143 阅读 · 0 评论 -
24. 两两交换链表中的节点
使用迭代法:如果链表中至少有两个节点,则在两两交换链表中的节点之后,原始链表的头节点变成新的链表的第二个节点,原始链表的第二个节点变成新的链表的头节点。链表中的其余节点的两两交换可以递归地实现。在对链表中的其余节点递归地两两交换之后,更新节点之间的指针关系,即可完成整个链表的两两交换。用 head 表示原始链表的头节点,新的链表的第二个节点,用 newHead 表示新的链表的头节点,原始链表的第二个节点,则原始链表中的其余节点的头节点是 newHead.next。令 head.next = s...原创 2021-09-08 15:06:32 · 136 阅读 · 0 评论 -
八大排序算法
插入排序基本思想:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。 算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。 代码: public static void insertionSort(int[] array){ int tmp; for(int i=1;i<array.length;i++){ t原创 2021-09-03 09:38:26 · 71 阅读 · 0 评论 -
236. 二叉树的最近公共祖先(DFS ,清晰图解)235. 二叉搜索树的最近公共祖先
//后续遍历,自带回朔,从下倒上class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //从root向下递归,如果遇到根节点或者p,q就返回 if(root == null || root == p || q == root) return root; // 在ro...原创 2021-09-02 17:30:28 · 146 阅读 · 0 评论 -
617合并二叉树
使用层次遍历// class Solution {// public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {// if(root1 == null && root2 == null) return null;// if(root1!=null&&root2==null) return root1;// if(root2!=null&a...原创 2021-09-01 14:44:18 · 37 阅读 · 0 评论 -
112. 路径总和
class Solution { public boolean hasPathSum(TreeNode root, int targetSum){ if(root==null) return false; if(root.left==null&&root.right==null && 0==targetSum-root.val) return true; if(root.left==null&&ro...原创 2021-09-01 10:09:06 · 50 阅读 · 0 评论