可以说我有一个大小不一的字符串,是“ OOOO”。我需要以下代码还返回“ O0O0、0O0O,0OO0,O00O,O000,OO00,OOO0”。基本上,O的所有可能变化都替换为0。做到这一点的最佳方法是什么?当前仅返回“ 0OOO,00OO,000O,0000”
public class test {
public static void main(String[] args) throws Exception {
//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "Galaxy S9+");
caps.setCapability("udid", "352402097079489"); //Give Device ID of your mobile phone
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "9.0");
caps.setCapability("appPackage", "com.example.android.myAPP,com.android.settings");
caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");
caps.setCapability("noReset", "true");
try {
// System.setProperty("webdriver.http.factory", "apache");
AppiumDriver < MobileElement > driver = new AndroidDriver < MobileElement > (new URL("http://localhost:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
}
}
答案 0 :(得分:0)
一种方法是通过递归:
Public Iterator Function ParseOs(ByVal PlayerName As String) As IEnumerable(Of String)
Dim i as Integer = PlayerName.IndexOf("O")
If i = -1 Then
Yield Return PlayerName
Yield Break
End If
'Keep everything (if anything) *before* the O
Dim base As String = PlayerName.SubString(0, i)
Dim permutations As IEnumerable(Of String) = New String() {""}
If PlayerName.Length > i Then
'Recurse to find all the variations for everything *after* the O
permutations = ParseOs(PlayerName.SubString(i+1))
End If
For Each permutation As String in permutations
Yield Return base & "O" & permutation
Yield Return base & "0" & permutation
Next
End Function
如果您确定,输入将始终是“ O”,则可以进一步简化:
Public Iterator Function ParseOs(ByVal PlayerName As String) As IEnumerable(Of String)
Dim permutations As IEnumerable(Of String) = New String() {""}
If PlayerName.Length > 1 Then
permutations = ParseOs(PlayerName.SubString(1))
End If
For Each permutation As String in permutations
Yield Return "O" & permutation
Yield Return "0" & permutation
Next
End Function
您可以通过将它们分成两个函数来提高效率,在这些函数中,递归部分处理字符数组而不是字符串。然后,通过将数组转换为使用Span<Char>
,可以进一步提高 的效率,因此不必为每个递归调用分配新的数组。