
算法基础
文章平均质量分 62
逃离ntydr
这个作者很懒,什么都没留下…
展开
-
排序算法(自用)
各类排序算法比较排序时间复杂度O(nlogn) ~ O(n^2)主要有:冒泡排序,选择排序,插入排序,归并排序,堆排序,快速排序等。冒泡排序实现:比较相邻的元素。如果第一个比第二个大,就交换它们两个;对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;针对所有的元素重复以上的步骤,除了最后一个;重复步骤1~3,直到排序完成。优点:实现简单,n较小时性能较好代码:public static void bubbleSort(int[] arr) {原创 2021-10-13 17:30:58 · 161 阅读 · 0 评论 -
两数之和+LUR
牛客简单题1.两数之和使用hashmap解决 public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> m = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (m.get(target - nums[i]) != null) {原创 2021-10-11 20:55:44 · 140 阅读 · 0 评论 -
关于二进制的算法
二进制中1的个数输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。public int NumberOf1(int n) { int count=0; while(n!=0){ count++; n=n&(n-1); } return count; } 如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数原创 2021-09-28 09:11:14 · 411 阅读 · 0 评论 -
Divide and Conquer15
标题原创 2021-07-03 11:14:38 · 87 阅读 · 0 评论 -
Duplicate Calculation in Recursion14
Duplicate Calculation in RecursionMemoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. (Source: wikipedia原创 2021-06-30 18:36:31 · 131 阅读 · 0 评论 -
Principle of Recursion13
Principle of Recursion原创 2021-06-26 11:12:31 · 91 阅读 · 0 评论 -
Conclusion12
ConclusionConstruct Binary Tree from Inorder and Postorder TraversalGiven two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the原创 2021-06-25 19:50:05 · 96 阅读 · 0 评论 -
Solve Tree Problems Recursively11
Solve Tree Problems RecursivelyMaximum Depth of Binary TreeA binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.class Solution { int max=0; private void maximum_depth(TreeN原创 2021-06-15 17:18:14 · 84 阅读 · 0 评论 -
Binary Tree Preorder Traversal10
a原创 2021-06-14 09:44:34 · 97 阅读 · 0 评论 -
Doubly Linked List09
Doubly Linked ListDesign Doubly Linked ListDesign your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: val and next. val is the value of the current node,原创 2021-06-10 11:35:53 · 141 阅读 · 0 评论 -
Reverse Linked List08
Reverse Linked ListGiven the head of a singly linked list, reverse the list, and return the reversed list.原创 2021-06-10 09:48:31 · 85 阅读 · 0 评论 -
single linked list&Two Pointer Technique
single linked list原创 2021-06-01 17:54:45 · 80 阅读 · 0 评论 -
Conclusion
ConclusionHeight CheckerA school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected原创 2021-05-31 08:27:45 · 116 阅读 · 0 评论 -
in-Place Operations
in-Place OperationsReplace Elements with Greatest Element on Right Sidepublic int[] replaceElements(int[] arr) { for (int i=0;i<arr.length;i++){ int Gnum=-1; for (int j=i+1;j<arr.length;j++){ if(arr[j原创 2021-05-27 12:53:08 · 117 阅读 · 0 评论 -
Searching for Items in an Array
ArrayCheck If N and Its Double ExistGiven an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).public boolean checkIfExist(int[] arr) { boolean flag=false; for (int a :原创 2021-05-26 09:45:10 · 85 阅读 · 0 评论 -
Deleting Items Froms an Array
ArrayRemove ElementGiven an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.T原创 2021-05-25 17:36:09 · 105 阅读 · 0 评论 -
Inserting Items Into an Array
ArrayDuplicate ZerosGiven a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.Note that elements beyond the length of the original array are not written.public void duplicateZeros(int[]原创 2021-05-24 12:11:56 · 94 阅读 · 0 评论 -
Introduction01
ArrayMax Consecutive OnesGiven a binary array nums, return the maximum number of consecutive 1’s in the array. public int findMaxConsecutiveOnes(int[] nums) { int count = 0; int[] result =new int[nums.length]; int原创 2021-05-23 16:26:39 · 137 阅读 · 0 评论 -
java-TreeMap
java-TreeMap问题来源TreeMap的实现是红黑树算法的实现1.先了解红黑树a.红黑树是一颗自平衡的排序二叉树。b.节点为红色或黑色的平衡二叉树c.具体规则d.红黑树的三大操作:左旋,右旋,着色2.TreeMap数据结构TreeMap的一个重要属性比较器a.如题,构造一个比较器new TreeMap<>();则使用默认的比较器b.也可new TreeMap<>(Comparator<? super K> comparator);则使原创 2021-02-10 11:22:56 · 177 阅读 · 0 评论 -
数据结构简单脑图
数据结构简单脑图https://kdocs.cn/l/ccjT1uZQtYfY[金山文档] React.pof原创 2020-12-30 19:52:40 · 333 阅读 · 0 评论