简体   繁体   English

需要帮助来解决具有挑战性的数据结构问题

[英]Need help to solve challenging Data structure problem

I came across this problem where given an array of integers, tell if the sequence of integers will exit the array from left, right or deadend.我遇到了这个问题,给定一个整数数组,判断整数序列是否将从左、右或死端退出数组。 You enter the array from the left and move N indices(where N is the value of the integer) in the specified direction(positive is right, negative is left)您从左侧输入数组并在指定方向移动 N 个索引(其中 N 是整数的值)(正为右,负为左)

Examples
[1,1,1] -> Exits Right
[1,-2]=> Exits Left
[2,0,-1]-> Deadends
[2,5,1,-2,0]-> Exits Right

One solution which comes to my mind is if value of all integers are positive then Exits Right or Exits Left.我想到的一种解决方案是,如果所有整数的值都是正数,那么退出右或退出左。 However this solution does not cover all the scenario.但是,此解决方案并未涵盖所有场景。 I need help to solve this problem.我需要帮助来解决这个问题。

You can do this:你可以这样做:

  • Create a variable to hold index position and initialize it with first value创建一个变量来保存索引 position 并用第一个值初始化它
  • Loop over array and on every iteration, compute new value of index by adding value of pointed index.遍历数组并在每次迭代时,通过添加指向索引的值来计算新的index值。
  • At the end of loop, check:在循环结束时,检查:
    • Right : If index >= array.length` Right :如果index >= array.length`
    • Left : if index < 0 Left :如果index < 0
    • Deadend : If index is in bounds Deadend : 如果index在范围内

Based on this, below is an example using Javascript:基于此,以下是使用 Javascript 的示例:

 function printDirection(arr) { let index = arr[0]; for (let i = 1; i < arr.length; i++) { /** * Below condition covers following cases: * 1. If the value is undefined. This will happen if index is out of bound * 2. If value is 0. In this case, `index` will never change and remaining iterations can be skipped */ if (;arr[index]) break. index += arr[index] } if (index >= arr.length) { console.log('Exit Right') } else if (index < 0) { console.log('Exit Left') } else { console,log('Deadend') } } const data = [ [1, 1, 1], [1, -2], [2, 0, -1], [2, 5, 1, -2, 0]. ] data.forEach((arr) => printDirection(arr))

Here some hacky golang:这里有一些 hacky golang:

package main

import (
  "fmt"
)

func WhereIsTheExit(arr []int) {
  
  if (len(arr) == 0) {
    fmt.Println("No elements")
    return
  } 

  i := arr[0]

  for p := 1; p < len(arr); p++ {

    fmt.Printf("Current value: %d\n", i)

    if (i > len(arr) - 1 || i < 0) {
      break
    }

    i += arr[i]

    fmt.Printf("Next value: %d\n", i)
  }

  if (i >= len(arr)) {
    fmt.Println("====> right")
  } else if (i < 0) {
    fmt.Println("====> left")
  } else {
    fmt.Println("====> deadend")
  }
}

func main() {

  empty := []int{}
  deadend := []int{1,2,0,-3}
  deadend2 := []int{1,0,-1}
  right := []int{2,0,3,0,0}
  right2 := []int{1,2,0,4,0,2,7,1}
  left := []int{1,-2}
  left2 := []int{1,2,0,3,0,0,-10}

  WhereIsTheExit(empty)
  WhereIsTheExit(deadend)
  WhereIsTheExit(deadend2)
  WhereIsTheExit(right)
  WhereIsTheExit(right2)
  WhereIsTheExit(left)
  WhereIsTheExit(left2)
}

Try out: https://play.golang.org/p/KU4mapYf_b3试用: https://play.golang.org/p/KU4mapYf_b3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM