![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法
算法
BetterCbx
这个作者很懒,什么都没留下…
展开
-
生成合法的括号
给定一个数字n,生成包含2n括号的合法序列n=3[((())), (()()), (())(), ()(()), ()()()]暴力法public List<String> generateParenthesis(int n ){ List<String> res=new ArrayList<>(); generateAll(new char[n*2],res,0); return res;}private void generateAll原创 2020-10-31 10:08:14 · 238 阅读 · 0 评论 -
树的前序遍历方法
递归package com.cbx;import com.cbx.LeetCode.树.TreeNode;import java.util.ArrayList;import java.util.List;/** * @Author:cbx * @Date:2020/10/27/11:25 */public class preOrder { public List<Integer> preorderTraversal(TreeNode root) {原创 2020-10-27 11:47:04 · 488 阅读 · 0 评论 -
整数转罗马数字
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。通常原创 2020-10-24 14:03:30 · 115 阅读 · 0 评论 -
查找共同的字符串
“bella”,“abe”,“roer”结果返回 epublic List<String> commonChars(String[] A) { int[] minfreq = new int[26]; Arrays.fill(minfreq, Integer.MAX_VALUE); for (String word: A) { int[] freq = new int[26]; int lengt原创 2020-10-24 13:38:42 · 462 阅读 · 0 评论 -
划分字符串--尽可能多的划分片段
字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表输入:S = “ababcbacadefegdehijhklij”输出:[9,7,8]解释:划分结果为 “ababcbaca”, “defegde”, “hijhklij”。每个字母最多出现在一个片段中。像 “ababcbacadefegde”, “hijhklij” 的划分是错误的,因为划分的片段数较少。public List<Integer>原创 2020-10-24 13:03:42 · 700 阅读 · 0 评论 -
首尾交换链表
1->2->3->4->5 ==> 1->5->2->4->3使用线性表将节点依次放入集合中,通过索引交换 /** * 将链表的节点放入顺序表中,这样方便获取最后一个节点 * 1->2->3->4 * 1->4->2->3 */ public void recordList(ListNode head){ if (head==null) return原创 2020-10-22 20:18:02 · 827 阅读 · 0 评论 -
两两交换链表中的节点
1->2->3->4->5 ==》 2->1->4->3->5非递归求解public ListNode swapPairs(ListNode head) { if (head==null||head.next==null) return head; ListNode pre=new ListNode(0); pre.next=head; ListNode temp=pre;原创 2020-10-22 20:13:24 · 70 阅读 · 0 评论 -
反转链表
给定一个链表进行反转1->2->3->4->nullnull->4->3->2->1双指针法 public ListNode reverseList2(ListNode head){ ListNode pre=null; ListNode cur=head; ListNode temp; while (cur!=null){ temp=cur.next原创 2020-10-22 13:20:49 · 150 阅读 · 0 评论 -
回文子串
https://leetcode-cn.com/problems/palindromic-substrings/输入:“aaa”输出:6解释:6个回文子串: “a”, “a”, “a”, “aa”, “aa”, “aaa”package com.cbx.LeetCode.动态规划;/** * @Author:cbx * @Date:2020/09/14/18:50 * https://leetcode-cn.com/problems/palindromic-substrings/solut原创 2020-10-19 22:20:26 · 63 阅读 · 0 评论 -
欧几里得算法(求最大公约数)
package com.cbx;/** * @Author:cbx * @Date:2020/10/16/18:36 * 求最大公约数的经典算法 */public class BiggestCommonDivisor { public static int commonDivisor(int m,int n){ if (n==0) return m; int r=m%n;原创 2020-10-16 18:42:18 · 283 阅读 · 0 评论