- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试检查两个单词是否是变位词,并尝试使用 LinkedList 来做到这一点。为此,首先,我创建了一个名为 LinkedList 的类:
class LinkedList
{
private Node head;
private int count;
public LinkedList()
{
this.head = null;
this.count = 0;
}
public bool Empty
{
get { return this.count == 0; }
}
public int Count
{
get { return this.count; }
}
public object this[int index]
{
get { return this.Get(index); }
}
public object Add(int index,object o)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("Index - " + index); //if index is less than 0 throw an error message
}
if (index > count) // if size exceeds the limit of the list the item will be added to the last line of the list.
{
index = count;
}
Node current = this.head;
if(this.Empty || index== 0)
{
this.head = new Node(o, this.head);
}
else
{
for(int i = 0; i < index - 1; i++)
{
current = current.Next;
}
current.Next = new Node(o, current.Next);
}
count++;
return o;
}
public object Add(Object o)
{
return this.Add(count, o);
}
public object Remove(int index)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("Index - " + index);
}
if (this.Empty)
{
return null;
}
if (index >= this.count)
{
index = count-1;
}
Node current = this.head;
object result = null;
if (index == 0)
{
result = current.Data; //gets the first node
this.head = current.Next; //makes 2nd node to the first node
}
else
{
for(int i = 0; i < index - 1; i++)
{
result = current.Next.Data;
}
result = current.Next;
current.Next = current.Next.Next;
}
count--;
return result;
}
public int IndexOf(object o)
{
Node current = this.head;
for(int i = 0; i < this.count; i++)
{
if (current.Data.Equals(o))
{
return i;
}
current = current.Next;
}
return -1;
}
public bool Contains(object o)
{
return this.IndexOf(o) >= 0; //if list contains object it returns bigger value than -1 and also 0.
}
public object Get(int index)
{
if(index < 0)
{
throw new ArgumentOutOfRangeException("Index - " + index);
}
if (this.Empty)
{
return null;
}
if(index >= this.count)
{
index = this.count-1;
}
Node current = this.head;
for(int i=0;i< index; i++)
{
current = current.Next;
}
return current.Data;
}
}
还有另一个名为“Node”的类:
class Node
{
private object data;
private Node next;
public Node(object data,Node next) //constructor
{
this.data = data;
this.next = next;
}
public object Data
{
get { return this.data; }
set { this.data = value; }
}
public Node Next
{
get { return this.next; }
set { this.next = value; }
}
}
在主程序中,我从链表类中创建了两个对象,并从用户那里读取了两个字符串,并将单词的字符添加到链表中。然后比较这些字符,如果发现它们将从链表中删除,则增加counter 等等。如果 counter 等于 list1 的元素数,那么它们就是变位词,如果不是,则单词不是变位词。这是我的主要程序代码:
class Program
{
static void Main(string[] args)
{
int counter = 0;
String word1, word2;
Console.WriteLine("Welcome to Anagram Checker!\nPlease enter your first word:");
word1 = Console.ReadLine();
Console.WriteLine("\nPlease enter the second word:");
word2 = Console.ReadLine();
int result = AnagramChecker(word1, word2, counter);
if (result == 1)
{
Console.WriteLine("These words are anagram");
}
if (result == 0)
{
Console.WriteLine("The words are not anagrams");
}
Console.ReadLine();
}
public static int AnagramChecker(String word1, String word2, int counter)
{
char[] ArrayWord1 = word1.ToCharArray();
char[] ArrayWord2 = word2.ToCharArray();
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
for (int i = 0; i < ArrayWord1.Length; i++) //Adds char of word1 to the list
{
list1.Add(i,ArrayWord1[i]);
}
for (int j = 0; j < ArrayWord2.Length; j++) //Adds char of word2 to the list
{
list2.Add(j,ArrayWord2[j]);
}
int max;
if (list1.Count >= list2.Count)
{
max = list1.Count;
}
if (list2.Count > list1.Count)
{
max = list2.Count;
}
for (int i = 0; i < list1.Count; i++)
{
if (list2.Contains(list1[i]) && list1.Contains(list2[i]))
{
list1.Remove(i);
list2.Remove(list2.IndexOf(list1[i]));
counter++;
}
}
Console.WriteLine(counter);
if (counter == word1.Length || counter == word2.Length)
{
return 1;
}
else
return 0;
}
}
当我输入不同的单词时,我会得到不同的结果。输出示例如下。我做错了什么?
输出:
1-)
2-)
感谢您的帮助。
最佳答案
如果您只是想知道单词是否是变位词,您可以使用这种方法:
private static bool areAnagrams(string word1, string word2)
{
List<char> w1 = word1.OrderBy(c => c).ToList();
List<char> w2 = word2.OrderBy(c => c).ToList();
return !w1.Where((t, i) => t != w2[i]).Any();
}
这会创建两个以单词 chars 排序的列表,然后比较两者。
更具可读性的等价物:
private static bool areAnagrams(string word1, string word2)
{
List<char> w1 = word1.OrderBy(c => c).ToList();
List<char> w2 = word2.OrderBy(c => c).ToList();
if (w1.Count != w2.Count)
return false;
for (int i = 0; i < w1.Count; i++)
{
if (w1[i] != w2[i])
return false;
}
return true;
}
关于C# Anagram Checker 与 LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37137496/
我想添加 LinkedList (我们称之为列表 A)到 LinkedList> (称之为列表 B)。执行此操作后,我需要更改列表 A 的值并将其再次添加到列表 B,但不更改已存储在列表 B 中的值。
更新:感谢所有的回答。我发现的最干净的解决方案是这个: if ( k(Arrays.asList(new LinkedList<>())); 我有一个递归方法,可以从列表中生成所有“n 选 k”组合。
在我的 Java 应用程序中,以下两个都将编译和运行,并产生所需的结果。 //"Rotate" the list items one place to the left. myLinkedList.a
我写了一个LinkedList接受 Nodes 的类存储 Integers . 然后我创建了一个 LinkedList stack = new LinkedList() ,并添加了 Node s 如果
这个问题在这里已经有了答案: What does it mean to "program to an interface"? (33 个答案) 关闭 9 年前。 新手 Java 问题: 谁能解释一下
我有一个问题。我无法并排输出我的 LinkedList。我问了这个问题,但遗憾的是我的老师告诉我不要更改方法头或使用 java 库(如日历)。我得到了很多关于使用它或更改方法头的建议。我是根据年级而定
这里有什么问题?。我正在尝试使用邻接列表,通过利用 util 包中的集合来实现图形数据结构。这里 LinkedList array which holds some integer. Each ele
这个问题已经有答案了: Reversing a linked list in Java, recursively (33 个回答) 已关闭10 年前。 如何使用 linkedList 类中的方法以相反
我需要实现一个 LinkedList,到目前为止,我已经编写了在列表中按顺序插入值的方法。我有我的节点 front 作为我的类的实例数据,当创建我的第一个值并尝试将 front 的 next 值设置为
目前,我的 LinkedList(不是 Java 的)类中有一个方法,可以将单个节点添加到 LinkedList 中,如下所示: public void add(int index, T v) {
我正在编写一个读取 XML 文件的类,该 XML 使用“sax”类进行解析。在我的 XML 文件中,我创建了“for”标签和“宏”,使 for 循环能够写入 XML,例如: Th
我正在处理一个 C++ 作业,我将在一个链表的链表上创建一个搜索引擎。根据要求,我不能使用其他库和 STL。 基本上它会是这样的(我从小列表中删除了变量,因为它们是不相关的): 我的结构是这些: st
老实说,我现在真的很困惑这个问题,并且真的不知道如何解决这个问题。我需要编写一个方法,其中给定一个字符链接列表(例如:{'a','A','d','X'})并返回仅包含大写字符的列表(返回:{'A','
我正在尝试获取可执行文件中的两个链表,并在交替位置将它们合并到一起。前任。 ListOne 1,2,3 和 ListTwo 4,5 新的 ListOne 应该是 1,4,2,5,3。 链表.h文件:
这个问题在这里已经有了答案: Is List a subclass of List? Why are Java generics not implicitly polymorphic? (19 个回答
在尝试了解如何将哈希表插入LinkedLists时,我遇到了麻烦。我失去了尝试过的不同事物的数量。我知道我可以使用ArrayList或其他东西,但是我想使它与LinkedLists一起工作,以便可以对
我一直在尝试编写一种方法,不仅可以从 LinkedList(allUsers) 中删除对象(User),还可以从所有用户拥有的单个 LinkedList 中删除。谁能向我解释为什么这是错误的?我已经包
我有一个列表结构和一个名为树的递归函数。在下面的代码中,它永远不会到达 current == null 语句,因此它将永远运行。 如果我无法使用null,解决方案是什么? private void t
这个问题在这里已经有了答案: How does one add a LinkedList to a LinkedList in C#? (3 个答案) 关闭 9 年前。 假设我有以下内容: Link
我正在尝试为 LinkedList 创建一个反向 ListIterator,并且打算将其实现为 linkedList.listIterator(linkedList. size()) 交换了 next
我是一名优秀的程序员,十分优秀!