英文:
print range, if number is missing
问题
我有一个已排序的数字列表,想要打印连续的数字范围,如果系列中有任何缺失的数字,我们将开始一个新的范围。
注意:在系列中可能连续缺少多个数字。
示例:
输入:
列表 = {301,302,303,304,305,307,308,310,312,318,319,322,390}
输出:范围
301 - 305 // 缺少 306,因此中断并开始新范围
307 - 308 // 缺少 309,因此中断
310 // 缺少 311,因此中断
312 // 缺少 313、314、315、316、317,中断(可以缺少一个或多个数字)
318 - 319
322
390
我的逻辑:
int count = list.get(0);
String output = list.get(0) + "-";
for(int n: list){
if(count++ != n){
output = output+(count-2);
System.out.println(output);
output = n+"-";
count++;
}
}
如果在其中缺少超过 2 个数字,这段逻辑会失败。
英文:
I have a list of number which is sorted and want to print the continuous numbers range and if any number is missing in the series, we will start a new range.
Note: There are possibility that multiple numbers are missing in the series continuously.
example:
Input:
list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390}
Output: range
301 - 305 //306 is missing so it breaks and new range starts
307 - 308 //309 is missing so breaks
310 // 311 is missing so breaks
312 // 313, 314, 315, 316, 317 are missing break (1 or more number can be missed)
318 - 319
322
390
My Logic:
int count = list.get(0);
String output = list.get(0) + "-";
for(int n: list){
if(count++ != n){
output = output+(count-2);
System.out.println(output);
output = n+"-";
count++;
}
}
It's failing, if there are more then 2 numbers is missing in between.
答案1
得分: 0
我会使用两个 int
变量来跟踪每个范围的开始和结束。遍历列表时,只要下一个数字是结束数字的后续数字,它就成为新的结束数字。如果不是,您就有了一个新的范围:
public static void main(String[] args) {
int[] list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
int start = list[0];
int end = list[1];
for (int i = 2; i < list.length; ++i) {
if (end + 1 == list[i]) {
end++;
} else {
printRange(start, end);
start = list[i];
end = list[i];
}
}
printRange(start, end);
}
private static void printRange(int start, int end) {
if (start != end) {
System.out.println(start + " - " + end);
}
else {
System.out.println(start);
}
}
英文:
I'd use two int
s to track the beginning and end of each range. Going over the list, as long as the next number is the following number of the end, it becomes the new end. If it isn't, you have a new range at hand:
public static void main(String[] args) {
int[] list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
int start = list[0];
int end = list[1];
for (int i = 2; i < list.length; ++i) {
if (end + 1 == list[i]) {
end++;
} else {
printRange(start, end);
start = list[i];
end = list[i];
}
}
printRange(start, end);
}
private static void printRange(int start, int end) {
if (start != end) {
System.out.println(start + " - " + end);
}
else {
System.out.println(start);
}
}
答案2
得分: 0
你可以避免使用count
,只与上一个值进行比较。 伪代码:
起始 = 0
对于 (i=1; i<=n; i++) {
如果 ((i==n) || (list[i]!=list[i-1]+1)) {
输出区间 列表[起始] - 列表[i-1]
//省略第二个条件 如果 i-1==start
起始 = i
}
}
英文:
You can avoid using count
and make only comparison with the last value. Pseudocode:
start = 0
for (i=1; i<=n; i++) {
if ((i==n) || (list[i]!=list[i-1]+1)) {
output interval list[start] - list[i-1]
//omit the second if i-1==start
start = i
}
}
答案3
得分: 0
这是Python中的简单逻辑 -
list = [301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390]
def printRange(list):
n = len(list)
i = 0
while i < n:
flag = False
j = i
k = 0
# 假设序列中每个数字之间的差值为1
while j + 1 < n and list[j] + 1 == list[j + 1]:
j += 1
k += 1
flag = True
if flag:
print('{} - {}'.format(list[i], list[j]))
i += k + 1
else:
print(list[i])
i += 1
printRange(list)
英文:
Here is simple logic in python -
list = [301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390]
def printRange(list):
n = len(list)
i = 0
while i < n:
flag = False
j = i
k = 0
# Assuming the sequence is, difference between each number is 1
while j + 1 < n and list[j] + 1 == list[j + 1]:
j += 1
k += 1
flag = True
if flag:
print('{} - {}'.format(list[i], list[j]))
i += k + 1
else:
print(list[i])
i += 1
printRange(list)
答案4
得分: 0
我们使用双指针来判断序列是否连续。
public static void main(String[] args) {
int[] arr = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
printRange(arr);
}
private static void printRange(int[] arr) {
int pre = arr[0];
int start = arr[0];
for (int i = 1; i < arr.length; i++) {
while (i < arr.length - 1 && arr[i] == pre + 1) {
pre = arr[i];
i++;
}
// 处理最后一个数
if (i == arr.length - 1) {
if (arr[i] == pre + 1) {
System.out.println(start + " - " + arr[i]);
} else {
System.out.println(start);
System.out.println(arr[i]);
}
break;
}
if (pre != start) {
System.out.println(start + " - " + pre);
} else {
System.out.println(start);
}
pre = arr[i];
start = arr[i];
}
}
英文:
We use double pointers to determine whether the sequence is continuous.
public static void main(String[] args) {
int[] arr = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
printRange(arr);
}
private static void printRange(int[] arr) {
int pre = arr[0];
int start = arr[0];
for (int i = 1; i < arr.length; i++) {
while (i < arr.length - 1 && arr[i] == pre + 1) {
pre = arr[i];
i++;
}
// deal with last number
if (i == arr.length - 1) {
if (arr[i] == pre + 1) {
System.out.println(start + " - " + arr[i]);
} else {
System.out.println(start);
System.out.println(arr[i]);
}
break;
}
if (pre != start) {
System.out.println(start + " - " + pre);
} else {
System.out.println(start);
}
pre = arr[i];
start = arr[i];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论