- 博客(255)
- 问答 (2)
- 收藏
- 关注
原创 470. 用 Rand7() 实现 Rand10()
思路:1、用Rand10()实现Rand7()比较容易(大数实现小数):第一次就命中的p:7/10第二次命中的p:3/10+7/10第三次命中的p:+以此类推:根据等比数列公式得出:2、用Rand7()实现Rand10()(小数实现大数):将小数映射为大数,并且要求在区间[x,y]内得数都能等概率获得(RandX()-1)*X+RandY()可以等概率获得区间[1,X*Y]内的数3、现在只有Rand7(),则第一步:(Rand7()-1)*7+Rand7()得
2022-02-27 14:12:00
419
原创 Spring Session 利用 Redis来支持HttpSession
1、配置Config@EnableRedisHttpSessionpublic class SessionConfig{ @Bean public LettuceConnectionFactory connectionFactory(){ return new LettuceConnectionFactory(); }}@EnableRedisHttpSession注解的作用:1)创建了一个Spring Bean2)springSessionRepositoryFilter过
2021-12-30 15:36:59
1584
原创 Springboot中获取路径的方式
前置条件:http://127.0.0.1:9001/aiforce/authentication/sso1)request.getContextPath()/aiforce2)request.getServletPath()/authentication/sso只返回传递到servlet的路径3)request.getPathInfo()/authentication/sso只返回传递到servlet的路径4)request.getRequest
2021-12-30 10:41:50
2977
原创 518. 零钱兑换 II(中等)
思路:背包问题,选和不选两种选择代码:二维数组class Solution { public int change(int amount, int[] coins) { int n=coins.length; int[][] dp=new int[n+1][amount+1]; for(int i=0;i<=n;i++){ //余额为0时只有“无为而治”一种解法 dp[i][0]=1; } //注意这里是取得到n的,也取得到amo
2021-08-04 22:51:09
294
原创 145. 二叉树的后序遍历(简单)
思路:递归迭代(中等、难)代码:class Solution { public List<Integer> postorderTraversal(TreeNode root) { Deque<TreeNode> stack=new LinkedList<>(); LinkedList<Integer> res=new LinkedList<>(); if(root==null){ return re.
2021-06-22 16:41:36
187
原创 144. 二叉树的前序遍历(简单)
思路:1.递归2.迭代代码:递归:class Solution { List<Integer> list=new ArrayList<>(); public List<Integer> preorderTraversal(TreeNode root) { if(root==null){ return new ArrayList<>(); } list.add(root.val); p.
2021-06-21 10:58:32
96
原创 48. 旋转图像(中等)
思路:进行2次翻转即可:水平翻转+主对角线翻转代码:class Solution { public void rotate(int[][] matrix) { int n=matrix.length; //水平翻转,只遍历行的一半即可 for(int i=0;i<n/2;i++){ for(int j=0;j<n;j++){ int temp=matrix[i][j]; matrix[i][j]=matrix[n-i-1][.
2021-06-20 11:25:18
121
原创 21.秒杀服务(sentinel熔断降级)
前端限流、恶意请求拦截:判断是否登录秒杀系统有2种模式:1)结合前面的订单服务,只是QPS增高了而已2)方法二的流程:判断登录-->合法性校验-->获取信号量-->放入mq中熔断:降级:限流:熔断限流的功能使用spring cloud 的sentinel实现:自定义拦截信息:使用Sentinel保护Feign,也就是熔断:先在xml中打开senti...
2021-06-19 19:40:21
581
原创 239. 滑动窗口最大值(困难)
思路:优先队列代码:class Solution { public int[] maxSlidingWindow(int[] nums, int k) { int n=nums.length; //int[]数组中0为元素,1为下标 PriorityQueue<int[]> queue=new PriorityQueue<int[]>(new Comparator<int[]>(){ public int compare(in.
2021-06-16 10:14:31
126
原创 739. 每日温度(中等)
思路:使用栈,来存储不大于的温度的下标代码:class Solution { public int[] dailyTemperatures(int[] T) { int n=T.length; Deque<Integer> stack=new ArrayDeque<>(); stack.addLast(0); int[] res=new int[n]; for(int i=1;i<n;i++){ //栈中存的是下标 .
2021-06-16 09:44:49
116
原创 617. 合并二叉树(简单)
思路:深度优先遍历分别遍历两个二叉树的左节点,右节点,分别相加代码:class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if(root1==null){ return root2; } if(root2==null){ return root1; } TreeNode merge=new TreeNode(root1.val+root2.
2021-06-16 09:23:02
137
原创 581. 最短无序连续子数组(中等)
思路:1.创建一个新array,保存排好序的数组,逐个比较哪个下标的值变化了2.双指针代码:public class Solution { public int findUnsortedSubarray(int[] nums) { int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE; boolean flag=false; for(int i=1;i<nums.length;i++){ if(nums[.
2021-06-15 16:41:44
116
原创 543. 二叉树的直径(简单)
思路:后序遍历,只是直径指的是边,而不是节点的个数代码:class Solution { int max=0; public int diameterOfBinaryTree(TreeNode root) { depth(root); return max; } private int depth(TreeNode root){ if(root==null){ return 0; } int left=depth(root.left.
2021-06-15 09:51:19
148
原创 647. 回文子串(中等)
思路:借鉴5.最长回文字串的写法代码:class Solution { public int countSubstrings(String s) { int len=s.length(); // if(len<2){ // return 1; // } int count=0; boolean[][] dp=new boolean[len][len]; //初始化 for(int i=0;i<len;i++){ dp[i].
2021-06-14 11:02:59
137
原创 560. 和为K的子数组(中等)
思路:1.前缀和2.前缀和+哈希表代码:前缀和class Solution { public int subarraySum(int[] nums, int k) { int n=nums.length; int sum=0; int[] dp=new int[n+1]; dp[0]=0; //计算前缀和 for(int i=1;i<=n;i++){ //dp[i]代表不包括当前数nums[i]的前缀和 dp[i]=d.
2021-06-14 10:45:30
123
原创 494. 目标和(中等)
思路:0-1背包问题,回溯(深度优先遍历)代码:class Solution { int count=0; public int findTargetSumWays(int[] nums, int target) { dfs(nums,target,0,0); return count; } private void dfs(int[] nums,int target,int index,int sum){ //index==nums.length表示.
2021-06-14 10:06:01
134
原创 448. 找到所有数组中消失的数字(简单)
思路:利于一个哈希表,把数据都存起来代码:class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { List<Integer> res=new ArrayList<>(); Map<Integer,Integer> map=new HashMap<>(); for(int num:nums){ map.pu.
2021-06-14 09:55:26
93
原创 416. 分割等和子集(中等)
思路:0-1背包问题,就是选和不选的关系,动态规划dp代码:public class Solution { public boolean canPartition(int[] nums) { int n=nums.length; int sum=0; //先计算总和 for(int num:nums){ sum+=num; } //总和不能为奇数 if(sum%2==1){ return false;.
2021-06-13 11:09:39
74
原创 406. 根据身高重建队列(中等)
思路:先按身高降序排列,如果身高相等,就按人数升序排序代码:class Solution { public int[][] reconstructQueue(int[][] people) { Arrays.sort(people,new Comparator<int[]>(){ public int compare(int[] a,int[] b){ if(a[0]!=b[0]){ //按身高降序 return b[0]-a[0.
2021-06-13 10:19:24
140
原创 437. 路径总和 III(中等)
思路:双递归代码:class Solution { public int pathSum(TreeNode root, int sum) { //双递归,保证每个节点都作为一次根节点 if(root==null){ return 0; } return res(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum); } private int res(TreeNode root,int.
2021-06-13 09:49:18
92
原创 338. 比特位计数(简单)
思路:位运算,分偶数和奇数2种情况代码:class Solution { public int[] countBits(int num) { int[] dp=new int[num+1]; dp[0]=0; for(int i=1;i<=num;i++){ if(i%2==1){ //奇数,前一个数的结果+1 dp[i]=dp[i-1]+1; } else{ //偶数,与前一个偶数相同 dp[i]=dp[.
2021-06-13 09:16:50
175
原创 347. 前 K 个高频元素(中等)
思路:小根堆(优先队列)维护前k个元素+哈希表HashMap代码:class Solution { public int[] topKFrequent(int[] nums, int k) { //利于哈希表存储出现的频次 Map<Integer,Integer> map=new HashMap<>(); for(int num:nums){ map.put(num,map.getOrDefault(num,0)+1); } .
2021-06-12 20:06:10
111
原创 337. 打家劫舍 III(中等)
思路:动态规划dp,后序遍历(消除后效性)代码:class Solution { public int rob(TreeNode root) { //注意dfs返回的参数是int[],2个值(根节点偷/不偷),还要比较哪一种比较大 int[] res=dfs(root); return Math.max(res[0],res[1]); } private int[] dfs(TreeNode root){ if(root==null){ ret.
2021-06-12 11:02:21
66
原创 322. 零钱兑换(中等)
思路:跟287.寻找重复数方法一致,动态规划dp代码:class Solution { public int coinChange(int[] coins, int amount) { int n=coins.length; //要将长度设置为amount+1 int[] dp=new int[amount+1]; //用amount+1的初始化长度来创建dp //目的是判断是否没有数字符合 Arrays.fill(dp,amount+1); .
2021-06-12 10:34:13
73
原创 287. 寻找重复数(中等)
思路:题目要求不能修改数组nums,也就是不能原地修改并且要求空间复杂度为O(1),表明不能使用哈希表则此题利于位运算或floy算法代码:class Solution { public int findDuplicate(int[] nums) { int f=0,s=0; //第一次循环找到相遇点 while(true){ f=nums[nums[f]]; s=nums[s]; if(f==s){ break; } } .
2021-06-11 10:26:41
258
2
原创 283. 移动零(简单)
思路:将不是0的值进行覆盖,再把0添加到最后代码:class Solution { public void moveZeroes(int[] nums) { //index1来记录不为0的值 //index2用来遍历所有值 int index1=0,index2=0; int m=nums.length; for(int num:nums){ if(nums[index2]!=0){ nums[index1++]=num; } .
2021-06-11 09:56:08
98
1
原创 234. 回文链表(简单)
思路:先通过快慢指针找到中间节点,把后半部分的链表翻转,再和前半部分的链表进行值比较,比较完后将后半部分的链表再翻转回来代码:class Solution { public boolean isPalindrome(ListNode head) { if(head==null){ return true; } ListNode tail=endOfHead(head); ListNode second=reverseList(tail.next); .
2021-06-09 10:00:49
120
原创 226. 翻转二叉树(简单)
思路:后序遍历(递归)代码:class Solution { public TreeNode invertTree(TreeNode root) { if(root==null){ return null; } //后序遍历 TreeNode left=invertTree(root.left); TreeNode right=invertTree(root.right); root.left=right; root.right=left; .
2021-06-09 09:36:32
64
原创 221. 最大正方形(中等)
思路:动态规划dp代码:class Solution { public int maximalSquare(char[][] matrix) { int max=0; if(matrix==null||matrix.length==0||matrix[0].length==0){ return max; } int m=matrix.length; int n=matrix[0].length; int[][] dp=new int[m][n].
2021-06-09 09:22:55
94
原创 215. 数组中的第K个最大元素(中等)
思路:1.暴力快排Arrays.sort()2.分治(快速排序)代码:class Solution { public int findKthLargest(int[] nums, int k) { int target=nums.length-k; int index=partition(nums,0,nums.length-1,target); return nums[index]; } private int partition(int[] nums,.
2021-06-09 09:02:01
111
原创 20.下单服务(mq、分布式事务)
mq介绍:交换机的类型:1.direct 2.fanout 3.topic 4.headers(一般不用)direct点对点通信模式:fanout:广播模式topic:部分广播RabbitMQ的消息确认机制——可靠抵达订单服务的流程:mq在下单服务是为了解决分布式事务的问题:远程服务假失败:远程服务其实成功了,由于网络问题没有返回,会导致:订单回滚,但是远程事务不会回滚本地事务@Trans...
2021-06-08 21:35:50
1007
原创 19.下单服务(Feign远程调用丢失请求头、Feign异步丢失上下文、幂等性)
跳转到结算页,获取订单确认页需要的数据时,出现以下3种问题:1.Feign远程调用丢失请求头的问题:order模块里调用了member、cart模块,但是本地cookie里没有数据,请求头中没有携带Cookie,就会被认为没登录解决:写一个filter,把原来的请求头给当前的新请求加上2.Feign异步丢失上下文问题:因为使用的是ThreadLocal传递数据,但是ThreadLocal的数据统一只能在同一个线程下,而异步情况是开了几个不同的线程,此时ThreadLoc.
2021-06-08 21:17:12
531
空空如也
springboot集成gateway时出现nacos错误
2021-03-20
TA创建的收藏夹 TA关注的收藏夹
TA关注的人