diff --git a/src/com/ernest/easy/_001.java b/src/com/ernest/easy/_001.java new file mode 100644 index 00000000..ef5dbf58 --- /dev/null +++ b/src/com/ernest/easy/_001.java @@ -0,0 +1,39 @@ +package com.ernest.easy; + +import java.util.Arrays; +import java.util.HashMap; + +public class _001 { + + + public int[] twoSum_0(int[] source, int target) { + for (int i = 0; i < source.length; i++) { + for (int j = i + 1; j < source.length; j++) { + if (source[i] + source[j] == target) { + return new int[]{i, j}; + } + } + } + return null; + } + + public int[] twoSum_1(int[] source, int target) { + int length = source.length; + HashMap map = new HashMap(); + for (int i = 0; i < length; i++) { + if (map.containsKey(source[i])) { + return new int[]{map.get(source[i]), i}; + } + map.put(target - source[i], i); + } + return null; + } + + public static void main(String[] args) { + int[] source = new int[]{2, 7, 11, 15}; + int target = 9; + _001 solution = new _001(); + System.out.println(Arrays.toString(solution.twoSum_0(source, target))); + System.out.println(Arrays.toString(solution.twoSum_1(source, target))); + } +} diff --git a/src/com/ernest/easy/_007.java b/src/com/ernest/easy/_007.java new file mode 100644 index 00000000..3792be37 --- /dev/null +++ b/src/com/ernest/easy/_007.java @@ -0,0 +1,23 @@ +package com.ernest.easy; + +public class _007 { + + + public int reverse(int source) { + int result = 0; + for (; source != 0; source /= 10) { + result = result * 10 + source % 10; + } + return result > Integer.MAX_VALUE | result < Integer.MIN_VALUE ? 0 : result; + } + + public static void main(String[] args) { + int source1 = 123; + int source2 = -123; + _007 solution = new _007(); + System.out.println(solution.reverse(source1)); + System.out.println(solution.reverse(source2)); + } + + +} diff --git a/src/com/ernest/easy/_009.java b/src/com/ernest/easy/_009.java new file mode 100644 index 00000000..dfdb6bbe --- /dev/null +++ b/src/com/ernest/easy/_009.java @@ -0,0 +1,53 @@ +package com.ernest.easy; + + +public class _009 { + + + public boolean isPalindrome_0(int source) { + int copySource = source; + int reverse = 0; + while (source > 0) { + reverse = reverse * 10 + source % 10; + source /= 10; + } + return copySource == reverse; + } + + /** + * WAY:首先确定一条原则可行的路,全面考虑可能得情况;具体执行时,重点关注临界点,前后判断差异。 + * @param source + * @return + */ + public boolean isPalindrome_1(int source) { + if (source < 0 || (source != 0 && source % 10 == 0)) { + return false; + } + + int halfReverse = 0; + + /** + * when source is odd, source will smaller than halfReverse one bit. + * when source is even, source will equal with halfReverse. + */ + while (source > halfReverse) { + // add one bit form source to halfReverse + halfReverse = halfReverse * 10 + source % 10; + // source delete the bit who is added.Add if source is odd,the middle bit will drop now. + source /= 10; + } + + System.out.println("halfReverse" + halfReverse); + System.out.println(source); + return source == halfReverse || source == halfReverse / 10; + + } + + public static void main(String[] args) { + + _009 solution = new _009(); + System.out.println(solution.isPalindrome_0(1234321)); + System.out.println(solution.isPalindrome_1(1234321)); + } +} + diff --git a/src/com/ernest/easy/_013.java b/src/com/ernest/easy/_013.java new file mode 100644 index 00000000..aac23824 --- /dev/null +++ b/src/com/ernest/easy/_013.java @@ -0,0 +1,38 @@ +package com.ernest.easy; + +import java.util.HashMap; +import java.util.Map; + +public class _013 { + + public int romanToInt(String s) { + Map map = new HashMap<>(); + map.put('I', 1); + map.put('V', 5); + map.put('X', 10); + map.put('L', 50); + map.put('C', 100); + map.put('D', 500); + map.put('M', 1000); + int len = s.length(); + + // s.chatAt(len-1) 是最后一位,不是第一位,字符串默认,从左至右,指针依次递增 + int sum = map.get(s.charAt(len - 1)); + + for (int i = len - 2; i >= 0; --i) { + if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) { + sum -= map.get(s.charAt(i)); + } else { + sum += map.get(s.charAt(i)); + } + } + return sum; + } + + public static void main(String[] args) { + _013 solution = new _013(); + System.out.println(solution.romanToInt("IV")); + System.out.println(solution.romanToInt("VI")); + System.out.println(solution.romanToInt("IVVI")); + } +}