写在开头:正值毕业生求职期间,翻一翻剑指offer的书籍,将其中的题目进行分析和代码编写,并将代码整理到csdn博客上,如果有错误,敬请指出!(此系列不提供题目原文,请参考书籍《剑指offer第二版》即可。)
Test20 - Test29系列
1. Test20.java
public class Test20{
public static boolean isnum(String str)
{
if(str==null||str.length()<1)
return false;
int index =0;
if(str.charAt(index)=='+'||str.charAt(index)=='-')
index++;
if(index>=str.length())
return false;
while(index<str.length()&&isshuzi(str,index))
index++;
if(index>=str.length())
return true;
if(str.charAt(index)=='.') {
index++;
if (index >= str.length())
return false;
while (index < str.length() && isshuzi(str, index))
index++;
if (index >= str.length())
return true;
}
else{
if(str.charAt(index)=='e'|| str.charAt(index)=='E')
index++;
if(index>=str.length())
return false;
else{
if(str.charAt(index)=='+'||str.charAt(index)=='-')
{ index++;
if(index>=str.length())
return false;
else {
while(index<str.length()&&isshuzi(str,index))
index++;
if(index>=str.length())
return true;
}
}
}
}
return false;
}
public static boolean isshuzi(String str,int index) {
if (str.charAt(index) >= '0' && str.charAt(index) <= '9')
return true;
else
return false;
}
public static void main(String[] args)
{
String str = "+100e+5";
System.out.println(isnum(str));
}
}
2. Test21.java
public class Test21{
public static int[] joSwap(int[] arr)
{
if(arr==null||arr.length<1)
return null;
int i = 0;
int j = arr.length-1;
while(i<=j)
{
if((arr[i]&0x1)==1)
i++;
else if((arr[i]&0x1)==0)
{
swap(arr,i,j);
j--;
}
}
return arr;
}
public static void swap(int[] arr,int i,int j){
if(arr==null||arr.length<1)
return;
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr)
{
if(arr==null||arr.length<1)
return;
for(int i : arr)
System.out.print(i);
}
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5};
print(joSwap(arr));
}
}
3. Test22.java
public class Test22{
public static class LNode{
int data;
LNode next;
public LNode(int data){
this.data = data;
}
}
public static LNode thKNode(LNode head,int k){
if(head==null||k<=0)
return null;
LNode quick = head;
LNode slow = head;
int i = 1; //如果 k=1就直接返回头结点
while (i < k && quick != null) {
++i;
quick = quick.next;
}
if(i<=k&&quick==null)
return null;//如果经过以上循环之后,i<k说明k已经超出链表的长度了,如果i=k且quick为空,说明给出的k比链表的长度大1;
else {
while (quick != null && quick.next != null) {
quick = quick.next;
slow = slow.next;
}
}
return slow;
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
LNode n2 = new LNode(2);
LNode n3 = new LNode(3);
LNode n4 = new LNode(4);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = null;
LNode head = n1;
LNode node = thKNode(head,5);
System.out.println(node.data);
}
}
4. Test23.java
public class Test23{
public static class LNode{
int data;
LNode next;
public LNode(int data){
this.data = data;
}
}
public static LNode meetingNode(LNode head)
{
if(head==null)
return null;
LNode quick = head;
LNode slow = head;
if(quick==null||quick.next==null||quick.next.next==null)
return null;
while(quick!=null&&quick.next!=null&quick.next.next!=null){
quick = quick.next.next;
slow = slow.next;
if(quick==slow)
break;
}
return quick;
}
public static int crlnum(LNode head,LNode meetingNode)
{
if(head==null)
return 0;
LNode p = meetingNode.next;
int num = 1;
while(p!=meetingNode) {
p = p.next;
++num;
}
return num;
}
public static LNode rkNode(LNode head,int num)
{
if(head==null)
return null;
LNode quick = head;
LNode slow = head;
int i = 1;
while (i<=num)
{
quick = quick.next;
++i;
}
while(quick != slow)
{
quick = quick.next;
slow = slow.next;
}
return slow;
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
LNode n2 = new LNode(2);
LNode n3 = new LNode(3);
LNode n4 = new LNode(4);
LNode n5 = new LNode(5);
LNode n6 = new LNode(6);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
n6.next = n3;
LNode head = n1;
int num = crlnum(head,meetingNode(head));
System.out.println(num);
LNode Node = rkNode(head,num);
System.out.println(Node.data);
}
}
5. Test24.java
public class Test24{
public static class LNode{
int data;
LNode next;
public LNode(int data){
this.data = data;
}
}
public static LNode reverseLink(LNode head)
{
if(head==null)
return null;
LNode pre = null;
LNode cur = head;
LNode net = head.next;
if(head.next==null)
return head;
while(net!=null)
{
cur.next = pre;
pre = cur;
cur = net;
net = net.next;
}
return cur;
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
// LNode n2 = new LNode(2);
// LNode n3 = new LNode(3);
// LNode n4 = new LNode(4);
n1.next = null;
// n1.next = n2;
// n2.next = n3;
// n3.next = n4;
// n4.next = null;
LNode head = n1;
LNode node = reverseLink(head);
System.out.println(node.data);
}
}
6. Test25.java
public class Test25{
public static class LNode{
int data;
LNode next;
public LNode(int data){
this.data = data;
}
}
public static LNode sumsort(LNode head1,LNode head2)
{
if(head1==null&&head2==null)
return null;
if(head1==null)
return head2;
if(head2==null)
return head1;
LNode cur1 = head1;
//LNode net1 = head1.next;
LNode cur2 = head2;
//LNode net2 = head2.next;
LNode head = null;
if(cur1!=null&&cur2!=null&&cur1.data<=cur2.data) {
head = cur1;
cur1 = cur1.next;
head.next = sumsort(cur1,cur2);
}
else if(cur1!=null&&cur2!=null&&cur2.data<cur1.data){
head = cur2;
cur2 = cur2.next;
head.next = sumsort(cur1,cur2);
}
return head;
}
public static void printLink(LNode head)
{
if(head==null)
return;
LNode p = head;
while(p!=null) {
System.out.print(p.data);
p = p.next;
}
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
LNode n2 = new LNode(3);
LNode n3 = new LNode(4);
LNode n4 = new LNode(2);
LNode n5 = new LNode(5);
LNode n6 = new LNode(6);
n1.next = n2;
n2.next = n3;
n3.next = null;
n4.next = n5;
n5.next = n6;
n6.next = null;
LNode head1 = n1;
LNode head2 = n4;
printLink(sumsort(head1,head2));
}
}
7. Test26.java
public class Test26{
public static class BinaryTree{
double data;
BinaryTree left;
BinaryTree right;
public BinaryTree(double data)
{
this.data = data;
}
}
public static boolean samesubTree(BinaryTree root1,BinaryTree root2)
{
if(root1==null||root2==null)
return false;
boolean result = false;
if(equal(root1.data,root2.data)) {
result = samesubTree2(root1.left, root2.left);
if (!result)
result = samesubTree(root1.left, root2);
else if (!result)
result = samesubTree(root1.right, root2);
}
return result;
}
public static boolean samesubTree2(BinaryTree root1,BinaryTree root2)
{
if(root2==null)
return true;
if(root1==null)
return false;
boolean result = false;
if(!equal(root1.data,root2.data))
return false;
else
return samesubTree2(root1.left,root2.left)&&samesubTree2(root1.right, root2.right);
}
public static boolean equal(double d1,double d2)
{
if((d1-d2>-0.0000001)&&(d1-d2)<0.0000001)
return true;
else
return false;
}
public static void main(String[] args)
{
BinaryTree t1 = new BinaryTree(8);
BinaryTree t2 = new BinaryTree(8);
BinaryTree t3= new BinaryTree(7);
BinaryTree t4 = new BinaryTree(9);
BinaryTree t5 = new BinaryTree(2);
BinaryTree t6 = new BinaryTree(4);
BinaryTree t7 = new BinaryTree(7);
BinaryTree t8 = new BinaryTree(8);
BinaryTree t9 = new BinaryTree(9);
BinaryTree t10 = new BinaryTree(2);
t1.left = t2;
t1.right = t3;
t2.left = t4;
t2.right = t5;
t5.left = t6;
t6.right = t7;
t4.left = null;
t4.right = null;
t6.right = null;
t6.left = null;
t7.left = null;
t7.right = null;
t8.left = t9;
t8.right = t10;
t9.left = null;
t9.right = null;
t10.left = null;
t10.right = null;
System.out.println(samesubTree(t1,t8));
}
}
8. Test27.java
public class Test27{
public static class BinaryTree{
int data;
BinaryTree left;
BinaryTree right;
public BinaryTree(int data)
{
this.data = data;
}
}
public static void reverse(BinaryTree root)
{
if(root==null)
return ;
if(root.left==null&&root.right==null)
return ;
if(root.left!=null&&root.right!=null) {
BinaryTree node = root.left;
root.left = root.right;
root.right = node;
reverse(root.left);
reverse(root.right);
}
else if(root.left==null) {
root.left = root.right;
root.right = null;
reverse(root.left);
}
else if(root.right == null)
{
root.right = root.left;
root.left = null;
reverse(root.right);
}
}
public static void print(BinaryTree root)
{
if(root==null)
return;
System.out.println(root.data);
print(root.left);
print(root.right);
}
public static void main(String[] args)
{
BinaryTree t1 = new BinaryTree(8);
BinaryTree t2 = new BinaryTree(8);
BinaryTree t3 = new BinaryTree(7);
BinaryTree t4 = new BinaryTree(9);
BinaryTree t5 = new BinaryTree(2);
BinaryTree t6 = new BinaryTree(4);
BinaryTree t7 = new BinaryTree(7);
t1.left = t2;
t1.right = t3;
t2.left = t4;
t2.right = t5;
t5.left = t6;
t5.right = t7;
t4.left = null;
t4.right = null;
t6.right = null;
t6.left = null;
t7.left = null;
t7.right = null;
// t1.left = null;
// t1.right = null;
reverse(t1);
print(t1);
}
}
9. Test28.java
public class Test28{
public static class BinaryTree{
int data;
BinaryTree left;
BinaryTree right;
public BinaryTree(int data)
{
this.data = data;
}
}
public static boolean isSymmerial(BinaryTree root)
{
if(root==null)
return true;
else
return isSymmerial(root,root);
}
public static boolean isSymmerial(BinaryTree root1,BinaryTree root2)
{
if(root1==null&&root2==null)
return true;
if(root1==null||root2==null)
return false;
if(root1.data!=root2.data)
return false;
else
return isSymmerial(root1.left,root2.right)&&isSymmerial(root1.right,root2.left);
}
public static void main(String[] args)
{
BinaryTree t1 = new BinaryTree(8);
BinaryTree t2 = new BinaryTree(8);
BinaryTree t3 = new BinaryTree(7);
BinaryTree t4 = new BinaryTree(9);
BinaryTree t5 = new BinaryTree(2);
BinaryTree t6 = new BinaryTree(4);
BinaryTree t7 = new BinaryTree(7);
t1.left = t2;
t1.right = t3;
t2.left = t4;
t2.right = t5;
t5.left = t6;
t5.right = t7;
t4.left = null;
t4.right = null;
t6.right = null;
t6.left = null;
t7.left = null;
t7.right = null;
System.out.println(isSymmerial(t1));
}
}
10. Test29.java
public class Test29{
public static void printMatrix(int[][] numbers,int rows,int cols)
{
if(numbers==null||rows<1||cols<1)
return;
int start = 0;
while(rows>start*2&&cols>start*2) {
printMatrix1(numbers, rows, cols, start);
start++;
}
return;
}
public static void printMatrix1(int[][] numbers,int rows,int cols,int start){
int endX = cols-1-start;
int endY = rows-1-start;
for(int i = start;i<=endX;i++)
{
print(numbers[start][i]);
}
if(start<endY) {
for (int i = start + 1; i <= endY; i++) {
print(numbers[i][endX]);
}
}
if(start<endX)
{
for(int i=endX-1;i>=start;i--)
{
print(numbers[endY][i]);
}
}
if(start<endY-1&&start<endX)
{
for(int i=endY-1;i>start;i--)
print(numbers[i][start]);
}
return;
}
public static void print(int num)
{
System.out.print(num);
}
public static void main(String[] args)
{
int[][] numbers = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
printMatrix(numbers,4,4);
}
}