
算法与数据结构
一些数据结构和算法的总结
_长银_
静下心来,获取信息,思考方法,努力去做。
展开
-
5、排序算法
排序算法原创 2023-07-28 06:05:59 · 841 阅读 · 0 评论 -
1、数据存储基本概念(重要)
程序数据存储基本概念原创 2023-07-16 21:16:35 · 327 阅读 · 0 评论 -
6、常见的算法设计思路
前面我们讲了数据结构的类型以及基本的,这一节我们聊一下常见的算法设计思路:贪心、分治、动态规划、回溯、分支界限法。原创 2023-08-06 16:54:53 · 304 阅读 · 0 评论 -
2、算法好坏的衡量尺度
算法好坏的衡量尺度原创 2023-07-17 21:59:03 · 216 阅读 · 0 评论 -
4、非线性数据结构
非线性数据结构简介原创 2023-07-25 06:59:13 · 426 阅读 · 0 评论 -
3、线性数据结构
书桌上堆放的图书,如果我们不允许从中间抽取,那么当我们需要拿中间的一本书时,就只能先把上边的书拿到一边。这个就是“后进先出”,我们叫做栈。在C#中,如果我们要实现这些数据结构,可以使用数组、也可以使用链表,也可以使用既有的已经封装好的Queue类和Stack类。”联想,水平的,我们可以想到食堂打饭排的队伍,垂直的,我们可以联想到书桌上层叠摆放的书籍。至于其他的,比如双端队列等等,只是多加了些。至此,线性数据结构我们就聊的差不多了。线性数据结构,从名字可以看出,和“,并忽略方向时,我们发现这就是一些。原创 2023-07-23 13:08:58 · 309 阅读 · 0 评论 -
LeetCode 树-简单题 4个典例
个人感觉学会这4个典例基本就算把初级树题刷完了(主要是其中遍历手法)!So,Enjoy!二叉树的所有路径(带父信息)class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> res=new ArrayList<>(100); dfs(root,"",res); return res; }原创 2021-04-05 22:04:26 · 101 阅读 · 0 评论 -
13 罗马数字转整数
一开始使用Hashtable 来做这个题,提示编译出错,估计其编译的jdk版本在1.8以下:class Solution { public static int romanToInt(String s) { //基本思路:从右向左扫描,每次扫描两位,按对应量转换数值 Map<String, Integer> rules1 = new HashMap<S...原创 2019-01-12 23:18:05 · 401 阅读 · 0 评论 -
23 合并K个有序数组
利用21题合并两个有序数组的代码,使用for循环进行合并,效率较低;参照第一名的代码,使用分治,改变对数组的处理方法,可以大幅度提高处理效率:修改后:public ListNode mergeKLists(ListNode[] lists) { if(lists==null||lists.length==0)return null; return sort(lists, 0, l...原创 2019-01-19 08:41:44 · 2881 阅读 · 2 评论 -
从leetCode写题总结的程序优化思路
优化思路1:将子方法创建的缓存提升到父方法,作为子方法的参数,以减少创建所需要的时间和空间。优化思路2:当已知字符串格式的时候,使用subString()而不是split来进行拆分优化思路3:通过减少引用类型的访问次数,减少整体访问时间。...原创 2021-02-18 08:55:36 · 231 阅读 · 0 评论 -
12 整数转罗马数字
效率还行 class Solution { public static String intToRoman(int num) { if(num < 0 || num>3999) return null; char[] romaA = {' ','I','V','X','L','C','D','M'}; //{ 1 , 5 , 10, 50,100,500,...原创 2019-01-11 17:32:22 · 143 阅读 · 0 评论 -
347. 前 K 个高频元素
这道题就有点复杂了,我使用的方法比较慢,主要思路是使用链表存储前K个高频元素,使用尾删除的方式保存数据,后边会继续优化这部分内容。代码如下(方便大家调试着玩,就把main方法也粘上了):package easy;import java.util.HashMap;import java.util.Map;public class TopKFrequent { public static void main(String[] args) { new TopKFrequ原创 2021-05-13 20:06:18 · 82 阅读 · 0 评论 -
11 盛水最多的容器
每每看及官方解法,总羞愧难当,官方解法简洁易懂,何时才能达到这种水平呢?只有不断努力了。package leetCode;//官方解法class Solution { public int maxArea(int[] height) { int maxarea = 0, l = 0, r = height.length - 1; while (l < r...原创 2019-01-10 11:59:48 · 152 阅读 · 0 评论 -
24 两两交换链表中的节点
效率一般 class Solution { public static ListNode swapPairs(ListNode head) { if(head==null||head.next==null)return head; ListNode l = head; ListNode r = head.next; //...原创 2019-01-21 09:59:15 · 274 阅读 · 0 评论 -
238. 除自身以外数组的乘积
238. 除自身以外数组的乘积 题解源自yauldmarpublic int[] productExceptSelf(int[] nums) { int len = nums.length; int[] result = new int[len]; if (len == 0) { return result; } if (len == 1) { result[0] = 0;原创 2021-04-12 21:03:43 · 95 阅读 · 0 评论 -
2423. 删除字符使频率相同
leetcode 2423. 删除字符使频率相同原创 2022-10-12 07:54:13 · 303 阅读 · 0 评论 -
20 有效的括号
使用栈进行匹配即可class Solution { public static boolean isValid(String s) { if(s.isEmpty())return true; else { int slength = s.length(); int point = 0; Stack&...原创 2019-01-18 08:54:07 · 179 阅读 · 0 评论 -
290. 单词规律
leetcode 290. 单词规律原创 2022-10-10 22:56:19 · 111 阅读 · 0 评论 -
7 整数反转
我自己的想法(效率比较低) class Solution { public int reverse(int info) { if(info==0)return 0; long reverseLong=0; String intas = info+""; if(info<0) { String substring = intas.substrin...原创 2019-01-05 21:21:47 · 106 阅读 · 0 评论 -
10 正则表达式匹配
这题我没做出来,以下是我已经写得代码,保存一下:package leetCode;public class RegularEMatching { public static void main(String[] args) { //System.out.println(isMatch("mississippi", "mis*is*p*.")); //System.out.pr...原创 2019-01-10 09:19:58 · 255 阅读 · 0 评论 -
230. 二叉搜索树中第K小的元素
主要思路是:根据二叉搜索树中节点左小右大的特点,使用中序遍历的方式,即可顺序找到第k小元素。代码如下:package easy;import tree.TreeNode;public class KthSmallest { private TreeNode tarNode; private int t_k; public int kthSmallest(TreeNode root, int k) { //二叉树搜索树:左小右大 .原创 2021-05-18 20:30:58 · 118 阅读 · 0 评论 -
75. 颜色分类
取了个巧,不具备强适用性:public class SortColors { public static void main(String[] args) { new SortColors().sortColors(new int[]{2,0,2,1,1,0}); } public void sortColors(int[] nums) { int zeroCount=0; int oneCount=0; int原创 2021-05-12 19:14:11 · 91 阅读 · 0 评论 -
1909. 删除一个元素使数组严格递增
1909. 删除一个元素使数组严格递增原创 2022-10-13 07:14:19 · 123 阅读 · 0 评论 -
9 回文数
效率比较低 class Solution { public static boolean isPalindrome(int testNumber) { int info = testNumber; if(testNumber<0) return false; if(testNumber==0) return true; int...原创 2019-01-09 10:13:37 · 308 阅读 · 0 评论 -
150. 逆波兰表达式求值
一看就懂,用的栈class Solution { public int evalRPN(String[] tokens) { Stack<Integer> numStack=new Stack<>(); for (String token : tokens) { if (!isOperator(token)){ numStack.push(Integer.parseInt(token原创 2021-05-10 20:07:29 · 82 阅读 · 0 评论 -
219. 存在重复元素 II
leetcode 219. 存在重复元素 II原创 2022-10-11 07:20:36 · 85 阅读 · 0 评论 -
811.子域名访问计数
LeetCode 811.子域名访问计数原创 2022-10-05 12:25:12 · 121 阅读 · 0 评论 -
171. Excel表列序号
从慢到快,两次优化,如代码:package easy;import java.util.HashMap;import java.util.Map;public class TitleToNumber { public static void main(String[] args) { System.out.println(new TitleToNumber().titleToNumber("Z")); } public int titleToNumbe原创 2021-05-18 21:26:32 · 94 阅读 · 0 评论 -
160. 相交链表
不多说,上代码,一看就懂。主要思路就是 从链表尾往链表头看public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { int countA=getCount(headA); int countB=getCount(headB); int gap=Math.abs(countA-countB); if (c原创 2021-05-10 20:24:48 · 77 阅读 · 0 评论 -
215. 数组中的第K个最大元素
这道题主要思路是 根据k的值得大小来决定是从左边开始冒泡还是从后边开始冒泡,代码如下package easy;public class FindKthLargest { public static void main(String[] args) { System.out.println(new FindKthLargest().findKthLargest(new int[]{3,2,1,5,6,4},1)); } public int findKthLa原创 2021-05-16 20:48:42 · 71 阅读 · 0 评论 -
4. 寻找两个正序数组的中位数
思路:复制一半+1个数据,数字个数为偶数时,取末两位;否则取末位。代码如下:public double findMedianSortedArrays(int[] nums1, int[] nums2) { int tLen=nums1.length; int bLen=nums2.length; int count=nums1.length+nums2.length; boolean sign=count%2==0;原创 2021-09-16 21:26:58 · 85 阅读 · 0 评论 -
17 电话号码的字母组合
效率还行class Solution { public static List<String> letterCombinations(String digits) { if(digits.isEmpty())return new ArrayList<String>(); String[] num2 = {"a","b","c"}; String[]...原创 2019-01-15 10:17:10 · 183 阅读 · 0 评论 -
1800. 最大升序子数组和
取数组升序子序列和原创 2022-10-07 07:18:38 · 97 阅读 · 0 评论 -
67. 二进制求和
二进制求和原创 2022-10-07 08:12:47 · 106 阅读 · 0 评论 -
94. 二叉树的中序遍历
中序遍历:首先遍历左子树,然后访问根结点,最后遍历右子树。思路跟定义一致public class InorderTraversal { private List<Integer> result=new ArrayList<>(); public List<Integer> inorderTraversal(TreeNode root) { if (root!=null){ result.add(root.v原创 2021-05-12 18:29:30 · 82 阅读 · 0 评论 -
8 字符串转数字
这道题对于我而言难度比较大,示例里没有显示所有情况,整提交了20次,对代码做了重构,终于完成class Solution { public static int myAtoi(String str) { //如果该字符串第一个字符非有效整数字符、串为空、仅包含空白 返回0 if(str.isEmpty()) return 0; if(str.indexOf(".")!=-...原创 2019-01-07 13:52:49 · 469 阅读 · 0 评论 -
19 删除链表倒数第n个节点
采用双指针 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode re...原创 2019-01-17 13:40:23 · 124 阅读 · 0 评论 -
21 合并两个有序数组
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public static ListNode mer...原创 2019-01-18 10:05:58 · 187 阅读 · 0 评论 -
15 三数之和
超时的代码,我不咋会优化,请大神指教class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> numsR = new ArrayList<List<Integer>>(); L...原创 2019-01-14 15:08:33 · 170 阅读 · 0 评论 -
链表——两数相加
精简之后class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { //设置进位位,当前结果值位,设置当前节点 int high=0; int nowvalue=0; ListNode now1=l1,now2=l2; ...原创 2018-12-28 10:57:58 · 133 阅读 · 0 评论