您好,登錄后才能下訂單哦!
在Java中高效處理大量回文串數據,可以采用以下方法:
import java.util.HashSet;
import java.util.Set;
public class PalindromeChecker {
public static void main(String[] args) {
String[] palindromes = {"level", "deed", "hello", "world", "madam", "racecar"};
Set<String> palindromeSet = new HashSet<>();
for (String s : palindromes) {
palindromeSet.add(s);
}
String input = "level";
if (palindromeSet.contains(input)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class PalindromeCheckerMultiThreaded {
public static void main(String[] args) throws InterruptedException, ExecutionException {
String[] palindromes = {"level", "deed", "hello", "world", "madam", "racecar"};
ExecutorService executorService = Executors.newFixedThreadPool(palindromes.length);
List<Future<Boolean>> futures = new ArrayList<>();
for (String s : palindromes) {
Future<Boolean> future = executorService.submit(() -> isPalindrome(s));
futures.add(future);
}
for (int i = 0; i < futures.size(); i++) {
System.out.println(palindromes[i] + " is a palindrome: " + futures.get(i).get());
}
executorService.shutdown();
}
public static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) {
return false;
}
}
return true;
}
}
public class PalindromeCheckerBitwise {
public static void main(String[] args) {
String input = "level";
System.out.println(input + " is a palindrome: " + isPalindrome(input));
}
public static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (((s.charAt(left++) ^ s.charAt(right--)) & 0xAAAAAAAA) != 0) {
return false;
}
}
return true;
}
}
結合這些方法,可以在Java中高效地處理大量回文串數據。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。