Refactored Identifiers (#5306)

Co-authored-by: Bama Charan Chhandogi <b.c.chhandogi@gmail.com>
This commit is contained in:
congyuluo 2024-08-08 07:09:00 -07:00 committed by GitHub
parent 6e23e198ab
commit cafea1ee52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 11 deletions

View File

@ -20,9 +20,9 @@ public final class TwoSumProblem {
public static Optional<Pair<Integer, Integer>> twoSum(final int[] values, final int target) {
HashMap<Integer, Integer> valueToIndex = new HashMap<>();
for (int i = 0; i < values.length; i++) {
final var rem = target - values[i];
if (valueToIndex.containsKey(rem)) {
return Optional.of(Pair.of(valueToIndex.get(rem), i));
final var remainder = target - values[i];
if (valueToIndex.containsKey(remainder)) {
return Optional.of(Pair.of(valueToIndex.get(remainder), i));
}
if (!valueToIndex.containsKey(values[i])) {
valueToIndex.put(values[i], i);

View File

@ -62,10 +62,10 @@ public class FibonacciSearch implements SearchAlgorithm {
Integer[] integers = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
int size = integers.length;
Integer shouldBeFound = 128;
Integer targetValue = 128;
FibonacciSearch fsearch = new FibonacciSearch();
int atIndex = fsearch.find(integers, shouldBeFound);
int atIndex = fsearch.find(integers, targetValue);
System.out.println("Should be found: " + shouldBeFound + ". Found " + integers[atIndex] + " at index " + atIndex + ". An array length " + size);
System.out.println("Should be found: " + targetValue + ". Found " + integers[atIndex] + " at index " + atIndex + ". An array length " + size);
}
}

View File

@ -75,13 +75,13 @@ final class WordLadder {
continue;
}
wordsChars[j] = c;
String newWord = String.valueOf(wordsChars);
if (newWord.equals(endWord)) {
String transformedWord = String.valueOf(wordsChars);
if (transformedWord.equals(endWord)) {
return level + 1;
}
if (set.contains(newWord)) {
set.remove(newWord);
queue.offer(newWord);
if (set.contains(transformedWord)) {
set.remove(transformedWord);
queue.offer(transformedWord);
}
}
wordsChars[j] = originalChars;