Skip to content

Commit 1244ba9

Browse files
committed
update string ops
1 parent eecb3d3 commit 1244ba9

3 files changed

Lines changed: 11 additions & 5 deletions

File tree

PuzzleCoding/src/SearchInRotatedSortedArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static int search(int a[], int left, int right, int x) {
5151
if (x == a[mid]) { // Found element
5252
return mid;
5353
}
54-
if (right < left) {
54+
if (right < left || right < 0 || left >= a.length || a.length == 0) {
5555
return -1;
5656
}
5757

PuzzleCoding/src/StablePartition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
public class StablePartition {
1212
public static void main(String[] args){
13-
int[] a = new int[]{1, -5, 2, 3, -4, -2, 9, 3, -1};
13+
// int[] a = new int[]{1, -5, 2, 3, -4, -2, 9, 3, -1};
14+
int[] a = new int[]{1, -5, 7, 3, -1, -2, 9, 3, -3};
1415
//int[] a = new int[]{1, -3, 2,-1,3};
1516

1617
stable2WayPartition(a);

StringOps/src/StringOps.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919
String lcs1 = LCS1(s1, s2);
2020
System.out.println("LCS1: " + lcs1);
2121

22-
String s3 = "abcdefg";
22+
String s3 = "abcdefgh";
2323
System.out.println("s3: " + s3);
2424
int k = 3;
2525
String rs3 = rotateString(s3, k);
@@ -190,9 +190,14 @@ public static String reverse(String st, int s, int e) {
190190

191191
public static String rotateString(String s, int k) {
192192
int n = s.length();
193+
/*
193194
String rs1 = reverse(s, 0, n - 1);
194-
String rs2 = reverse(rs1, 0, k);
195-
String rs3 = reverse(rs2, k + 1, n - 1);
195+
String rs2 = reverse(rs1, 0, n-k-1);
196+
String rs3 = reverse(rs2, n-k, n - 1);
197+
*/
198+
String rs1 = reverse(s, 0, k-1);
199+
String rs2 = reverse(rs1, k, n-1);
200+
String rs3 = reverse(rs2, 0, n - 1);
196201

197202
return rs3;
198203

0 commit comments

Comments
 (0)