新浪笔试题

一个数组,里面是1——1000的整数,除了1以外的其他数都不能重复,其中1能代表任何数,求问数组a是否是连续的,如,a[5]={4,2,3,1,5}为连续,a[5]={2,3,5,4,7}为不连续


package xinlang;

import java.util.SortedSet;
import java.util.TreeSet;


public class XinLang1 {

	public static int[] a = {4,2,3,1,6,9,1};
	
	private boolean isLinked() {
		int count1 = 0;
		SortedSet<Integer> set = new TreeSet<Integer>();
		for (int i=0; i < a.length; ++i) {
			if (a[i] == 1) {
				count1 ++;
				continue;
			}
			set.add(a[i]);
		}
		int first = set.first();
		set.remove(first);
		while (set.size() != 0) {
			int tmp = set.first();
			int sub = tmp - first;
			if (sub == 1) {
				first = tmp;
				set.remove(first);
				continue;
			}
			count1 = count1 - sub + 1;
			if (count1 < 0) {
				return false;
			}
			first = set.first();
			set.remove(first);
		}
		return true;
	}
	
	public static void main(String[] args) {
		XinLang1 xl = new XinLang1();
		System.out.println(xl.isLinked());
	}
}

欢迎指正~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值