算法 排序
少壮要努力&&
这个作者很懒,什么都没留下…
展开
-
判断5张牌是否为顺子
一个大佬的想法:能够组成顺子的条件:1.除大小王外,所有牌 无重复 ;2. 设此 55 张牌中最大的牌为 max ,最小的牌为 min (大小王除外),则需满足:max - min < 5class Solution { public boolean isStraight(int[] nums) { Set<Integer> repeat = new HashSet<>(); int max = 0; int原创 2021-04-08 22:08:49 · 597 阅读 · 0 评论 -
链表中倒数第k个数-力扣
方法1:使用快慢指针,快指针先走k步,当k到结尾时,慢指针就是倒数第k个数/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode getKthFromEnd(ListNode head,原创 2021-04-06 17:12:31 · 89 阅读 · 0 评论 -
两数之和2 - 力扣
方法一:两个for遍历,没什么好说的class Solution { public int[] twoSum(int[] numbers, int target) { int[] arr = new int[2]; for(int i=0;i<numbers.length;i++){ for(int j=i+1;j<numbers.length;j++){ if(numb原创 2021-04-04 23:30:26 · 45 阅读 · 0 评论 -
相交链表-力扣
方法1:遍历A链表并加入集合中,然后遍历B链表,查看集合是否包含当前节点,有则是相交点,否则不相交/* * 相交链表 */public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; } Set<ListNode> list = new HashSet<Lis原创 2021-04-04 22:31:36 · 52 阅读 · 0 评论 -
力扣 - 加1
力扣简单题加1class Solution { public int[] plusOne(int[] digits) { int index = digits.length-1; while(index >=0){ //加1后没有向上一位进位,直接返回 if(digits[index]+1 <10 ){ digits原创 2021-04-03 21:41:55 · 93 阅读 · 0 评论 -
2021-04-03
多种方式,有效括号方法1:这是力扣一个大佬的很妙的方式 /* * 判断括号是否有效,心得:在多种可能的情况下可以使用hashmap存储所有可能性 * */ static boolean kuohao(String s) { Stack<Character> stack = new Stack<Character>(); for(int i=0;i<s.length();i++) { char ch=s.charAt(i);原创 2021-04-03 20:11:12 · 50 阅读 · 0 评论 -
二分法
/* * 二分查找 */ static int myBinaryseach(int[] nums, int target) { int left = 0; int right = nums.length-1; while(left <= right) { int mid = ((right - left) >>1)+left; if(target > nums[mid]) { left = mid+1; }原创 2021-04-03 20:01:44 · 34 阅读 · 0 评论 -
看了这么多文章,没有一个快速排序能跑起来的
static void myquickSort(int[] arr,int left , int right) { int i=left; int j=right; int shaobing = arr[i]; int temp; while(i<j) { while(i<j && arr[i] <= shaobing ) i++; while(i<j && ar原创 2021-03-03 22:56:08 · 47 阅读 · 0 评论