File tree Expand file tree Collapse file tree 4 files changed +140
-0
lines changed
leetcode/src/main/java/com/mistray Expand file tree Collapse file tree 4 files changed +140
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .mistray .link ;
2+
3+ import java .util .ArrayList ;
4+
5+ /**
6+ * @author ZJY(MistRay)
7+ * @Project algorithm-study
8+ * @Package com.mistray.link
9+ * @create 2020年03月04日 15:02
10+ * @Desc
11+ */
12+ public class PalindromeLinkedList234 {
13+
14+
15+ public static void main (String [] args ) {
16+ System .out .println (2 %2 );
17+ }
18+
19+ // 双指针法
20+ public boolean isPalindrome (ListNode head ) {
21+ if (head == null || head .next == null ){
22+ return true ;
23+ }
24+ ArrayList <ListNode > list = new ArrayList <>();
25+ ListNode fast = head ;
26+ ListNode slow = head ;
27+ int count = 1 ;
28+ while (fast != null && fast .next != null ) {
29+ list .add (slow );
30+ slow = slow .next ;
31+ fast = fast .next .next ;
32+ if (fast != null ) {
33+ count = count + 2 ;
34+ } else {
35+ count ++;
36+ }
37+ }
38+ ListNode tmp ;
39+ if (count % 2 == 0 ) {
40+ tmp = slow ;
41+ } else {
42+ tmp = slow .next .next ;
43+ }
44+ for (int i = list .size () - 1 ; i > -1 ; i --) {
45+ if (tmp .val != list .get (i ).val ) {
46+ return false ;
47+ }
48+ tmp = tmp .next ;
49+ }
50+ return true ;
51+ }
52+
53+ // 复制数组双指针法
54+ public boolean isPalindrome2 (ListNode head ) {
55+ ArrayList <ListNode > list = new ArrayList <>();
56+ while (head != null ) {
57+ list .add (head );
58+ head = head .next ;
59+ }
60+ for (int i = 0 , f = list .size () - 1 ; i < list .size () / 2 ; i ++, f --) {
61+ if (list .get (i ).val != list .get (f ).val ) {
62+ return false ;
63+ }
64+ }
65+ return true ;
66+ }
67+ }
Original file line number Diff line number Diff line change 1+ package com .mistray .tree ;
2+
3+ /**
4+ * @author ZJY(MistRay)
5+ * @Project algorithm-study
6+ * @Package com.mistray.tree
7+ * @create 2020年03月04日 9:59
8+ * @Desc
9+ */
10+ public class MergeTwoBinaryTrees617 {
11+
12+ public static void main (String [] args ) {
13+
14+ }
15+
16+ public TreeNode mergeTrees (TreeNode t1 , TreeNode t2 ) {
17+ if (t1 == null && t2 == null ) {
18+ return null ;
19+ } else if (t1 == null ) {
20+ return t2 ;
21+ } else if (t2 == null ) {
22+ return t1 ;
23+ } else {
24+ t1 .val = t1 .val + t2 .val ;
25+ t1 .left = mergeTrees (t1 .left , t2 .left );
26+ t1 .right = mergeTrees (t1 .right , t2 .right );
27+ return t1 ;
28+ }
29+ }
30+ }
31+
32+
33+
Original file line number Diff line number Diff line change 1+ package com .mistray .tree ;
2+
3+ /**
4+ * @author ZJY(MistRay)
5+ * @Project algorithm-study
6+ * @Package com.mistray.tree
7+ * @create 2020年03月04日 13:31
8+ * @Desc
9+ */
10+ public class SymmetricTree101 {
11+
12+ public static void main (String [] args ) {
13+
14+ }
15+
16+ public boolean isSymmetric (TreeNode root ) {
17+ return helper (root ,root );
18+ }
19+
20+ public boolean helper (TreeNode t1 , TreeNode t2 ) {
21+ if ((t2 != null && t1 != null ) && t1 .val == t2 .val ) {
22+ return helper (t1 .right , t2 .left ) && helper (t1 .left , t2 .right );
23+ } else if (t1 == null && t2 == null ) {
24+ return true ;
25+ } else {
26+ return false ;
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .mistray .tree ;
2+
3+ public class TreeNode {
4+ int val ;
5+ TreeNode left ;
6+ TreeNode right ;
7+
8+ TreeNode (int x ) {
9+ val = x ;
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments