程序员面试金典

package com.zte.array1;

import java.util.HashSet;

//程序员面试金典链表
public class Main3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/*
	 * 1.移除不重复链表中的重复节点
	 */
	public static ListNode delete(ListNode head) {
		HashSet<Integer> set = new HashSet<>();
		ListNode currrent = null;
		ListNode parent = head;
		while (parent != null) {
			if (set.contains(parent.val)) {
				currrent.next = parent.next;
			} else {
				set.add(parent.val);
				currrent = parent;
				parent = parent.next;
			}
		}
		return head;
	}

	/*
	 * 2.实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
	 */
	public static int kthToLast(ListNode head, int k) {
		ListNode front = head;
		ListNode after = head;
		if (head == null && head.next != null) {
			return -1;
		} else {
			for (int i = 0; i < k; i++) {
				front = front.next;
			}
			while (front != null) {
				front = front.next;
				after = after.next;
			}
		}
		return after.val;

	}

	/*
	 * 3.实现一种算法,删除单向链表中间的某个节点(除了第一个和最后一个节点,不一定是中间节点),假定你只能访问该节点
	 */

	public static void deleteNode(ListNode node) {
		if (node != null && node.next != null) {
			ListNode nodeNext = node.next;
			node.val = nodeNext.val;
			node.next = nodeNext.next;
		}

	}

	/*
	 * 4.分隔列表
	 */
	public static ListNode partition(ListNode head, int x) {
		ListNode before = null;
		ListNode after = null;
		while (head != null) {
			if (x > head.val) {
				if (before == null) {
					before = head;
					head = head.next;
				} else {
					ListNode end = before;
					while (end.next != null)
						end = before.next;
					end.next = head;
					head = head.next;
				}
			} else {
				if (after == null) {
					after = head;
					head = head.next;
				} else {
					ListNode end = after;
					while (end.next != null)
						end = end.next;
					end.next = head;
					head = head.next;
				}
			}
		}
		while (before.next != null) {
			before = before.next;
		}
		before.next = after;
		return before;

	}

	public static ListNode partition1(ListNode head, int x) {
		ListNode beforestart = null;
		ListNode beforeend = null;
		ListNode afterstart = null;
		ListNode afterend = null;
		while (head != null) {
			if (x > head.val) {
				if (beforestart == null) {
					beforestart = head;
					beforeend = head;
				} else {
					beforeend.next = head;
					beforeend = head;
				}
				head = head.next;
			} else {
				if (afterstart == null) {
					afterstart = head;
					afterend = head;
				} else {
					afterend.next = head;
					afterend = head;
				}
				head = head.next;
			}
		}
		if (beforeend == null)
			return afterstart;
		else {
			beforeend.next = afterstart;
			return beforestart;
		}

	}
}

class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		val = x;
	}
}

package com.zte.array1;

import java.util.Stack;

public class Main4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int n = 3;
		// int[][] graph = { { 0, 1 }, { 0, 2 }, { 1, 2 }, { 1, 2 } };
		// int start = 0;
		// int target = 2;
		int n = 12;
		int[][] graph = { { 0, 1 }, { 1, 2 }, { 1, 3 }, { 1, 10 }, { 1, 11 }, { 1, 4 }, { 2, 4 }, { 2, 6 }, { 2, 9 },
				{ 2, 10 }, { 2, 4 }, { 2, 5 }, { 2, 10 }, { 3, 7 }, { 3, 7 }, { 4, 5 }, { 4, 11 }, { 4, 11 }, { 4, 10 },
				{ 5, 7 }, { 5, 10 }, { 6, 8 }, { 7, 11 }, { 8, 10 } };
		int start = 2;
		int target = 3;
		System.out.println(findWhetherExistsPath(n, graph, start, target));
	}

	/*
	 * 节点间通路。给定有向图,设计一个算法,找出两个节点之间是否存在一条路径。
	 */
	public static boolean findWhetherExistsPath(int n, int[][] graph, int start, int target) {
		int[][] arr = new int[n][n];
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < n; i++) {
			int k = graph[i][0];
			int l = graph[i][1];
			arr[k][l] = 1;
		}
		return isVisitied1(n, arr, start, target, stack);
	}

	public static boolean isVisitied(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
		boolean[] isvisited = new boolean[n];
		isvisited[start] = true;
		for (int i = 0; i < n; i++) {
			if (!isvisited[i] && arr[start][i] != 0 && target == i) {
				return true;
			}
			if (!isvisited[i] && arr[start][i] != 0 && target != i) {
				stack.push(i);
			}
		}
		if (stack.size() != 0) {
			isVisitied(n, arr, stack.pop(), target, stack);
		}
		return false;

	}

	public static boolean findWhetherExistsPath1(int n, int[][] graph, int start, int target) {
		int[][] arr = new int[n][n];
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < n; i++) {
			int k = graph[i][0];
			int l = graph[i][1];
			arr[k][l] = 1;
			arr[l][k] = 1;
		}
		return isVisitied(n, arr, start, target, stack);
	}

	public static boolean isVisitied1(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
		boolean[] isvisited = new boolean[n];
		isvisited[start] = true;
		stack.push(start);
		while (stack.size() != 0) {
			int k = stack.pop();
			System.out.print(k + " ->");
			isvisited[k] = true;
			for (int i = 0; i < n; i++) {
				if (!isvisited[i] && arr[k][i] != 0 && i == target) {
					return true;
				}
				if (!isvisited[i] && arr[k][i] != 0 && i != target) {
					stack.push(i);
				}
			}
		}
		return false;

	}

	public static boolean isVisitied2(int n, int[][] arr, int start, int target, Stack<Integer> stack) {
		boolean[] isvisited = new boolean[n];
		isvisited[start] = true;
		stack.push(start);
		while (stack.size() != 0) {
			int k = stack.pop();
			System.out.print(k + " ->");
			isvisited[k] = true;
			for (int i = 0; i < n; i++) {
				if (!isvisited[i] && arr[k][i] != 0 && i == target) {
					return true;
				}
				if (!isvisited[i] && arr[k][i] != 0 && i != target) {
					stack.push(i);
				}
			}
		}
		return false;

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值