diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ec58ff0 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..401d515 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..377f290 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,944 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1453122864267 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/java/PlusOne.java b/java/PlusOne.java new file mode 100644 index 0000000..85c2356 --- /dev/null +++ b/java/PlusOne.java @@ -0,0 +1,29 @@ +public class Solution { + public int[] plusOne(int[] digits) { + int length = digits.length; + int sum; + int one = 1;//plus one, it contains carry as well + HashMap map = new HashMap<>(); + for (int i = length - 1; i >= 0; i--) {//from end to top + sum = one + digits[i];//add the number i level digit with the carry + one = sum / 10;//get the new carry + map.put(i, sum % 10);//put the finally new digit to map + } + if (one > 0) { + int[] ret = new int[map.size() + 1]; + ret[0] = one; + for (int j = 0; j < map.size(); j++) { + ret[j+1] = map.get(j); + } + return ret; + } else { + int[] ret = new int[map.size()]; + for (int j = 0; j < map.size(); j++) { + ret[j] = map.get(j); + } + return ret; + } + + + } +} \ No newline at end of file diff --git a/java/RemoveDuplicates.java b/java/RemoveDuplicates.java new file mode 100644 index 0000000..cd9704d --- /dev/null +++ b/java/RemoveDuplicates.java @@ -0,0 +1,36 @@ +/** + * Created by sooglejay on 16/1/12. + */ +public class RemoveDuplicates { + public static void main(String[] args) { + System.out.print(removeDuplicates(new int[]{1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4 + })); + + } + + /*** + * 使用两个下标,i,j,i做遍历,j做新数组长度 + * @param nums + * @return + */ + public static int removeDuplicates(int[] nums) { + int n; + if ((n = nums.length) < 1) { + return n; + } + int j = 0; + int num = 0; + for (int i = 1; i < n; i++) { + if (nums[j] == nums[i]) { + num++; + if (num < 2) {//超过两个就不管了, + nums[++j] = nums[i]; + } + } else {//把剩下的都往前面搬 + nums[++j] = nums[i]; + num = 0; + } + } + return j + 1; + } +} diff --git a/java/RemoveValue.java b/java/RemoveValue.java new file mode 100644 index 0000000..7ea2311 --- /dev/null +++ b/java/RemoveValue.java @@ -0,0 +1,30 @@ +/** + * Created by sooglejay on 16/1/12. + */ +public class RemoveValue { + public static void main(String args[]) { + System.out.print(removeElement(new int[]{4, 5}, 4)); + } + + /** + * 删除数组中的特定值,使用变量newLength来计算新数组的长度 + * @param nums + * @param val + * @return + */ + public static int removeElement(int[] nums, int val) { + int length; + if ((length = nums.length) < 1) { + return length; + } + int newLength = 0; + for (int i = 0; i < length; i++) { + if (nums[i] == val) { + continue; + } + nums[newLength] = nums[i]; + newLength++; + } + return newLength; + } +} diff --git a/java/ReverseLink.java b/java/ReverseLink.java new file mode 100644 index 0000000..7a83401 --- /dev/null +++ b/java/ReverseLink.java @@ -0,0 +1,100 @@ +/** + * Created by sooglejay on 16/1/16. + */ +public class Solution { + //Assume that we have linked list 1 → 2 → 3 → Ø, we would like to change it to Ø ← 1 ← 2 ← 3 + + /** + * efficient solution + * + * @param head + * @return + */ + public ListNode reverseList1(ListNode head) { + if (head == null || head.next == null) return head; + ListNode p = reverseList1(head.next); + head.next.next = head; + head.next = null; + return p; + } + + /** + * efficient solution + * + * tips: + * While you are traversing the list, + * change the current node's next pointer to point to its previous element + * @param head + * @return + */ + public ListNode reverseList2(ListNode head) { + ListNode prev = null; + ListNode curr = head; + while (curr != null) { + ListNode nextTemp = curr.next; + curr.next = prev; + prev = curr; + curr = nextTemp; + } + return prev; + } + + /** + * inefficient solution + * + * @param head + * @return + */ + public static ListNode reverseList3(ListNode head) { + int size = 0; + ListNode tempNode = head; + //get the size of the list + while (tempNode != null) { + size++; + tempNode = tempNode.next; + } + if (size < 2) { + return head; + } + //data array to store each value of each node + int data[] = new int[size]; + while (head != null) { + data[--size] = head.val;//store array elements from end to start + head = head.next; + } + + ListNode footer = new ListNode(data[0]); + head = footer; + //recreate a new list + for (int j = 1; j < data.length; j++) { + ListNode node = new ListNode(data[j]); + footer.next = node; + footer = footer.next; + } + return head; + } + +} + +/** + * Created by sooglejay on 16/1/16. + */ +private static class ListNode { + + int val; + ListNode next; + + ListNode(int x) { + val = x; + } + + @Override + public String toString() { + return "ListNode{" + + "val=" + val + + ", next=" + next + + '}'; + } + + +}