提交 d3fec083 编写于 作者: 门心叼龙's avatar 门心叼龙

code perfect

上级 0c665e91
......@@ -35,7 +35,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
package I2019.A003;
/**
* Description: <ReView03ListNode><br>
* Author: mxdl<br>
* Date: 2019/9/1<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class ReView03ListNode {
//1.判断一个链表有没有后环
public static boolean isRingListNode(ListNode listNode) {
ListNode slow = listNode;
ListNode fast = listNode;
while (fast != null && fast.next != null) {//*****
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}
//2.找到链表的入口节点
public static ListNode getEnterRingListNode(ListNode listNode) {
ListNode slow = listNode;
ListNode fast = listNode;
boolean isRing = false;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
isRing = true;
break;
}
}
if (!isRing) {
return null;//*****
}
fast = listNode;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
//3.翻转一个链表
public static ListNode revertListNode(ListNode listNode) {
ListNode currNode = listNode;
ListNode preNode = null;
ListNode headNode = null;
while (currNode != null) {
ListNode nextNode = currNode.next;//*****
if (nextNode == null) {
headNode = currNode;
}
currNode.next = preNode;
preNode = currNode;
currNode = nextNode;
}
return headNode;
}
//4.删除链表的倒数第n个节点
public static ListNode removeListNodeByN(ListNode listNode, int n) {
ListNode pa = listNode;
int i = 0;
while (i < n) {
pa = pa.next;
i++;
}
if (pa == null) {
return listNode.next;
}
ListNode pb = listNode;
while (pa.next != null) {//*****
pa = pa.next;
pb = pb.next;
}
pb.next = pb.next.next;
return listNode;
}
//5.合并两个有序链表
public static ListNode mergeTwoListNode(ListNode pa, ListNode pb) {
ListNode listNode = new ListNode(-1);
ListNode headNode = listNode;//*****
while (pa != null && pb != null) {
if (pa.val < pb.val) {
listNode.next = pa;
pa = pa.next;
} else {
listNode.next = pb;
pb = pb.next;
}
listNode = listNode.next;
}
listNode.next = pa != null ? pa : pb;//*****
return headNode.next;
}
//6.两个链表求和
public static ListNode sumTowListNode(ListNode pa,ListNode pb){
ListNode currNode = new ListNode(-1);
ListNode headNode = currNode;
int carry = 0;
while(pa != null && pb != null){
int sum = carry + pa.val + pb.val;
currNode.next = new ListNode(sum%10);
carry = sum/10;
pa = pa.next;
pb = pb.next;
currNode = currNode.next;
}
if(carry > 0){
currNode.next = new ListNode(carry);
}
return headNode.next;
}
public static ListNode mergeListNode1(ListNode pa, ListNode pb) {
ListNode currNode = new ListNode(-1);
ListNode headNode = currNode;
while (pa != null && pb != null) {
if (pa.val < pb.val) {
currNode.next = pa;
pa = pa.next;
} else {
currNode.next = pb;
pb = pb.next;
}
currNode = currNode.next;
}
currNode.next = pa != null ? pa : pb;
return headNode.next;
}
public static void main(String[] args) {
// ListNode listNode = new ListNode(0);
// ListNode listNode1 = new ListNode(1);
// ListNode listNode2 = new ListNode(2);
// ListNode listNode3 = new ListNode(3);
//
// listNode.next = listNode1;
// listNode1.next = listNode2;
// listNode2.next = listNode3;
//listNode3.next = listNode2;
//boolean cricleListNode = isRingListNode(listNode);
//System.out.println(cricleListNode);
//ListNode enterRingListNode = getEnterRingListNode(listNode);
//System.out.println(enterRingListNode.val);
// ListNode listNode4 = revertListNode(listNode);
// ListNode listNode4 = removeListNodeByN(listNode, 1);
// while (listNode4 != null) {
// System.out.println(listNode4.val);
// listNode4 = listNode4.next;
// }
ListNode listNodea = new ListNode(1);
ListNode listNode1a = new ListNode(3);
ListNode listNode2a = new ListNode(5);
listNodea.next = listNode1a;
listNode1a.next = listNode2a;
ListNode listNodeaa = new ListNode(2);
ListNode listNode1aa = new ListNode(4);
ListNode listNode2aa = new ListNode(6);
//ListNode listNode2aaa = new ListNode(7);
listNodeaa.next = listNode1aa;
listNode1aa.next = listNode2aa;
//listNode2aa.next = listNode2aaa;
ListNode listNode4 = sumTowListNode(listNodea, listNodeaa);
while (listNode4 != null) {
System.out.println(listNode4.val);
listNode4 = listNode4.next;
}
}
}
package I2019.A003;
/**
* Description: <ReView04TreeNode><br>
* Author: mxdl<br>
* Date: 2019/9/1<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class ReView04TreeNode {
//1.求二叉树的结点数
public static int getTreeNodeCount(TreeNode treeNode) {
if (treeNode == null) {
return 0;
}
return 1 + getTreeNodeCount(treeNode.leftNode) + getTreeNodeCount(treeNode.rightNode);
}
//2.求二叉树的高度
public static int getTreeNodeHeight(TreeNode treeNode) {
if (treeNode == null) {
return 0;
}
int leftHeight = 1 + getTreeNodeHeight(treeNode.leftNode);
int rightHeight = 1 + getTreeNodeHeight(treeNode.rightNode);
return Math.max(leftHeight, rightHeight);
}
//3.中序遍历二叉树
public static void printMiddleTreeNode(TreeNode treeNode) {
if (treeNode == null) {
return;
}
printMiddleTreeNode(treeNode.leftNode);
System.out.println(treeNode.val);
printMiddleTreeNode(treeNode.rightNode);
}
//5.判断两个二叉树是否是完全二叉树
public static boolean isEqualTreeNode(TreeNode ta, TreeNode tb) {
if (ta == null && tb == null) {
return true;
}
if (ta == null && tb != null) {
return false;
}
if (ta != null && tb == null) {
return false;
}
return ta.val == tb.val && isEqualTreeNode(ta.leftNode, tb.leftNode) && isEqualTreeNode(ta.rightNode, tb.rightNode);
}
//6.判断一个二叉树是否是对称二叉树
public static boolean isSymsetry(TreeNode treeNode) {
if (treeNode == null) {
return false;
} else {
return isSymserty(treeNode.leftNode, treeNode.rightNode);
}
}
public static boolean isSymserty(TreeNode leftNode, TreeNode rightNode) {
if (leftNode == null && rightNode == null) {
return true;
}
if (leftNode != null && rightNode == null) {
return false;
}
if (leftNode == null && rightNode != null) {
return false;
}
return leftNode.val == rightNode.val && isSymserty(leftNode.leftNode, rightNode.rightNode) && isSymserty(leftNode.rightNode, rightNode.leftNode);
}
public static void main(String[] args) {
TreeNode treeNode = new TreeNode(0);
TreeNode treeNode1 = new TreeNode(1);
TreeNode treeNode2 = new TreeNode(1);
TreeNode treeNode3 = new TreeNode(3);
TreeNode treeNode4 = new TreeNode(4);
TreeNode treeNode5 = new TreeNode(5);
TreeNode treeNode6 = new TreeNode(6);
treeNode.leftNode = treeNode1;
treeNode.rightNode = treeNode2;
treeNode1.leftNode = treeNode3;
treeNode2.rightNode = treeNode3;
treeNode1.rightNode = treeNode4;
treeNode2.leftNode = treeNode6;
//treeNode1.leftNode = treeNode3;
//treeNode1.rightNode = treeNode4;
//treeNode2.leftNode = treeNode6;
//treeNode2.rightNode = treeNode5;
// int treeNodeCount = getTreeNodeCount(treeNode);
// System.out.println(treeNodeCount);
//int treeNodeHeight = getTreeNodeHeight(treeNode);
//System.out.println(treeNodeHeight);
//printMiddleTreeNode(treeNode);
boolean equalTreeNode = isSymsetry(treeNode);
System.out.println(equalTreeNode);
}
}
package I2019.A003;
/**
* Description: <ReView05Int><br>
* Author: mxdl<br>
* Date: 2019/9/1<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class ReView05Int {
//1.翻转一个整数
public static int revertInt(int num) {
int result = 0;
while (num > 0) {
result = result * 10 + num % 10;
num = num / 10;
}
return result;
}
public static void main(String[] args) {
int result = revertInt1(123);
System.out.println(result);
}
public static int revertInt1(int num) {
int result = 0;
while (num > 0) {
result = result * 10 + num % 10;
num = num / 10;
}
return result;
}
}
package I2019.A003;
/**
* Description: <ReView06String><br>
* Author: mxdl<br>
* Date: 2019/9/1<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class ReView06String {
//1.查找最大的非重复字符串
public static String searchMaxStr(String result) {
//String reg = "\\.(1)+";
String reg1 = "(.)\\1+";//******************
String[] split = result.split(reg1);
String maxStr = "";
for (String str : split) {
if (str.length() > maxStr.length()) {
maxStr = str;
}
}
return maxStr;
}
public static String getLongText(String str) {
boolean[][] arr = new boolean[str.length()][str.length()];
int maxLen = 0;
String longText = "";
for (int i = 0; i < str.length(); i++) {
arr[i][i] = true;
maxLen = 1;
}
for (int i = 0; i < str.length()-1; i++) {//*****
if (str.charAt(i) == str.charAt(i + 1)) {
arr[i][i + 1] = true;
}
maxLen = 2;
}
for (int len = 3; len < str.length(); len++) {
for (int i = 0, j = 0; (j = len + i - 1) < str.length(); i++) {//******
if(str.charAt(i) == str.charAt(j)){
arr[i][j] = arr[i+1][j-1];
if(arr[i][j] && maxLen < len){
longText = str.substring(i,j+1);
maxLen = len;
}
}else{
arr[i][j] = false;
}
}
}
return longText;
}
public static String getLongText1(String str){
boolean[][] arr = new boolean[str.length()][str.length()];
int maxLen = 0;
String longText = "";
for(int i = 0;i < str.length();i++){
arr[i][i] = true;
maxLen = 1;
}
for(int i = 0;i < str.length()-1 ;i++){
if(str.charAt(i) == str.charAt(i+1)){
arr[i][i+1] = true;
maxLen = 2;
}
}
for(int len = 3; len < str.length();len++){
for(int i = 0,j = 0; (j=len+i-1)<str.length();i++){
if(str.charAt(i) == str.charAt(j)){
arr[i][j] = arr[i+1][j-1];
if(arr[i][j] && maxLen < len){
longText = str.substring(i,j+1);
maxLen = len; }
}else{
arr[i][j] = false;
}
}
}
return longText;
}
public static void main(String[] args) {
//String value = searchMaxStr("abcaabb23435dd987ff");
//System.out.println(value);
String result = getLongText1("ac1d2ff2d1db");
System.out.println(result);
}
}
package I2019.A003;
/**
* Description: <TreeNode><br>
* Author: mxdl<br>
* Date: 2019/9/1<br>
* Version: V1.0.0<br>
* Update: <br>
*/
public class TreeNode {
public int val;
public TreeNode leftNode;
public TreeNode rightNode;
public TreeNode(int val) {
this.val = val;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册