【Leetcode】Swap Nodes in Pairs in JAVA 难得的一次写对不带改的。。附赠测试程序like always

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

给一个链表,两两交换。

我的思路是两对两对看,但是分为三种情况,这对和后面的情形是:1-2-3-4;1-2-3;1-2;

然后根据不同的情况写出他们的交换策略~代码里有注释,看不懂的可以看注释明白我的思路~

附赠main函数供各位测试!!!

public class SwapNodesinPairs {
	public static void main(String args[]){
		SwapNodesinPairs dp = new SwapNodesinPairs();
		ListNode head  = new ListNode(1);
		ListNode p1 = new ListNode(2);
		head.next=p1;
		ListNode p2  = new ListNode(3);
		p1.next =  p2;
		ListNode p3 = new ListNode(4);
		p2.next = p3;
		ListNode p4 = new ListNode(5);
		p3.next = p4;
		prinf(head);
		prinf(dp.swapPairs(head));
	}
	
	private static void prinf(ListNode input){
		while(input!=null)	{
			System.out.print(input.val+"->");
			input = input.next;
		}
		System.out.println();
	}

	public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null)	return head;
        ListNode p=head;
        ListNode start = head.next;
        while(p!=null&&p.next!=null){
        	ListNode nxt = p.next;
        	//如果后面四个都有,2连向1,1连向4,4连向3,然后切记让下一轮的p得等于3(但是由于这个算法会跳过3,所以提前存好3)
        	if(p.next.next!=null&&p.next.next.next!=null){
        	p.next=nxt.next.next;
        	ListNode tmp = nxt.next;//提前存好3
        	nxt.next=p;
        	p=tmp;//让下一个p等于3,如果p.next的话下一个就是4了,而且3被永远跳过了
        	}
        	//如果后面有三个,就是这一对完了,还剩一个,那么最后那个不动,这两个交换
        	else if(p.next.next!=null&&p.next.next.next==null){
        		p.next=nxt.next;
        		nxt.next=p;
        		p=p.next;
        	}
        	//就剩最后两个,自己交换即可
        	else if(p.next.next==null){
        		nxt.next=p;
        		p.next=null;
        	}
        }
        return start;
    }
}


题目描述: 给定一个由 n 个节点组成的树,每个节点都有一个权值。定义一个节点的权值为其子树中所有节点的权值之和。请你返回所有满足下列条件的节点的权值之和: 该节点位于树的重心以上,即如果将该节点删除后,剩余各个连通分量的节点权值最大值最小。 如果节点数为偶数,则要求上述节点在剩余节点中,左右两部分节点数目相同;如果节点数为奇数,则要求左部分节点数目比右部分节点数目多一。 示例 1: 输入:edges = [[1,2],[1,3],[1,4],[4,5]], weight = [2,3,1,4] 输出:15 解释:树上的节点权值为 [,2,3,1,4] 。重心是节点 1 ,删除后为两个子树 [2,3,4] 和 [5] 。剩余节点权值分别为 9 和 4,均最小化。 示例 2: 输入:edges = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9]], weight = [1,2,3,4,5,6,7,8,9] 输出:31 解释:树上的节点权值为 [,1,2,3,4,5,6,7,8,9] 。重心是节点 5 ,删除后为两个子树 [1,2,3,4,6,7,8,9] 和 [] 。剩余节点权值分别为 33 和 ,均最小化。 提示: 1 <= n <= 10^5 edges.length == n - 1 edges[i].length == 2 1 <= edges[i][], edges[i][1] <= n 1 <= weight.length <= n 1 <= weight[i] <= 10^5 解题思路: 题目要求我们找到树的重心,然后删除重心以上的节点,使得剩余各个连通分量的节点权值最大值最小。 首先,我们需要知道什么是树的重心。树的重心是指树上的一个节点,如果将该节点删除后,剩余各个连通分量的节点数最大值最小,那么这个节点就是树的重心。 我们可以使用两次 DFS 来解决这个问题。第一次 DFS 用来求出树的重心,第二次 DFS 用来计算删除重心以上的节点后,剩余各个连通分量的节点权值之和。 具体来说,我们可以先从任意一个节点开始,进行一次 DFS,求出以该节点为根的子树中的节点数和子树中所有节点的权值之和。然后,我们可以再进行一次 DFS,求出以该节点为根的子树中,删除该节点后,剩余各个连通分量的节点数最大值。我们可以使用一个数组 subsize 来记录每个节点的子树大小,使用一个数组 sum 来记录每个节点的子树中所有节点的权值之和。我们可以使用一个变量 ans 来记录删除重心以上的节点后,剩余各个连通分量的节点权值之和的最小值。 在第一次 DFS 中,我们可以使用一个变量 maxsubsize 来记录以当前节点为根的子树中,最大的子树大小。我们可以使用一个变量 totsize 来记录以当前节点为根的子树中,所有节点的总数。我们可以使用一个变量 cursum 来记录以当前节点为根的子树中,所有节点的权值之和。我们可以使用一个变量 curans 来记录删除当前节点后,剩余各个连通分量的节点数最大值。具体来说,我们可以枚举当前节点的每个子节点,然后递归地计算以该子节点为根的子树中,最大的子树大小。我们可以使用一个变量 cursize 来记录以该子节点为根的子树中,所有节点的总数。我们可以使用一个变量 subsum 来记录以该子节点为根的子树中,所有节点的权值之和。然后,我们可以使用 maxsubsize 来更新以当前节点为根的子树中,最大的子树大小。我们可以使用 totsize 来更新以当前节点为根的子树中,所有节点的总数。我们可以使用 cursum 来更新以当前节点为根的子树中,所有节点的权值之和。最后,我们可以使用一个变量 maxsize 来记录当前节点的父节点到当前节点这条路径上,最大的子树大小。我们可以使用一个变量 parentsize 来记录当前节点的父节点的子树大小。然后,我们可以使用 maxsize 和 totsize - cursize 来计算删除当前节点后,剩余各个连通分量的节点数最大值。最后,我们可以使用 curans 来更新 ans。 在第二次 DFS 中,我们可以使用一个变量 maxsubsize 来记录以当前节点为根的子树中,最大的子树大小。我们可以使用一个变量 totsize 来记录以当前节点为根的子树中,所有节点的总数。我们可以使用一个变量 cursum 来记录以当前节点为根的子树中,所有节点的权值之和。我们可以使用一个变量 parentsize 来记录当前节点的父节点的子树大小。具体来说,我们可以枚举当前节点的每个子节点,然后递归地计算以该子节点为根的子树中,最大的子树大小。我们可以使用一个变量 cursize 来记录以该子节点为根的子树中,所有节点的总数。我们可以使用一个变量 subsum 来记录以该子节点为根的子树中,所有节点的权值之和。然后,我们可以使用 maxsubsize 来更新以当前节点为根的子树中,最大的子树大小。我们可以使用 totsize 来更新以当前节点为根的子树中,所有节点的总数。我们可以使用 cursum 来更新以当前节点为根的子树中,所有节点的权值之和。最后,我们可以使用 parentsize 和 totsize - cursize 来计算删除当前节点后,剩余各个连通分量的节点数最大值。如果当前节点不是树的重心,那么我们可以使用 ans 来更新剩余各个连通分量的节点权值之和的最小值。 最后,我们可以返回 ans。 Java 代码: class Solution { int[] subsize; int[] sum; int ans = Integer.MAX_VALUE; public int getCenter(int[][] edges, int[] weight) { int n = weight.length; subsize = new int[n]; sum = new int[n]; dfs1(, -1, edges, weight); dfs2(, -1, edges, weight); return ans; } private void dfs1(int u, int p, int[][] edges, int[] weight) { subsize[u] = 1; sum[u] = weight[u]; int maxsubsize = ; int totsize = 1; int cursum = weight[u]; int curans = ; for (int v : edges[u]) { if (v == p) { continue; } dfs1(v, u, edges, weight); int cursize = subsize[v]; int subsum = sum[v]; subsize[u] += cursize; sum[u] += subsum; maxsubsize = Math.max(maxsubsize, cursize); totsize += cursize; cursum += subsum; int maxsize = Math.max(cursize, subsize[u] - cursize); int parentsize = totsize - cursize; curans = Math.max(curans, Math.min(maxsize, parentsize)); } int maxsize = Math.max(maxsubsize, totsize - maxsubsize); if (maxsize < ans) { ans = maxsize; } } private void dfs2(int u, int p, int[][] edges, int[] weight) { subsize[u] = 1; sum[u] = weight[u]; int maxsubsize = ; int totsize = 1; int cursum = weight[u]; int parentsize = p == -1 ? : subsize[p]; for (int v : edges[u]) { if (v == p) { continue; } int cursize = subsize[v]; int subsum = sum[v]; subsize[u] += cursize; sum[u] += subsum; maxsubsize = Math.max(maxsubsize, cursize); totsize += cursize; cursum += subsum; int maxsize = Math.max(cursize, subsize[u] - cursize); int childsize = totsize - cursize; int curans = Math.max(Math.min(maxsize, parentsize + childsize), Math.min(subsize[v], totsize - subsize[v])); if (curans < ans) { ans = curans; } dfs2(v, u, edges, weight); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值