所以我正在解决这个问题:https : //www.hackerrank.com/challenges/30-review-loop/problem(它在 C# 中)
到目前为止,我只是试图将它一块一块地分解,到目前为止,我能够让它显示所有其他字符,但我不确定如何将每个字母连接成一个新字符串。
我的问题代码如下我已经注释掉了两个 for 循环,因为我觉得有一个比我拥有的更优雅的解决方案,但我不想丢失我所在的位置以防万一另一条路径事实证明更具挑战性。
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int inputQTY = Int32.Parse(Console.ReadLine());
string input = Console.ReadLine(); // The example gives us the first word to be Hacker then the next word Rank on the next line, so the outputs would be Hce akr, and Rn ak respectively.
int strLen = input.Length;
char[] inputCharArray = input.ToCharArray();
string output = "";
/*
for (int j = 0; j < inputQTY; j++){
for (int i = 0; i < strLen; i++) {
if (j % 2 == 0 && i % 2 == 0) {
Console.WriteLine(inputCharArray[i]);
output = new string (new char[] {inputCharArray[i]});
Console.WriteLine(output);
Console.WriteLine("This is i: {0}", i);
Console.WriteLine("This is j: {0}", j);
Console.WriteLine("--------------");
Console.WriteLine("");
}
else {
Console.WriteLine("This is the next j part hopefully: {0}", j);
}
}
}*/
}
}
Run Code Online (Sandbox Code Playgroud)
就像我理解的那样,我需要首先遍历单词,抓取每个其他字母,然后再次遍历单词并抓取剩余的字母,然后将这些字母连接成单词,然后将这些单词连接成一个句子,所以 j 将是循环给我两个词,我是循环将两个词放在一起..... 最重要的是,我觉得我完全缺少另一种方法,使用我什至可能不知道的命令。
Anyhoo 任何帮助表示赞赏,希望我不会在这之后变得如此绿色。谢谢!
好的,所以我最终用以下代码解决了它,谢谢大家的帮助!
我最终用以下代码(在 C# 中)解决了它:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int count = Int32.Parse(Console.ReadLine());
for (int k = 0; k < count; k++) {
char[] word = Console.ReadLine().ToCharArray();
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < word.Length; i+=2) {
sb1.Append(word[i]);
}
for (int j = 1; j < word.Length; j+=2) {
sb2.Append(word[j]);
}
Console.WriteLine(sb1 + " " + sb2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
LINQ 版本,更新以修复索引错误:
output = $"{new string(s.Where((x,i) => i % 2 == 0).ToArray())} {new string(s.Where((x,i) => i % 2 != 0).ToArray())}";
Run Code Online (Sandbox Code Playgroud)
解释一下,您要获取字符串中索引不能被 2 整除的每个字符并打印它,然后获取字符串中索引不能被 2 整除的每个字符并打印它。
更新:
因为我被要求进一步解释。首先,这是在 HackerRank 挑战中成功运行的完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
List<string> tests = new List<string>();
var testCount = int.Parse(Console.ReadLine());
for (var i = 0; i < testCount; i++)
{
tests.Add(Console.ReadLine());
}
foreach (var s in tests)
{
Console.WriteLine($"{new string(s.Where((x, i) => i % 2 == 0).ToArray())} {new string(s.Where((x, i) => i % 2 != 0).ToArray())}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
关于代码的每个部分的作用:
i % 2 == 0
Run Code Online (Sandbox Code Playgroud)
这是一个测试,看看一个数是否能被 2 整除,或者是偶数。
s.Where((x,i) => i % 2 == 0)
Run Code Online (Sandbox Code Playgroud)
这表示,对于组成字符串 's' 的字符数组,返回所有字符(结果是一个 IEnumerable),其中该字符的索引(字符串中的位置)是偶数。
new string(s.Where((x,i) => i % 2 == 0).ToArray())
Run Code Online (Sandbox Code Playgroud)
这表示要使用具有偶数索引的字符的 IEnumerable 并将它们返回到字符数组。然后,从该字符数组中创建一个新字符串。
对于奇数,它是相同的,但您在 mod 中使用 != 0。
归档时间: |
|
查看次数: |
3295 次 |
最近记录: |