![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 58
嘤嘤怪QQQ
这个作者很懒,什么都没留下…
展开
-
二叉树三种遍历方式 非递归
import java.util.*;public class BinaryTree { static class Node{ Node left; Node right; int val; public Node (int val,Node left, Node right) { this.left = left; this.right = right; th原创 2021-03-08 15:16:35 · 71 阅读 · 0 评论 -
leetcode-TOP100—树
文章目录leetcode 94 二叉树的中序遍历leetcode 96 不同的二叉搜索树leetcode 98 验证二叉搜索树leetcode 101 对称二叉树leetcode 94 二叉树的中序遍历/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} *原创 2021-03-07 19:13:36 · 183 阅读 · 0 评论 -
OJ 有9个因子的数
有9个因数的数DescriptionFind the count of numbers less than N having exactly 9 divisors1<=T<=1000,1<=N<=10^12InputFirst Line of Input contains the number of testcases. Only Line of each testcase contains the number of members N in the rival gang原创 2021-03-07 17:02:45 · 147 阅读 · 0 评论 -
OJ最小交换次数
文章目录最小交换次数最小交换次数最小交换次数DescriptionGiven an array of N distinct elementsA[ ],find the minimum number of swaps required to sort the array.Your are required to complete the function which returns an integer denoting the minimum number of swaps,required原创 2021-03-07 16:52:39 · 161 阅读 · 0 评论 -
OJ数学公式——乘方运算以及取模技巧
import java.util.Scanner;/** * 实现Implement pow(A, B) % C.In other words, given A, B and C, find (A^B)%C * */public class Main { static Scanner in=new Scanner(System.in); public static void main(String args[]){ int T=in.nextInt();原创 2021-03-06 22:48:04 · 353 阅读 · 0 评论 -
OJ序号乘方
import java.util.Scanner;/*序号乘方DescriptionThere are Infinite People Standing in a row,indexed from 1.A person having index 'i' has strength of (i*i).You have Strength 'P'.You need to tell what is the maximum number of People You can Kill With your原创 2021-03-06 22:37:12 · 223 阅读 · 1 评论 -
OJ图遍历
文章目录广度优先遍历深度优先遍历(遍历所有情况,求最大深度,其实是回溯)广度优先遍历/** * Output * * 输出遍历顺序,用空格隔开 * * * Sample Input 1 * * 1 * 4 a * a b c d * a 0 1 1 0 * b 1 0 1 0 * c 1 1 0 1 * d 0 0 1 0 * Sample Output 1 * * a b c d */public class Main { static Scanner s原创 2021-03-06 22:35:31 · 179 阅读 · 0 评论 -
OJ回溯
文章目录分配问题分配问题分配问题Description对给定的n个任务与n个人之间的成本矩阵完成成本最低的任务分配策略。Input输入:第一行为用例个数,之后为每一个用例;用例的第一行为任务个数,即n;用例的第二行为使用逗号隔开的人员完成任务的成本;每一个成本描述包括人员序号、任务序号和成本,使用空格隔开。人员序号和任务序号都是从1到n的整数,序号出现的次序没有固定规则。Output输出:每一个用例输出一行,从序号为1的人员开始,给出其分配的任务序号,使用空格隔开;使用逗号将多个解隔开。原创 2021-03-06 22:24:26 · 253 阅读 · 2 评论 -
OJ分治
文章目录最近对问题最近对问题Description最近对问题:使用分治算法解决最近对问题。Input第一行为测试用例个数。后面每一行表示一个用例,一个用例为一些平面上点的集合,点与点之间用逗号隔开,一个点的两个坐标用空格隔开。坐标值都是正数。Output对每一个用例输出两个距离最近的点(坐标使用空格隔开),用逗号隔开,先按照第一个坐标大小排列,再按照第二个坐标大小排列。如果有多个解,则按照每个解的第一个点的坐标排序,连续输出多个解,用逗号隔开。Sample Input 111 1,2 2原创 2021-03-06 22:07:06 · 205 阅读 · 1 评论 -
OJ二叉树层次遍历相关
文章目录数据结构层次遍历层次遍历换行打印层次遍历分行打印且每行排好序Z型打印数据结构public class BinaryTree { static class Node{ Node left; Node right; int val; public Node(Node left, Node right, int val) { this.left = left; this.right原创 2021-03-06 21:18:01 · 173 阅读 · 0 评论 -
OJ多个连续区间查找第n个位置的元素
Searching_3DescriptionThey declared Sonam as bewafa.Although she is not, believe me!She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries.All the students who原创 2021-03-06 20:06:21 · 121 阅读 · 0 评论 -
OJ排序
文章目录冒泡排序插入排序希尔排序计数排序冒泡排序import java.util.Scanner;/** * 13 24 3 56 34 3 78 12 29 49 84 51 9 100 */public class Main { public static void main(String args[]){ Scanner sc=new Scanner(System.in); int N=sc.nextInt(); int[] nu原创 2021-03-06 19:33:57 · 494 阅读 · 0 评论 -
LeetCode
[LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity...原创 2020-01-03 21:59:23 · 295 阅读 · 0 评论 -
Leetcode-1
Leetcode -1/*Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not ...原创 2020-01-08 22:11:21 · 104 阅读 · 0 评论 -
leetcode-2
leetcode-2/*You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers...原创 2020-01-08 22:12:36 · 106 阅读 · 0 评论 -
leetcode-3
leetcode-3/*Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Examp...原创 2020-01-08 22:13:48 · 123 阅读 · 0 评论 -
leetcode-4
leetcode-4/*There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).You may assume ...原创 2020-01-08 22:14:41 · 114 阅读 · 0 评论 -
leetcode-5
leetcode-5/*Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: “babad”Output: “bab”Note: “aba” is also a valid ans...原创 2020-01-08 22:15:45 · 114 阅读 · 0 评论 -
leetcode-6
leetcode-6*The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA...原创 2020-01-08 22:16:37 · 116 阅读 · 0 评论 -
leetcode-7
leetcode-7*Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21*/public class Solution7...原创 2020-01-08 22:17:13 · 111 阅读 · 0 评论 -
leetcode-8
leetcode-8/*Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, start...原创 2020-01-08 22:18:12 · 140 阅读 · 0 评论 -
leetcode-9
leetcode-9/*Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: fals...原创 2020-01-08 22:19:09 · 116 阅读 · 0 评论 -
【leetcode-10】——Regular Expression Matching
【leetcode-10】——Regular Expression Matching题目Given an input string (s) and a pattern §, implement regularexpression matching with support for ‘.’ and ‘*’.‘.’ Matches any single character. ‘*’ Matc...原创 2020-01-10 16:12:18 · 142 阅读 · 0 评论 -
【leetcode-11】 Container With Most Water
leetcode-11—Container With Most WaterGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line ...原创 2020-01-10 16:30:20 · 117 阅读 · 0 评论 -
【Leetcode-12】 Integer to Roman
[Leetcode—12]—Integer to RomanRoman numerals are represented by seven different symbols: I, V, X, L,C, D and M.Symbol Value I 1 V 5 X 10 L50 C ...原创 2020-01-10 17:03:14 · 142 阅读 · 0 评论 -
【Leetcode-13】Roman to Integer
【Leetcode-13】—Roman to Integer/*Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50...原创 2020-01-10 17:20:13 · 153 阅读 · 0 评论 -
【Leetcode-14 】Longest Common Prefix
【Leetcode-14 】Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: [“...原创 2020-01-10 17:41:12 · 119 阅读 · 0 评论 -
【leetcode-15】 3Sum
【leetcode15】-3SumGiven an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution s...原创 2020-01-10 18:46:07 · 117 阅读 · 0 评论 -
【leetcode -16】3Sum Closest
【leetcode -16】3Sum Closest/*Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may ...原创 2020-01-11 12:49:15 · 137 阅读 · 0 评论 -
【Leetcode-17】Letter Cominations of a Phone Number
【Leetcode-17】Letter Cominations of a Phone NumberGiven a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to le...原创 2020-01-11 13:38:21 · 142 阅读 · 0 评论 -
【Leetcode- 18】4Sum
【Leetcode- 18】4Sum/*Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which...原创 2020-01-11 14:00:00 · 98 阅读 · 0 评论 -
【Leetcode -19】Remove Nth Node From End of List
【Leetcode -19】Remove Nth Node From End of ListGiven a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.Aft...原创 2020-01-11 14:32:19 · 140 阅读 · 0 评论 -
【Leetcode-20】Valid Parentheses
【Leetcode-20】Valid Parentheses/*Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must ...原创 2020-01-11 14:44:43 · 110 阅读 · 0 评论 -
【Leetcode】算法总结——栈
【Leetcode】算法总结——1. 栈Valid Parentheses(有效的括号)Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:O...原创 2020-01-12 11:28:17 · 211 阅读 · 0 评论 -
【Leetcode】算法总结——堆
【Leetcode】算法总结——堆2. 堆Merge k Sorted ListsMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[1->4->5,1->3->4,2->...原创 2020-01-12 13:08:24 · 212 阅读 · 0 评论 -
【Leetcode】算法总结—贪心
【Leetcode】算法总结—贪心引用一则漫画,了解贪心算法重点:下面是例题Wildcard Matching(通配符匹配)Given an input string (s) and a pattern §, implement wildcard pattern matching with support for ‘?’ a...原创 2020-01-12 20:07:52 · 233 阅读 · 0 评论 -
leetcode 刷题总结【1-20】题
前些日子刷完了leetcode的1-20题,现在来一个总结!!!1. 两数之和【简单】这道题给定一个数组 以及一个目标数,返回两个数之和等于这个目标数的索引。这个题首先会想到量词循环遍历,但是这样一来时间复杂度就变成了的O(n2)这个提供一个时间复杂度为 O(n)的解法牺牲空间换时间思路:查找时,建立索引。本题的缓存可在找的过程中建立索引,故一个循环可以求出。2. 两数相加【中...原创 2020-02-03 00:34:02 · 162 阅读 · 0 评论 -
【Divide-and-conquer DC分治】
目录一、梵塔问题二、假硬币问题三、金块问题四、分治五、例子一——二分查找六、例子——斯特拉森矩阵(Strassen’s matrix)乘法七、例子——棋盘问题八、例子——归并排序九、例子——快排十、例子——线性时间选择一、梵塔问题Void Hanoi(int n,int A,int B,int C){ if(n>0) { Hanoi(n-1,A,C,B);...原创 2020-02-05 15:05:40 · 222 阅读 · 0 评论 -
【动态规划 Dynamic Programming dp】
目录动态规划以及其与分治的对比矩阵连乘问题备忘录方法0-1背包问题最优二叉搜索树动态规划以及其与分治的对比分治,是把一个问题分解成独立的子问题,递归的解决子问题,再组成原问题动态规划是解决这些子问题不是独立的问题的情况。斐波那契数列其子问题不是独立的子问题f(4)子问题是f(3)与f(2),而f(2)又是f(3)的子问题。一个分而治之的算法做的工作比必要的多,反复地解决公共的子问...原创 2020-02-09 12:27:23 · 177 阅读 · 0 评论 -
【回溯算法 backtracking】
目录什么是回溯算法???从全排列问题开始理解回溯搜索算法总结回溯算法基础问题列表什么是回溯算法???回溯算法(backtracking)是暴力搜索算法的一种。这句话向我们揭示了回溯算法的用途:搜索,因此回溯算法也被称为回溯搜索算法。与“二分查找”、“线性查找”等“查找问题”不同的是,“搜索问题”完成一件事情有可能多种方法,而每一种方法又有多个步骤,回溯算法就是在不断尝试,以得到待求问题的...原创 2020-02-09 18:00:36 · 1320 阅读 · 0 评论