上一页 1 2 3 4 5 6 ··· 11 下一页
摘要: class Solution { //nums[mid]要么在较大一半 //要么在较小一半 //注意范围缩减至两个和一个的时候的处理就行了 public int findMin(int[] nums) { //if(nums.length==1)return nums[0]; int lo=0; i 阅读全文
posted @ 2021-08-01 21:16 wsshub 阅读(31) 评论(0) 推荐(0)
摘要: 优秀题解: 从抛硬币开始,循序渐进把这道题吃透!看不懂算我输! /** * The rand7() API is already defined in the parent class SolBase. * public int rand7(); * @return a random integer 阅读全文
posted @ 2021-08-01 00:43 wsshub 阅读(40) 评论(0) 推荐(0)
摘要: 回溯(耗时大): class Solution { int ans=0; public int findTargetSumWays(int[] nums, int target) { dfs(nums,0,target,0); return ans; } void dfs(int[] nums,in 阅读全文
posted @ 2021-07-27 00:33 wsshub 阅读(38) 评论(0) 推荐(0)
摘要: 0-1背包问题类似 二维dp: class Solution { public boolean canPartition(int[] nums) { int sum=0; for(int num:nums){ sum+=num; } if(sum%2!=0)return false; int tar 阅读全文
posted @ 2021-07-25 11:04 wsshub 阅读(39) 评论(0) 推荐(0)
摘要: 速度比较: 做完整的快排<<<不做完整的快排<<自建堆<不做完整的且随机选择哨兵的快排 贴上最快的代码: class Solution { int[] nums; public int findKthLargest(int[] nums, int k) { this.nums=nums; quick 阅读全文
posted @ 2021-07-24 15:54 wsshub 阅读(46) 评论(0) 推荐(0)
摘要: hashmap+自建堆 class Solution { public int[] topKFrequent(int[] nums, int k) { HashMap<Integer,Integer> map=new HashMap<>(); for(int i=0;i<nums.length;i+ 阅读全文
posted @ 2021-07-24 15:37 wsshub 阅读(33) 评论(0) 推荐(0)
摘要: 通过Math.random()来获取随机数。实际上,它返回的是0(包含)到1(不包含)之间的double值。使用方法如下: final double d = Math.random(); 若要获取int类型的整数,只需要将上面的结果转行成int类型即可。比如,获取[0, 100)之间的int整数。方 阅读全文
posted @ 2021-07-23 12:25 wsshub 阅读(45) 评论(0) 推荐(0)
摘要: 非递归的解法无语了,根本不是模仿递归。 递归解法两条链总是几乎一样个数的,题解的非递归解法,可能会出现个数悬殊的情况。 非递归: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListN 阅读全文
posted @ 2021-07-16 00:57 wsshub 阅读(34) 评论(0) 推荐(0)
摘要: 环形链表 public class Solution { public boolean hasCycle(ListNode head) { if(head==null)return false; ListNode p1=head; ListNode p2=head; do{ p1=p1.next; 阅读全文
posted @ 2021-07-03 15:29 wsshub 阅读(36) 评论(0) 推荐(0)
摘要: 非递归实现,且空间复杂度O(n): class Solution { public void flatten(TreeNode root) { LinkedList<TreeNode> sta=new LinkedList<>(); TreeNode p=root; TreeNode pre=nul 阅读全文
posted @ 2021-06-13 18:16 wsshub 阅读(39) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 ··· 11 下一页