我试图解决一个面试问题,但为此我必须逐级旅行二叉树.我设计的BinaryNode具有以下变量
private object data;
private BinaryNode left;
private BinaryNode right;
Run Code Online (Sandbox Code Playgroud)
有人可以帮我在BinarySearchTree类中编写BreadthFirstSearch方法吗?
更新:感谢大家的投入.所以这是面试问题."给定一个二叉搜索树,设计一个算法,创建每个深度的所有节点的链表(即,如果你有一个深度为D的树,你将有D个链表)".
这是我的方法,让我知道你的专家评论.
public List<LinkedList<BNode>> FindLevelLinkList(BNode root)
{
Queue<BNode> q = new Queue<BNode>();
// List of all nodes starting from root.
List<BNode> list = new List<BNode>();
q.Enqueue(root);
while (q.Count > 0)
{
BNode current = q.Dequeue();
if (current == null)
continue;
q.Enqueue(current.Left);
q.Enqueue(current.Right);
list.Add(current);
}
// Add tree nodes of same depth into individual LinkedList. Then add all LinkedList into a List
LinkedList<BNode> LL = new LinkedList<BNode>();
List<LinkedList<BNode>> result = …
Run Code Online (Sandbox Code Playgroud) 使用MSTest如何验证来自测试方法的确切错误消息?我知道[ExpectedException(typeof(ApplicationException), error msg)]
不会比较来自我的测试方法的错误消息,尽管在其他单元测试框架中它正在做.
解决这个问题的一种方法是使用一些try catch块编写单元测试,但是我需要再写4行.
有没有最聪明的方法来检查错误消息.
干杯,Pritam
我做了这个功课.并以下列方式解决了它.我需要你的意见是否是一个好的方法,或者我需要使用任何其他数据结构以更好的方式解决它.
public string ReturnCommon(string firstString, string scndString)
{
StringBuilder newStb = new StringBuilder();
if (firstString != null && scndString != null)
{
foreach (char ichar in firstString)
{
if (!newStb.ToString().Contains(ichar) && scndString.Contains(ichar))
newStb.Append(ichar);
}
}
return newStb.ToString();
}
Run Code Online (Sandbox Code Playgroud) 问题:给定大小为n的整数输入数组和大小为k的整数查询数组,找到包含查询数组的所有元素的输入数组的最小窗口,并且顺序也相同.
我试过以下方法.
int[] inputArray = new int[] { 2, 5, 2, 8, 0, 1, 4, 7 };
int[] queryArray = new int[] { 2, 1, 7 };
Run Code Online (Sandbox Code Playgroud)
将在inputArray中找到所有查询数组元素的位置.
public static void SmallestWindow(int[] inputArray, int[] queryArray)
{
Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
int index = 0;
foreach (int i in queryArray)
{
HashSet<int> hash = new HashSet<int>();
foreach (int j in inputArray)
{
index++;
if (i == j)
hash.Add(index);
}
dict.Add(i, hash);
index = 0;
}
// Need to …
Run Code Online (Sandbox Code Playgroud) string s1 = "t";
string s2 = 't'.ToString();
Console.WriteLine(s1.Equals(s2)); // returning true
Console.WriteLine(object.Equals(s1, s2)); // returning true
Run Code Online (Sandbox Code Playgroud)
这里返回相同的结果.现在,当我使用StringBuilder时,它没有返回相同的值.底层的原因是什么?
StringBuilder s1 = new StringBuilder();
StringBuilder s2 = new StringBuilder();
Console.WriteLine(s1.Equals(s2)); // returning true
Console.WriteLine(object.Equals(s1, s2)); // returning false
Run Code Online (Sandbox Code Playgroud)
编辑1:我的上述问题在下面回答.但在本次讨论中,我们发现StringBuilder在其实现中没有任何覆盖Equals方法.所以当我们调用StringBuilder.Equals时,实际上它会转到Object.Equals.因此,如果有人调用StringBuilder.Equals和S1.Equals(S2),结果将会有所不同.
这是我的场景:我有一个MVC Web应用程序和Web API.Web应用程序调用web api以保存/检索服务器中的数据.
让我们说这是一个问答网站.现在我有一个API,如果我提供用户名,密码,它会给我userid.但是网站中还有其他区域,并且很容易检索其他用户的用户ID.我将userid保留在会话存储中,并在POST对象中将其发送到需要的地方.现在,任何用户都可以在会话存储中调整该用户ID,并且他们可以代表其他用户发布问题/答案.
我怎么能阻止这个?我正在思考但不确定这是否是可行解决方案的一种方法 - 我们可以从服务器端提供的承载令牌中检索用户ID吗?
可能重复:
接口:为什么我似乎无法掌握它们?
我为什么要使用接口?
我认为这个问题重复了1000次,我很抱歉再次提问.我真的在寻找一个简单的简单答案,为什么我明确需要接口,或者如果你请解释一些我没有接口就无法实现的东西.
如果它是多重继承,那么我会请你给我一个简单的例子,通过它我可以理解为什么我需要接口.
- 提前.
注意:我在.NET(C#)语言的上下文中提出这个问题
编辑1:现在,每当我尝试学习界面时,我的思绪告诉我 - >"伙计你要用白纸勾勒出某人的身体"但是你还需要另一张白纸,你需要再次绘制轮廓,绘制所有身体部位填充颜色给他们以获得真实的图片".那么为什么我只是为了获得轮廓而浪费第一张白纸.
我正在尝试从我们的内部网站运行应用程序.当我使用时,Process.Start("notepad");
我可以看到记事本进程在我们的Web服务器中启动,并在应用程序池设置中提到了默认标识.
但我必须使用特定的用户名和密码启动该过程.所以我试着运行这行代码
string password = "XXXXX";
System.Security.SecureString securePwd = new System.Security.SecureString();
foreach (char c in password)
{
// Append the character to the password.
securePwd.AppendChar(c);
}
Process.Start("notepad", "username", securePwd, "domain");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我甚至没有看到在Web服务器中启动任何记事本进程.执行的代码行,因为当我输入错误的密码时,我可以看到我的网页抛出"错误的用户名或密码"错误.
我知道这是最愚蠢的问题.但实际上我很难将价格(字符串)转换为小数.这是我尝试过的
string s = "123.45";
decimal d = decimal.Parse(s, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint);
Run Code Online (Sandbox Code Playgroud)
但我得到格式异常
也试过了
decimal num;
bool pass = decimal.TryParse("2.85", out num);
Run Code Online (Sandbox Code Playgroud)
num表示为0.
任何帮助将不胜感激.
c# ×8
.net ×4
algorithm ×4
asp.net ×1
asp.net-mvc ×1
automation ×1
collections ×1
decimal ×1
iis ×1
inheritance ×1
interface ×1
mstest ×1
process ×1
string ×1
token ×1
unit-testing ×1