Skip to content

Commit 3dae4db

Browse files
author
wb-hjk570755
committed
链表
1 parent 7ae4a98 commit 3dae4db

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.blankj.myself;
2+
3+
import com.blankj.myself.HasCycle_fast_slow_point.ListNode;
4+
5+
/**
6+
* Description:leecode 160. 相交链表
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/2
12+
*/
13+
public class GetIntersectionNode_two_linklist {
14+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
15+
ListNode pA = headA;
16+
ListNode pB = headB;
17+
while (pA!=pB){
18+
pA = pA==null?headB:pA.next;
19+
pB = pB==null?headA:pB.next;
20+
}
21+
return pA;
22+
}
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.blankj.myself;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/2
12+
*/
13+
public class HasCycle_fast_slow_point {
14+
15+
public boolean hasCycle(ListNode head) {
16+
if(head==null||head.next==null){
17+
return false;
18+
}
19+
ListNode fast = head;
20+
ListNode slow = head;
21+
while (fast!=null&&fast.next!=null){
22+
23+
fast = fast.next.next;
24+
slow = slow.next;
25+
26+
if(fast == slow){
27+
return true;
28+
}
29+
30+
}
31+
return false;
32+
}
33+
34+
class ListNode {
35+
int val;
36+
ListNode next;
37+
ListNode(int x) {
38+
val = x;
39+
next = null;
40+
}
41+
}
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.blankj.myself;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/2
12+
*/
13+
public class HasCycle_hash {
14+
15+
public boolean hasCycle(ListNode head) {
16+
HashSet<ListNode> hashSet = new HashSet<>();
17+
while (head!=null){
18+
if(hashSet.contains(head)){
19+
return true;
20+
}else {
21+
hashSet.add(head);
22+
head = head.next;
23+
}
24+
}
25+
return false;
26+
}
27+
28+
class ListNode {
29+
int val;
30+
ListNode next;
31+
ListNode(int x) {
32+
val = x;
33+
next = null;
34+
}
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.blankj.myself;
2+
3+
/**
4+
* Description:
5+
* Copyright: Copyright (c) 2012
6+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
7+
*
8+
* @author huangjk
9+
* @version 1.0 2020/10/1
10+
*/
11+
public class PredictTheWinner_Dp {
12+
public static void main(String[] args) {
13+
int v[] = { 1, 2, 3, 6, 9, 5, 7, 4, 2, 6, 9, 5, 8, 7, 2, 1, 55, 3, 6, 9, 7, 5, 2 };
14+
PredictTheWinner_Dp predictTheWinner = new PredictTheWinner_Dp();
15+
System.out.println(predictTheWinner.PredictTheWinner(v));
16+
}
17+
18+
public boolean PredictTheWinner(int[] nums) {
19+
if(nums.length%2==0){
20+
return true;
21+
}
22+
23+
int[][] dp = new int[nums.length][nums.length];
24+
for(int i=0;i<nums.length;i++){
25+
dp[i][i] = nums[i];
26+
}
27+
28+
for(int i=nums.length-2;i>=0;i--){
29+
for (int j=i+1;j<nums.length;j++){
30+
dp[i][j] = Math.max(nums[i]-dp[i+1][j],nums[j]-dp[i][j-1]);
31+
}
32+
}
33+
return dp[0][nums.length-1]>=0;
34+
}
35+
36+
37+
}

src/com/blankj/myself/StrStr.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.blankj.myself;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Description:
7+
* Copyright: Copyright (c) 2012
8+
* Company: keruyun Technology(Beijing) Chengdu Co. Ltd.
9+
*
10+
* @author huangjk
11+
* @version 1.0 2020/10/1
12+
*/
13+
public class StrStr {
14+
public static void main(String[] args) {
15+
StrStr strStr = new StrStr();
16+
strStr.strStr("mississippi",
17+
"issip");
18+
}
19+
20+
public int strStr(String haystack, String needle) {
21+
int hLen = haystack.length();
22+
int nLen = needle.length();
23+
24+
for (int i=0;i<hLen-nLen;i++){
25+
for (int j=0; j<nLen;j++){
26+
if (haystack.charAt(i+j)==needle.charAt(j)){
27+
if(j==nLen-1){
28+
return i;
29+
}
30+
}else {
31+
break;
32+
}
33+
}
34+
35+
}
36+
return -1;
37+
}
38+
}

0 commit comments

Comments
 (0)