打印范围,在数字缺失时。

huangapple go评论108阅读模式
英文:

print range, if number is missing

问题

我有一个已排序的数字列表,想要打印连续的数字范围,如果系列中有任何缺失的数字,我们将开始一个新的范围。
注意:在系列中可能连续缺少多个数字。
示例:

  1. 输入:
  2. 列表 = {301302303304305307308310312318319322390}
  3. 输出:范围
  4. 301 - 305 // 缺少 306,因此中断并开始新范围
  5. 307 - 308 // 缺少 309,因此中断
  6. 310 // 缺少 311,因此中断
  7. 312 // 缺少 313、314、315、316、317,中断(可以缺少一个或多个数字)
  8. 318 - 319
  9. 322
  10. 390

我的逻辑:

  1. int count = list.get(0);
  2. String output = list.get(0) + "-";
  3. for(int n: list){
  4. if(count++ != n){
  5. output = output+(count-2);
  6. System.out.println(output);
  7. output = n+"-";
  8. count++;
  9. }
  10. }

如果在其中缺少超过 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:

  1. Input:
  2. list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390}
  3. Output: range
  4. 301 - 305 //306 is missing so it breaks and new range starts
  5. 307 - 308 //309 is missing so breaks
  6. 310 // 311 is missing so breaks
  7. 312 // 313, 314, 315, 316, 317 are missing break (1 or more number can be missed)
  8. 318 - 319
  9. 322
  10. 390

My Logic:

  1. int count = list.get(0);
  2. String output = list.get(0) + "-";
  3. for(int n: list){
  4. if(count++ != n){
  5. output = output+(count-2);
  6. System.out.println(output);
  7. output = n+"-";
  8. count++;
  9. }
  10. }

It's failing, if there are more then 2 numbers is missing in between.

答案1

得分: 0

我会使用两个 int 变量来跟踪每个范围的开始和结束。遍历列表时,只要下一个数字是结束数字的后续数字,它就成为新的结束数字。如果不是,您就有了一个新的范围:

  1. public static void main(String[] args) {
  2. int[] list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
  3. int start = list[0];
  4. int end = list[1];
  5. for (int i = 2; i < list.length; ++i) {
  6. if (end + 1 == list[i]) {
  7. end++;
  8. } else {
  9. printRange(start, end);
  10. start = list[i];
  11. end = list[i];
  12. }
  13. }
  14. printRange(start, end);
  15. }
  16. private static void printRange(int start, int end) {
  17. if (start != end) {
  18. System.out.println(start + " - " + end);
  19. }
  20. else {
  21. System.out.println(start);
  22. }
  23. }
英文:

I'd use two ints 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:

  1. public static void main(String[] args) {
  2. int[] list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
  3. int start = list[0];
  4. int end = list[1];
  5. for (int i = 2; i &lt; list.length; ++i) {
  6. if (end + 1 == list[i]) {
  7. end++;
  8. } else {
  9. printRange(start, end);
  10. start = list[i];
  11. end = list[i];
  12. }
  13. }
  14. printRange(start, end);
  15. }
  16. private static void printRange(int start, int end) {
  17. if (start != end) {
  18. System.out.println(start + &quot; - &quot; + end);
  19. }
  20. else {
  21. System.out.println(start);
  22. }
  23. }

答案2

得分: 0

你可以避免使用count,只与上一个值进行比较。 伪代码:

  1. 起始 = 0
  2. 对于 (i=1; i<=n; i++) {
  3. 如果 ((i==n) || (list[i]!=list[i-1]+1)) {
  4. 输出区间 列表[起始] - 列表[i-1]
  5. //省略第二个条件 如果 i-1==start
  6. 起始 = i
  7. }
  8. }
英文:

You can avoid using count and make only comparison with the last value. Pseudocode:

  1. start = 0
  2. for (i=1; i&lt;=n; i++) {
  3. if ((i==n) || (list[i]!=list[i-1]+1)) {
  4. output interval list[start] - list[i-1]
  5. //omit the second if i-1==start
  6. start = i
  7. }
  8. }

答案3

得分: 0

这是Python中的简单逻辑 -

  1. list = [301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390]
  2. def printRange(list):
  3. n = len(list)
  4. i = 0
  5. while i < n:
  6. flag = False
  7. j = i
  8. k = 0
  9. # 假设序列中每个数字之间的差值为1
  10. while j + 1 < n and list[j] + 1 == list[j + 1]:
  11. j += 1
  12. k += 1
  13. flag = True
  14. if flag:
  15. print('{} - {}'.format(list[i], list[j]))
  16. i += k + 1
  17. else:
  18. print(list[i])
  19. i += 1
  20. printRange(list)
英文:

Here is simple logic in python -

  1. list = [301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390]
  2. def printRange(list):
  3. n = len(list)
  4. i = 0
  5. while i &lt; n:
  6. flag = False
  7. j = i
  8. k = 0
  9. # Assuming the sequence is, difference between each number is 1
  10. while j + 1 &lt; n and list[j] + 1 == list[j + 1]:
  11. j += 1
  12. k += 1
  13. flag = True
  14. if flag:
  15. print(&#39;{} - {}&#39;.format(list[i], list[j]))
  16. i += k + 1
  17. else:
  18. print(list[i])
  19. i += 1
  20. printRange(list)

答案4

得分: 0

我们使用双指针来判断序列是否连续。

  1. public static void main(String[] args) {
  2. int[] arr = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
  3. printRange(arr);
  4. }
  5. private static void printRange(int[] arr) {
  6. int pre = arr[0];
  7. int start = arr[0];
  8. for (int i = 1; i < arr.length; i++) {
  9. while (i < arr.length - 1 && arr[i] == pre + 1) {
  10. pre = arr[i];
  11. i++;
  12. }
  13. // 处理最后一个数
  14. if (i == arr.length - 1) {
  15. if (arr[i] == pre + 1) {
  16. System.out.println(start + " - " + arr[i]);
  17. } else {
  18. System.out.println(start);
  19. System.out.println(arr[i]);
  20. }
  21. break;
  22. }
  23. if (pre != start) {
  24. System.out.println(start + " - " + pre);
  25. } else {
  26. System.out.println(start);
  27. }
  28. pre = arr[i];
  29. start = arr[i];
  30. }
  31. }
英文:

We use double pointers to determine whether the sequence is continuous.

  1. public static void main(String[] args) {
  2. int[] arr = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390};
  3. printRange(arr);
  4. }
  5. private static void printRange(int[] arr) {
  6. int pre = arr[0];
  7. int start = arr[0];
  8. for (int i = 1; i &lt; arr.length; i++) {
  9. while (i &lt; arr.length - 1 &amp;&amp; arr[i] == pre + 1) {
  10. pre = arr[i];
  11. i++;
  12. }
  13. // deal with last number
  14. if (i == arr.length - 1) {
  15. if (arr[i] == pre + 1) {
  16. System.out.println(start + &quot; - &quot; + arr[i]);
  17. } else {
  18. System.out.println(start);
  19. System.out.println(arr[i]);
  20. }
  21. break;
  22. }
  23. if (pre != start) {
  24. System.out.println(start + &quot; - &quot; + pre);
  25. } else {
  26. System.out.println(start);
  27. }
  28. pre = arr[i];
  29. start = arr[i];
  30. }
  31. }

huangapple
  • 本文由 发表于 2020年10月10日 02:15:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64285277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定