diff --git a/src/ReverseInteger.java b/src/ReverseInteger.java new file mode 100644 index 0000000..6155e65 --- /dev/null +++ b/src/ReverseInteger.java @@ -0,0 +1,21 @@ +public class ReverseInteger { + public int reverse(int x) { + long r = 0; + while (x != 0) { + int carry = x % 10; + x = x / 10; + r = r * 10 + carry; + if (r > Integer.MAX_VALUE || r < Integer.MIN_VALUE) { + return 0; + } + } + return (int)r; + } + + public static void main(String[] args) { + int input = 120; + ReverseInteger solution = new ReverseInteger(); + int output = solution.reverse(input); + System.out.println(output); + } +} diff --git a/src/TwoSum.java b/src/TwoSum.java index e827bfd..7ed99b0 100644 --- a/src/TwoSum.java +++ b/src/TwoSum.java @@ -12,15 +12,14 @@ import java.util.Hashtable; public class TwoSum { - public int[] twoSum(int[] listIn, int target) { - + public int[] twoSum(int[] nums, int target) { Hashtable h = new Hashtable<>(); - for (int i = 0; i < listIn.length; i++) { - int tmp = target - listIn[i]; + for (int i = 0; i < nums.length; i++) { + int tmp = target - nums[i]; if (h.containsKey(tmp)) { return new int[]{h.get(tmp), i}; } - h.put(listIn[i], i); + h.put(nums[i], i); } return new int[]{}; }