8000 1,给Vector添加equals和hashCode方法。 · mapengfei00099/javaalgorithm@e3de22f · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit e3de22f

Browse files
author
shengshijun
committed
1,给Vector添加equals和hashCode方法。
2,完成从一个连续数组中查找连续子数组其和为指定值。
1 parent 738d353 commit e3de22f

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/main/java/ssj/algorithm/collections/Vector.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ public String toString() {
145145
return sb.toString();
146146
}
147147

148+
@Override
149+
public boolean equals(Object o) {
150+
if (this == o) return true;
151+
if (o == null || getClass() != o.getClass()) return false;
152+
153+
@SuppressWarnings("unchecked")
154+
Vector vector = (Vector) o;
155+
156+
// Probably incorrect - comparing Object[] arrays with Arrays.equals
157+
if (!Arrays.equals(_values, vector._values)) return false;
158+
159+
return true;
160+
}
161+
162+
@Override
163+
public int hashCode() {
164+
return _values != null ? Arrays.hashCode(_values) : 0;
165+
}
166+
148167
public void swap(int start, int end) {
149168
ArrayUtil.swap(_values, start, end);
150169
}

src/main/java/ssj/algorithm/math/MathUtil.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.base.Preconditions;
44
import ssj.algorithm.ArrayUtil;
5+
import ssj.algorithm.List;
6+
import ssj.algorithm.collections.Vector;
57
import ssj.algorithm.lang.Tuple2;
68

79
import java.math.BigInteger;
@@ -398,4 +400,36 @@ private static boolean checkUnique(int[] arr, int ele) {
398400
}
399401
return count == 1;
400402
}
403+
404+
405+
/**
406+
* 在一个排好序的int数组中查找连续子数组其和为指定的值。
407+
* @param arr
408+
* @param value
409+
* @return
410+
*/
411+
public static List<Tuple2<Integer, Integer>> findSumInOrder(int[] arr, int value) {
412+
Preconditions.checkNotNull(arr);
413+
Preconditions.checkArgument(arr.length > 1);
414+
List<Tuple2<Integer, Integer>> result = new Vector<>();
415+
int start = arr.length - 2;
416+
int end = arr.length - 1;
417+
int sum = arr[start] + arr[end];
418+
while (start >= 0) {
419+
if (sum == value) {
420+
result.add(new Tuple2<>(start, end));
421+
sum -= arr[end];
422+
end--;
423+
} else if (sum < value) {
424+
start--;
425+
if (start >= 0) {
426+
sum += arr[start];
427+
}
428+
} else {
429+
sum -= arr[end];
430+
end--;
431+
}
432+
}
433+
return result;
434+
}
401435
}

src/test/java/ssj/algorithm/math/MathUtilTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,10 @@ public void testUniqueInt() {
8585
assertEquals(2, MathUtil.uniqueInt(new int[]{100, 100, 3, 4, 5, 2, 3, 4, 5}));
8686
assertEquals(new Tuple2<>(2, 100), MathUtil.uniqueTwoInt(new int[]{100, 3, 4, 5, 2, 3, 4, 5}));
8787
}
88+
89+
@Test
90+
public void testFindSumInOrder() {
91+
System.out.println(MathUtil.findSumInOrder(new int[]{1, 2, 3, 4, 5, 6, 7, 8}, 15));
92+
System.out.println(MathUtil.findSumInOrder(new int[]{1,4,7,8,11}, 20));
93+
}
8894
}

0 commit comments

Comments
 (0)
0