Skip to content

Commit 708d8d0

Browse files
committed
5.22 string to integer?
1 parent ff3f47a commit 708d8d0

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

1.38 KB
Binary file not shown.

bin/Sort/SortColors.class

1.18 KB
Binary file not shown.

bin/Sort/WiggleSort.class

483 Bytes
Binary file not shown.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package Class99_Mix;
2+
3+
// Implement atoi to convert a string to an integer.
4+
public class StringtoInteger {
5+
public int myAtoi(String str) {
6+
int index = str.length() - 1;
7+
int sign = 1;
8+
int total = 0;
9+
// 1. Empty string
10+
if (str == null || str.length() == 0) {
11+
return 0;
12+
}
13+
14+
//
15+
int signCnt = 0;
16+
for (int i = 0; i < str.length(); i++) {
17+
if (str.charAt(i) == '+' || str.charAt(i) == '-') {
18+
if (signCnt == 0) {
19+
sign = str.charAt(i) == '+' ? 1 : -1;
20+
signCnt++;
21+
} else {
22+
break;
23+
}
24+
} else if (isDigit(str.charAt(i))) {
25+
// check overflow
26+
if (Integer.MAX_VALUE < total * 10
27+
|| Integer.MAX_VALUE / 10 == total
28+
&& Integer.MAX_VALUE % 10 < str.charAt(i) - '0') {
29+
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
30+
}
31+
total = total * 10 + str.charAt(i) - '0';
32+
} else if (i>0 && (str.charAt(i) == ' ' && str.charAt(i-1) != ' ') || (str.charAt(i) != ' ' && str.charAt(i-1) != ' ') ) {
33+
return total * sign;
34+
}
35+
}
36+
return total * sign;
37+
}
38+
39+
private boolean isDigit(char c) {
40+
if (c < '0' || c > '9') {
41+
return false;
42+
}
43+
return true;
44+
}
45+
46+
public static void main(String[] args) {
47+
StringtoInteger sol = new StringtoInteger();
48+
System.out.println(sol.myAtoi(" +1222, 123 "));
49+
}
50+
51+
}

src/Sort/SortColors.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package Sort;
2+
3+
/*
4+
* Given an array with n objects colored red, white or blue, sort them so that
5+
* objects of the same color are adjacent, with the colors in the order red,
6+
* white and blue.
7+
*
8+
* Here, we will use the integers 0, 1, and 2 to represent the color red, white,
9+
* and blue respectively.
10+
*
11+
* Note:
12+
* You are not suppose to use the library's sort function for this problem.
13+
*/
14+
public class SortColors {
15+
public void sortColors(int[] nums) {
16+
if (nums == null || nums.length < 2) {
17+
return;
18+
}
19+
// three pointers:
20+
// red: 0 - red-th are red color
21+
// white: red - white-th are white color
22+
// ** white - blue-th are unknown area
23+
// blue: blue - end are blue color
24+
int red = 0;
25+
int white = 0;
26+
int blue = nums.length - 1;
27+
for (int i = 0; i <= blue; i++) {
28+
if (nums[i] == 0) {
29+
swap(red, i, nums);
30+
red++;
31+
white++;
32+
} else if (nums[i] == 1) {
33+
// swap(white, i, nums);
34+
white++;
35+
} else {
36+
swap(blue, i, nums);
37+
blue--;
38+
i--;
39+
}
40+
}
41+
}
42+
43+
private void swap(int i, int j, int[] array) {
44+
int temp = array[i];
45+
array[i] = array[j];
46+
array[j] = temp;
47+
48+
}
49+
50+
public static void main(String[] args) {
51+
// TODO Auto-generated method stub
52+
SortColors sol = new SortColors();
53+
int[] nums = new int[] {0,1,1,2,1,2,0,0 };
54+
sol.sortColors(nums);
55+
System.out.println(nums.toString());
56+
}
57+
58+
}

src/Sort/WiggleSort.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package Sort;
2+
3+
/*
4+
* Given an unsorted array nums, reorder it such that
5+
* nums[0] < nums[1] > nums[2] < nums[3]....
6+
*
7+
* Example:
8+
* (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
9+
* (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
10+
*
11+
* Note:
12+
* You may assume all input has valid answer.
13+
* Follow Up:
14+
* Can you do it in O(n) time and/or in-place with O(1) extra space?
15+
*/
16+
public class WiggleSort {
17+
public void wiggleSort(int[] nums) {
18+
// divide(nums);
19+
}
20+
21+
22+
23+
24+
public static void main(String[] args) {
25+
// TODO Auto-generated method stub
26+
27+
}
28+
29+
}

0 commit comments

Comments
 (0)