swift操场错误错误错误指示
因此,我有以下贪婪算法,它给出了以下错误: 执行中止:错误:执行被中断, 原因:EXC_错误指令(代码=EXC_I386_INVOP,子代码=0x0)。这个 进程被留在中断的位置,请使用 “thread return-x”返回到表达式之前的状态 评估 类别:swift操场错误错误错误指示,swift,xcode,swift-playground,Swift,Xcode,Swift Playground,因此,我有以下贪婪算法,它给出了以下错误: 执行中止:错误:执行被中断, 原因:EXC_错误指令(代码=EXC_I386_INVOP,子代码=0x0)。这个 进程被留在中断的位置,请使用 “thread return-x”返回到表达式之前的状态 评估 类别: // This class represents an undirected graph using adjacency list public class Graph{ var V: Int // number of vertic
// This class represents an undirected graph using adjacency list
public class Graph{
var V: Int // number of vertices
var adj: [[Int]] = [[]] //Adjacency List
public init(v: Int) {
V = v
adj = [[Int]](repeating: [], count: v)
}
// Function to add an edge into the graph
public func addEdge(v: Int, w: Int){
adj[v].append(w)
adj[w].append(v) // Graph is undirected
}
// Assigns colors (starting from 0) to all vertices and
// prints the assignment of colors
public func greedyColoring() {
var result = [Int]()
//Assign the first color to first vertex
result[0] = 0
//Initialize the remaining V-1 vertices as unassigned
for i in 0 ..< V{
//No Color is assigned
result[i] = -1
}
// A temporary array to store the available colors. True
// value of available[cr] would mean that the color cr is
// assigned to one of its adjacent vertices
var available = [Bool]()
for cr in 0 ..< V{
available[cr] = false
}
// Assign colors to remaining V-1 vertices
for i in 1 ..< V{
//Process all adjacent vertices and flag their colors as unavailable
for un in 0 ..< adj[i].count{
if result[un] != -1 {
available[result[un]] = true
}
}
//find the first available color
for cr in 0 ..< V{
if available[cr] == false{
result[i] = cr
break
}
}
//Reset the values back to false for the next iteraation
for un in 0 ..< adj[i].count{
if result[un] != -1 {
available[result[un]] = true
}
}
}
//Print result
for r in 0 ..< V{
print("Vertex \(r) --> Color \(result[r])")
}
}
}
import Foundation
import UIKit
import XCPlayground
var g1 = Graph(v: 5)
g1.addEdge(v: 0, w: 1)
g1.addEdge(v: 0, w: 2)
g1.addEdge(v: 1, w: 2)
g1.addEdge(v: 1, w: 3)
g1.addEdge(v: 2, w: 3)
g1.addEdge(v: 3, w: 4)
g1.greedyColoring() // Fails HERE
所以我在前面的一些行中出现了这个错误,它与我如何使用数组有关。为什么游乐场不给出一个确切的错误,比如索引超出界限之类的?
我的调试控制台不打印任何内容。。。我的代码中出现错误的原因是什么
在此代码片段中:
var result = [Int]()
//Assign the first color to first vertex
result[0] = 0
数组result
为空,因此无法通过result[0]
访问第一个元素
解决方案:
// This class represents an undirected graph using adjacency list
public class Graph{
var V: Int // number of vertices
var adj: [[Int]] = [[]] //Adjacency List
public init(v: Int) {
V = v
adj = [[Int]](repeating: [], count: v)
}
// Function to add an edge into the graph
public func addEdge(v: Int, w: Int){
adj[v].append(w)
adj[w].append(v) // Graph is undirected
}
// Assigns colors (starting from 0) to all vertices and
// prints the assignment of colors
public func greedyColoring() {
var result = [Int]()
//Assign the first color to first vertex
result[0] = 0
//Initialize the remaining V-1 vertices as unassigned
for i in 0 ..< V{
//No Color is assigned
result[i] = -1
}
// A temporary array to store the available colors. True
// value of available[cr] would mean that the color cr is
// assigned to one of its adjacent vertices
var available = [Bool]()
for cr in 0 ..< V{
available[cr] = false
}
// Assign colors to remaining V-1 vertices
for i in 1 ..< V{
//Process all adjacent vertices and flag their colors as unavailable
for un in 0 ..< adj[i].count{
if result[un] != -1 {
available[result[un]] = true
}
}
//find the first available color
for cr in 0 ..< V{
if available[cr] == false{
result[i] = cr
break
}
}
//Reset the values back to false for the next iteraation
for un in 0 ..< adj[i].count{
if result[un] != -1 {
available[result[un]] = true
}
}
}
//Print result
for r in 0 ..< V{
print("Vertex \(r) --> Color \(result[r])")
}
}
}
import Foundation
import UIKit
import XCPlayground
var g1 = Graph(v: 5)
g1.addEdge(v: 0, w: 1)
g1.addEdge(v: 0, w: 2)
g1.addEdge(v: 1, w: 2)
g1.addEdge(v: 1, w: 3)
g1.addEdge(v: 2, w: 3)
g1.addEdge(v: 3, w: 4)
g1.greedyColoring() // Fails HERE
更改为:
var result = [Int]()
//Assign the first color to first vertex
result[0] = 0
//Initialize the remaining V-1 vertices as unassigned
for i in 0 ..< V{
//No Color is assigned
result[i] = -1
}
// A temporary array to store the available colors. True
// value of available[cr] would mean that the color cr is
// assigned to one of its adjacent vertices
var available = [Bool]()
for cr in 0 ..< V{
available[cr] = false
}
var结果=[Int]()
//将第一种颜色指定给第一个顶点
结果[0]=0
//将其余V-1顶点初始化为未指定顶点
对于0..
致:
var结果=[Int]()
//将第一种颜色指定给第一个顶点
结果。追加(0)
//将其余V-1顶点初始化为未指定顶点
对于0..
只要写下:
import PlaygroundSupport
这对我很有帮助完美,有没有办法更好地调试操场?我的日志会打印出
致命错误:索引超出范围
,你呢?我的xcode只显示我发布的错误,而不显示在日志中。你打开调试控制台了吗?试着按一下⇧ + ⌘ + 如果我将图形代码从单独的类切换到主类,调试控制台会显示错误(索引超出范围)在Xcode-12.5上不起作用