Skip to content

Commit 619fa27

Browse files
committed
feat:add four easy task
1 parent e185615 commit 619fa27

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

src/com/ernest/easy/_001.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ernest.easy;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
public class _001 {
7+
8+
9+
public int[] twoSum_0(int[] source, int target) {
10+
for (int i = 0; i < source.length; i++) {
11+
for (int j = i + 1; j < source.length; j++) {
12+
if (source[i] + source[j] == target) {
13+
return new int[]{i, j};
14+
}
15+
}
16+
}
17+
return null;
18+
}
19+
20+
public int[] twoSum_1(int[] source, int target) {
21+
int length = source.length;
22+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
23+
for (int i = 0; i < length; i++) {
24+
if (map.containsKey(source[i])) {
25+
return new int[]{map.get(source[i]), i};
26+
}
27+
map.put(target - source[i], i);
28+
}
29+
return null;
30+
}
31+
32+
public static void main(String[] args) {
33+
int[] source = new int[]{2, 7, 11, 15};
34+
int target = 9;
35+
_001 solution = new _001();
36+
System.out.println(Arrays.toString(solution.twoSum_0(source, target)));
37+
System.out.println(Arrays.toString(solution.twoSum_1(source, target)));
38+
}
39+
}

src/com/ernest/easy/_007.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ernest.easy;
2+
3+
public class _007 {
4+
5+
6+
public int reverse(int source) {
7+
int result = 0;
8+
for (; source != 0; source /= 10) {
9+
result = result * 10 + source % 10;
10+
}
11+
return result > Integer.MAX_VALUE | result < Integer.MIN_VALUE ? 0 : result;
12+
}
13+
14+
public static void main(String[] args) {
15+
int source1 = 123;
16+
int source2 = -123;
17+
_007 solution = new _007();
18+
System.out.println(solution.reverse(source1));
19+
System.out.println(solution.reverse(source2));
20+
}
21+
22+
23+
}

src/com/ernest/easy/_009.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.ernest.easy;
2+
3+
4+
public class _009 {
5+
6+
7+
public boolean isPalindrome_0(int source) {
8+
int copySource = source;
9+
int reverse = 0;
10+
while (source > 0) {
11+
reverse = reverse * 10 + source % 10;
12+
source /= 10;
13+
}
14+
return copySource == reverse;
15+
}
16+
17+
/**
18+
* WAY:首先确定一条原则可行的路,全面考虑可能得情况;具体执行时,重点关注临界点,前后判断差异。
19+
* @param source
20+
* @return
21+
*/
22+
public boolean isPalindrome_1(int source) {
23+
if (source < 0 || (source != 0 && source % 10 == 0)) {
24+
return false;
25+
}
26+
27+
int halfReverse = 0;
28+
29+
/**
30+
* when source is odd, source will smaller than halfReverse one bit.
31+
* when source is even, source will equal with halfReverse.
32+
*/
33+
while (source > halfReverse) {
34+
// add one bit form source to halfReverse
35+
halfReverse = halfReverse * 10 + source % 10;
36+
// source delete the bit who is added.Add if source is odd,the middle bit will drop now.
37+
source /= 10;
38+
}
39+
40+
System.out.println("halfReverse" + halfReverse);
41+
System.out.println(source);
42+
return source == halfReverse || source == halfReverse / 10;
43+
44+
}
45+
46+
public static void main(String[] args) {
47+
48+
_009 solution = new _009();
49+
System.out.println(solution.isPalindrome_0(1234321));
50+
System.out.println(solution.isPalindrome_1(1234321));
51+
}
52+
}
53+

src/com/ernest/easy/_013.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ernest.easy;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _013 {
7+
8+
public int romanToInt(String s) {
9+
Map<Character, Integer> map = new HashMap<>();
10+
map.put('I', 1);
11+
map.put('V', 5);
12+
map.put('X', 10);
13+
map.put('L', 50);
14+
map.put('C', 100);
15+
map.put('D', 500);
16+
map.put('M', 1000);
17+
int len = s.length();
18+
19+
// s.chatAt(len-1) 是最后一位,不是第一位,字符串默认,从左至右,指针依次递增
20+
int sum = map.get(s.charAt(len - 1));
21+
22+
for (int i = len - 2; i >= 0; --i) {
23+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
24+
sum -= map.get(s.charAt(i));
25+
} else {
26+
sum += map.get(s.charAt(i));
27+
}
28+
}
29+
return sum;
30+
}
31+
32+
public static void main(String[] args) {
33+
_013 solution = new _013();
34+
System.out.println(solution.romanToInt("IV"));
35+
System.out.println(solution.romanToInt("VI"));
36+
System.out.println(solution.romanToInt("IVVI"));
37+
}
38+
}

0 commit comments

Comments
 (0)