2019-02-15到2019-03-13准备笔试代码内容

1.二分法查找元素在不在数组里面。递归和非递归方法


package test;
import java.util.Scanner;

public class CutTwoSearch {
	
	public static String search(Integer[] arr,Integer num) {
		
		int low=0;
		int hight=arr.length-1;
		int mid=0;
		while(hight>low) {
			mid=(hight+low)/2;
			if(arr[mid]==num) {
				return "数组下标是"+mid+"最后一个比较元素是"+arr[mid];
			}
			if(arr[mid]>num) {
				hight=mid-1;
			}
			if(arr[mid]<num) {
				low=mid+1;
			}
		}
		return "没有这个数字哦";
	}
public static String search02(Integer[] arr,Integer num,Integer low,Integer hight) {
		while(hight>low) {
			int mid=(hight+low)/2;
			if(arr[mid]>num) {
				hight=mid-1;
				return search02(arr,num,low,hight);
			}
			if(arr[mid]<num) {
				low=mid+1;
				return search02(arr,num,low,hight);
			}
			if(arr[mid]==num) {
				return "找到了";
			}
		}
		return "没有这个数字哦";
	}
	public static void main(String[] args) {
		Integer[] arr=new Integer[] {0,1,3,6,8,9,15,18,35,39,46,48,51,53,68,69,75,79,88,92,94};
		Scanner s = new Scanner(System.in);
		Integer num=Integer.parseInt(s.nextLine());
		System.out.println(num);
		System.out.println(CutTwoSearch.search02(arr, num,0,arr.length));
	}
}

2.求输入数字是否为数组中两个元素的和。

package test0313;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class test {
	public static void main(String[] args) {
		Integer[] arr=new Integer[] {2,7,15,56,85,99,102,103,106,254};
		Scanner scanner=new Scanner(System.in);
		Integer num=scanner.nextInt();
		System.out.println(findFrom(arr,num)[0]+"sss"+findFrom(arr,num)[1]);
	}

	private static  Integer[] findFrom(Integer[] arr, Integer num) {
		Map<Integer,Integer> map=new HashMap<>();
		for (int i = 0; i < arr.length; i++) {
			Integer integer = arr[i];
			Integer complement=num-integer;
			if(map.containsKey(complement)) {
				return new Integer[] {map.get(complement),i};
			}
			map.put(integer, i);
		}
		return null;
	}
}

3.求最大不重复子字符串长度,复杂度o(n)

package test0313;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
 * 求最大不重复子串,两个小时没做出来。
* @ClassName: test01types
* @Description:
* @author xiawei
* @date 2019年3月14日 上午8:28:01
 */
public class test01 {
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
	    while(true) {
	    	 String inNum=scanner.nextLine();
	    	if(inNum.equals("exit")) {
	    		break;
	    	}
//	    	System.out.println(findLongestXW(new HashMap<>(),0,inNum,0).toString());     
	    	System.out.println(lengthOfLongestSubstring(inNum));     
	    }
	}
	public static int lengthOfLongestSubstring(String s) {
		//abacdaefgh
        int n = s.length(), ans = 0;
        // current index of character 用于记录现有不重复元素的个数和其位置下标长度。key是不重复的元素value是其所在长度
        Map<Character, Integer> map = new HashMap<>(); 
        // try to extend the range [i, j]
        for (int j = 0, i = 0; j < n; j++) {
        	//如果有重复的,找他所在位置的下标长度也就是value
            if (map.containsKey(s.charAt(j))) {
                i =map.get(s.charAt(j));
            }
            //用数组下标减去value就是从一开始到遇到的重复元素的长度。比较这次遇重和上次,看谁大。
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }

	private static String findLongestXW(Map<Character,Integer> map,Integer long1,String inNum,Integer i2) {
		//length递归从哪个下标开始==i。longest最大长度,即走过的下标数。全局变量。j1为上次走过下标数,j为走过下标数,和longest比较
		Integer longest =long1;
		for (int i = i2,j=0; i < inNum.length(); i++) {
			Character a=inNum.charAt(i);
			if(map.containsKey(a)&&j>longest) {
				for (int k =i2; k <= j; k++) {
					map.remove(inNum.charAt(k));
				}
				j=i;
				return findLongestXW(map,j,inNum,i);
			}
			if(map.containsKey(a)) {
				j=i;
				return findLongestXW(map,j,inNum,i);
			}
			if(!map.containsKey(a)) {
				j++;
				while(j>longest) {
					longest=j;
				}
				map.put(a, i);
			}
		}
		return map.toString()+"长度是"+longest;
	}
}

4.Java实现单链表基本操作和addTwoList操作

package test0313;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
/**
 * 单链表的增删改查
* @ClassName: test02types
* @Description:
* @author xiawei
* @date 2019年3月14日 下午6:00:19
 */
public class test02b {
	public Node head;
	public Node q;
	public Node q1;
	public Node q2;
	//新建
	public Node creatList(int a[]) {
		Node head=new Node(0,null);
		Node q=new Node(a[0],null);
		head.next=q;
		for (int i = 1; i < a.length; i++) {
			Node p=new Node(a[i],null);
			q.next=p;
			q=p;
		}
		return head;
	}
	//便利
	public int findList(Node head) {
		int length = 0;
		Node p=head;
		while(p!=null){ 
 		System.out.print(p.data+"/"); 
 		p=p.next; 
 		length++;
		}
		return length;
	}
	//查找
	public int findNode (Node head,int num) {
		while(head.next!=null) {
			if(head.data.equals(num)) {
				return Integer.parseInt(head.data.toString());
			}head=head.next;
		}
		return 0;
	}
	//添加
	public Node addNode (Node head,int num1,int num2) {
		if(head.next!=null) {
			q=head.next;
			if(head.data.equals(num1)) {//如果是第一个结点
				Node p=new Node(num2,head.next);
				head.next=p;
				return head;
			}
			while(null!=q.next) {
				if(!q.data.equals(num1)) {
					q=q.next;
				}else {
					Node p=new Node(num2,q.next);
					p.next=q.next;
					q.next=p;
					return head;
				}
			}
			while(null==q.next) {
				if(q.data.equals(num1)) {
					Node p=new Node(num2,null);
					q.next=p;
					return head;
				}else {
					return null;//没找到插入位置
				}
			}
		}else {
			return null;//此链表头结点为空
		}
		return null;
	}
	//删除
	public Node delNode(Node head,int num) {
		if(head.next!=null) {
			q=head.next;
			if(q.data.equals(num)&&null!=q.next) {
				head.next=q.next;
				return head;
			}
			if(q.data.equals(num)&&null==q.next) {
				head.next=null;
				return head;
			}
			while(!q.data.equals(num)&&null!=q.next.next) {
				if(!q.next.data.equals(num)) {
					q=q.next;
				}else {
					q.next=q.next.next;
					return head;
				}
			}
			while(null==q.next.next) {
				if(q.next.data.equals(num)) {
					q.next=null;
					return head;
				}else {
					return null;//没找到插入位置
				}
			}
		}else {
			return null;//此链表头结点为空
		}
		return head;
	}
	//两个结点相加
	public Node addTwoList(Node head1,Node head2) {
		Node newHead=new Node(null,null);
		if(head1.next==null||head2.next==null) {
			return null;//有链表为空
		}else {
			q1=head1.next;
			q2=head2.next;
			Node q=new Node(Integer.parseInt(q1.data.toString())+Integer.parseInt(q2.data.toString()),null);
			newHead.next=q;
			while(q1.next!=null) {
				Node p=new Node(Integer.parseInt(q1.next.data.toString())+Integer.parseInt(q2.next.data.toString()),null);
				q.next=p;
				q=p;
				q1=q1.next;
				q2=q2.next;
			}
		}
		return newHead;
	}
	public Node change(Node newHead) {
		q=newHead.next;
		while(q.next!=null) {
			if(Integer.parseInt(q.data.toString())/10!=0) {//如果大于10,后面结点+1
				q.data=Integer.parseInt(q.data.toString())%10;
				q.next.data=Integer.parseInt(q.next.data.toString())+1;
			}
			q=q.next;
		}
		if(q.next==null&&Integer.parseInt(q.data.toString())/10!=0) {
			q.data=Integer.parseInt(q.data.toString())%10;
			Node w=new Node(1,null);
			q.next=w;
		}
		return newHead;
	}
	//便利02 逆序输出 和栈一样
	public int findList02(Node head) {
		Stack<Node> stack=new Stack<>();
		int length = 0;
		Node p=head.next;
		while(p!=null) {
			stack.push(p);
			p=p.next;
		}
		while (!stack.isEmpty()) {
			length++;
            System.out.print(stack.pop().data + " ");
        }
		return length;
	}
	public static void main(String[] args) {
		test02b test=new test02b();
		Node head1=test.creatList(new int [] {8,7,6});
		Node head2=test.creatList(new int [] {8,9,8});
		Node head3=test.creatList(new int [] {1,2,3});
		Node head4=test.creatList(new int [] {1,2,3});
		Node head5=test.creatList(new int [] {8,9,3});
		Node head6=test.creatList(new int [] {5,9,3});
//		test.addNode(head, 2, 3);
//		System.out.println("    共有"+test.findList(test.delNode(head,5))+"个结点");
//		System.out.println("    共有"+test.findList02(test.addTwoList(head1, head2))+"个结点");
		System.out.println("    共有"+test.findList02(test.change(test.addTwoList(head1, head2)))+"个结点");
//		System.out.println("    共有"+test.findList(test.addNode(head, 5, 3))+"个结点");
//		System.out.println("    共有"+test.findList(test.creatList(new int [] {1,2,3}))+"个结点");
//		System.out.println(test.findNode(head,2));  test.delNode(head,1)
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值