Java List 常见编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kaikai_sk/article/details/82989995

最大面积

题意是给你 a1, a2, …, an 这 n 个数,代表 (i, ai) 坐标,让你从中找两个点与 x 轴围成的容器可以容纳最多的水。
在这里插入图片描述

public int maxArea(int[] height)
	{
		int left = 0,right = height.length - 1;
		int max = 0, h = 0;
		while(left < right)
		{
			h = Math.min(height[left],height[right]);
			max  = Math.max(max, (right-left)*h);
			while(height[left]<= h && left < right)
				left++;
			while(height[right] <= h && left <right)
				right--;
		}
		return max;
	}

合并区间

Description
Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considerred overlapping.
Tags: Array, Sort

题意是给你一组区间,让你把区间合并成没有交集的一组区间。我们可以把区间按 start 进行排序,然后遍历排序后的区间,如果当前的 start 小于前者的 end,那么说明这两个存在交集,我们取两者中较大的 end 即可;否则的话直接插入到结果序列中即可。

class Interval 
{
    int start;
     int end;
     Interval() { start = 0; end = 0; }
     Interval(int s, int e) { start = s; end = e; }
}

class Solution
{
	public static void printIntervalList(List<Interval> list)
	{
		for (Interval interval :list)
		{
			System.out.print("["+interval.start+","+interval.end+"]"+" ");
		}
		System.out.println();
	}
	public static void main(String[] args) 
	{
		List<Interval> intervals = new ArrayList<>();
		intervals.add(new Interval(1,3));
		intervals.add(new Interval(2,6));
		intervals.add(new Interval(8,10));
		intervals.add(new Interval(15,18));
		printIntervalList(new Solution().merge(intervals));
	}
	
	public List<Interval> merge(List<Interval> intervals)
	{
		if(intervals == null || intervals.size()==0)
			return intervals;
		
		intervals.sort(new Comparator<Interval>() 
		{
			@Override
			public int compare(Interval o1, Interval o2) 
			{
				if (o1.start < o2.start)
					return -1;
				if (o1.start > o2.start)
					return 1;
				return 0;
			}
			
		});
		
		int start = intervals.get(0).start;
		int end = intervals.get(0).end;
		List<Interval> ans = new ArrayList<>();
		
		for(Interval interval : intervals)
		{
			if(interval.start <= end)
				end = Math.max(end, interval.end);
			else
			{
				ans.add(new Interval(start,end));
				start = interval.start;
				end = interval.end;
			}
		}
		ans.add(new Interval(start,end));
		return ans;
		
	}

判断回文数

一半长度

??
什么时候会走 halfReverseX == x这个return 路径呢??
??

好好思考下是否需要计算整个长度,比如 1234321,其实不然,我们只需要计算一半的长度即可,就是在计算过程中的那个逆序数比不断除 10 的数大就结束计算即可,但是这也带来了另一个问题,比如 10 的倍数的数 10010,它也会返回 true,所以我们需要对 10 的倍数的数再加个判断即可,代码如下所示。

public boolean isPalindrome(int x)
	{
		if (x<0 || (x!= 0 && x%10 == 0))
		{
			return false;
		}
		int halfReverseX = 0;
		while(x > halfReverseX)
		{
			halfReverseX = halfReverseX * 10 + x%10;
			x /= 10;
		}
		return halfReverseX == x || halfReverseX / 10 == x;
			
	}

直接求出回文数后进行判断

class Solution 
{
    public int reverse(int x)
	{
		long res = 0;
		for (;x!=0;x/=10)
		{
			res = res * 10 + x%10; 
		}
		if(res > Integer.MAX_VALUE || res<Integer.MIN_VALUE)
			return 0;
		else
			return (int) res;
	}
    
    public boolean isPalindrome(int x) 
    {
        if(x<0)
			return false;
		int reverseX = reverse(x);
		if (reverseX == x)
			return true;
		return false;
    }
}

整数反转

Description
Given a 32-bit signed integer, reverse digits of an integer.

Example 1:
Input: 123
Output: 321

Example 2:
Input: -123
Output: -321

扫描二维码关注公众号,回复: 3746785 查看本文章

Example 3:
Input: 120
Output: 21

Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution
{
	public static void main(String[] args) 
	{
		System.out.println(new Solution().reverse(-123));
	}
	
	public int reverse(int x)
	{
		long res = 0;
		for (;x!=0;x/=10)
		{
			res = res * 10 + x%10; 
		}
		if(res > Integer.MAX_VALUE || res<Integer.MIN_VALUE)
			return 0;
		else
			return (int) res;
	}
	
	
//	public int reverse(int x)
//	{
//		long res = 0;
//		for( ; x!=0 ; x/=10)
//		{
//			res = res * 10 + x%10;
//		}
//		
//		if (res>Integer.MAX_VALUE || res < Integer.MIN_VALUE)
//			return 0;
//		else
//			return (int )res;
//	}
}

两个大数的加法

以链表表示一个数,低位在前,高位在后,所以题中的例子就是 342 + 465 = 807,所以我们模拟计算即可。
Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Tags: Linked List, Math

class Solution 
{
	public static void printList(ListNode list)
	{
		ListNode current = list;
		while(current != null)
		{
			System.out.print(current.val + " ");
			current = current.next;
		}
		System.out.println();
	}
	
	public static void main(String[] args) 
	{
		ListNode l1 = new ListNode(2);
		l1.next = new ListNode(4);
		l1.next.next = new ListNode(3);
		
		ListNode l2 = new ListNode(5);
		l2.next = new ListNode(6);
		l2.next.next = new ListNode(4);
		
		printList(new Solution().addTwoNumbers(l1, l2));
	}
	
	public ListNode addTwoNumbers (ListNode list1,ListNode list2)
	{
		ListNode newList = new ListNode(0);
		ListNode currentNode = newList;
		ListNode node1 = list1,node2 = list2;
		int sum =0 ;
		while(node1 != null || node2 != null)
		{
			sum = sum / 10;
			
			if(node1!= null)
			{
				sum += node1.val;
				node1 = node1.next;
			}
			
			if( node2 != null)
			{
				sum += node2.val;
				node2 = node2.next;
			}
			
			currentNode.next = new ListNode( sum% 10);
			currentNode = currentNode.next;
			
		}
		if (sum / 10 > 0)
			currentNode.next = new ListNode(1);
		
		return newList.next;
	
	}

猜你喜欢

转载自blog.csdn.net/kaikai_sk/article/details/82989995