Skip to content

Commit 0e2f0cf

Browse files
Merge pull request #27 from wangjunting/master
019
2 parents f339695 + ffa09a4 commit 0e2f0cf

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Week_01/id_19/LeetCode_189_19.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Solution {
2+
public void rotate(int[] nums, int k) {
3+
if(k <= 0 || nums == null || nums.length == 0) return;
4+
k = k % nums.length;
5+
if(k == 0) return;
6+
int[] temp = new int[k];
7+
for(int i = nums.length-k,j=0;i < nums.length;i++,j++)
8+
temp[j] = nums[i];
9+
for(int i = nums.length-k-1,j = nums.length-1;i >= 0;i--,j--)
10+
nums[j] = nums[i];
11+
for(int i = 0,j = 0; i < k; i++,j++)
12+
nums[i]=temp[j];
13+
}
14+
}
15+
16+
public class MainClass {
17+
public static int[] stringToIntegerArray(String input) {
18+
input = input.trim();
19+
input = input.substring(1, input.length() - 1);
20+
if (input.length() == 0) {
21+
return new int[0];
22+
}
23+
24+
String[] parts = input.split(",");
25+
int[] output = new int[parts.length];
26+
for(int index = 0; index < parts.length; index++) {
27+
String part = parts[index].trim();
28+
output[index] = Integer.parseInt(part);
29+
}
30+
return output;
31+
}
32+
33+
public static String integerArrayToString(int[] nums, int length) {
34+
if (length == 0) {
35+
return "[]";
36+
}
37+
38+
String result = "";
39+
for(int index = 0; index < length; index++) {
40+
int number = nums[index];
41+
result += Integer.toString(number) + ", ";
42+
}
43+
return "[" + result.substring(0, result.length() - 2) + "]";
44+
}
45+
46+
public static String integerArrayToString(int[] nums) {
47+
return integerArrayToString(nums, nums.length);
48+
}
49+
50+
public static void main(String[] args) throws IOException {
51+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
52+
String line;
53+
while ((line = in.readLine()) != null) {
54+
int[] nums = stringToIntegerArray(line);
55+
line = in.readLine();
56+
int k = Integer.parseInt(line);
57+
58+
new Solution().rotate(nums, k);
59+
String out = integerArrayToString(nums);
60+
61+
System.out.print(out);
62+
}
63+
}
64+
}

Week_01/id_19/LeetCode_26_19.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
if(nums==null || nums.length==0) return 0;
4+
int left=0,right=0;
5+
while((++right)<nums.length){
6+
if(nums[right]!=nums[left])
7+
nums[++left]=nums[right];
8+
}
9+
return left+1;
10+
}
11+
}
12+
13+
public class MainClass {
14+
public static int[] stringToIntegerArray(String input) {
15+
input = input.trim();
16+
input = input.substring(1, input.length() - 1);
17+
if (input.length() == 0) {
18+
return new int[0];
19+
}
20+
21+
String[] parts = input.split(",");
22+
int[] output = new int[parts.length];
23+
for(int index = 0; index < parts.length; index++) {
24+
String part = parts[index].trim();
25+
output[index] = Integer.parseInt(part);
26+
}
27+
return output;
28+
}
29+
30+
public static String integerArrayToString(int[] nums, int length) {
31+
if (length == 0) {
32+
return "[]";
33+
}
34+
35+
String result = "";
36+
for(int index = 0; index < length; index++) {
37+
int number = nums[index];
38+
result += Integer.toString(number) + ", ";
39+
}
40+
return "[" + result.substring(0, result.length() - 2) + "]";
41+
}
42+
43+
public static String integerArrayToString(int[] nums) {
44+
return integerArrayToString(nums, nums.length);
45+
}
46+
47+
public static void main(String[] args) throws IOException {
48+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
49+
String line;
50+
while ((line = in.readLine()) != null) {
51+
int[] nums = stringToIntegerArray(line);
52+
53+
int ret = new Solution().removeDuplicates(nums);
54+
String out = integerArrayToString(nums, ret);
55+
56+
System.out.print(out);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)