diff --git a/src/com/hong/www/A198.java b/src/com/hong/www/A198.java new file mode 100644 index 0000000..cb66df4 --- /dev/null +++ b/src/com/hong/www/A198.java @@ -0,0 +1,38 @@ +package com.hong.www; + +public class A198 { + public int rob(int[] nums) { + int[] dp = new int[nums.length + 1]; + if (nums.length == 1) { + dp[1] = nums[0]; + } else if (nums.length == 2) { + dp[1] = nums[0]; + dp[2] = Math.max(nums[0], nums[1]); + } else { + dp[1] = nums[0]; + dp[2] = Math.max(nums[0], nums[1]); + for (int i = 3; i <= nums.length; i++) { + /* int temp = 0; + for(int j=i-2;j>=1;j--){ + if(dp[j] + nums[i - 1]>temp){ + temp=dp[j]+nums[i-1]; + } + } */ + dp[i] = Math.max(dp[i - 2] + nums[i - 1], dp[i - 1]); + } + + } + int temp = 0; + for (int x : dp) { + if (x > temp) { + temp = x; + } + } + return temp; + } + public static void main(String[] args) { + A198 a198 = new A198(); + int[] nums = {1,2,3,1}; + System.out.println(a198.rob(nums)); + } +} diff --git a/src/com/hong/www/A25.java b/src/com/hong/www/A25.java new file mode 100644 index 0000000..229a8d6 --- /dev/null +++ b/src/com/hong/www/A25.java @@ -0,0 +1,53 @@ +package com.hong.www; + +public class A25 { + public ListNode reverseKGroup(ListNode head, int k) { + ListNode dummy = new ListNode(0, head); + ListNode prev = dummy; + ListNode next; + ListNode curr = head; + int time = 1; + while (true) { + while (time < k) { + time++; + curr = curr.next; + if (curr == null) { + return prev.next; + } + } + next = curr.next; + curr.next = null; + ListNode temp = prev.next; + ListNode last; + ListNode nexttemp = temp.next; + while (nexttemp != null) { + nexttemp = nexttemp.next; + last = temp; + temp = temp.next; + temp.next = last; + last = temp; + } + prev.next.next = null; + prev.next=temp; + prev=temp; + curr=next; + time=1; + } + //return dummy; + } + + public static void main(String[] args) { + A25 a25 = new A25(); + ListNode head = new ListNode(1); + head.next = new ListNode(2); + head.next.next = new ListNode(3); + head.next.next.next = new ListNode(4); + head.next.next.next.next = new ListNode(5); + int k = 2; + ListNode result = a25.reverseKGroup(head, k); + while (result != null) { + System.out.print(result.val + " "); + result = result.next; + } + } +} diff --git a/src/com/hong/www/A27.java b/src/com/hong/www/A27.java new file mode 100644 index 0000000..6d88c8f --- /dev/null +++ b/src/com/hong/www/A27.java @@ -0,0 +1,64 @@ +package com.hong.www; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +public class A27 { + public int ladderLength(String beginWord, String endWord, List wordList) { + HashSet history = new HashSet<>(wordList); + ArrayList now = new ArrayList<>(); + int times=0; + now.add(beginWord); + while (true) { + ArrayList now1 = new ArrayList<>(); + times++; + for (String nowword : now) { + char[] temp = nowword.toCharArray(); + + for (int i = 0; i < temp.length; i++) { + for (char j = 'a'; j <= 'z'; j++) { + char[] temp1 = Arrays.copyOf(temp, temp.length); + temp1[i] = j; + String u=new String(temp1); + if(history.contains(u)){ + history.remove(u); + now1.add(u); + if(u.equals(endWord)){ + return times+1; + } + } + /* for (String p : wordList) { + + if (p.equals(u)&&!history.contains(p)) { + history.add(p); + now1.add(p); + if(p.equals(endWord)){ + return times+1; + } + } + } */ + } + } + } + if(now1.size()==0){ + return 0; + } + now=now1; + } + + //return 0; + } + public static void main(String[] args) { + A27 a27 = new A27(); + List list = new ArrayList<>(); + list.add("hot"); + list.add("dot"); + list.add("dog"); + list.add("lot"); + list.add("log"); + list.add("cog"); + System.out.println(a27.ladderLength("hit", "cog", list)); + } +} diff --git a/src/com/hong/www/A42.java b/src/com/hong/www/A42.java new file mode 100644 index 0000000..0cb92d9 --- /dev/null +++ b/src/com/hong/www/A42.java @@ -0,0 +1,32 @@ +package com.hong.www; + +public class A42 { + public int trap(int[] height) { + int max = -1; + int temp = -1; + for(int x=0;x temp){ + temp = height[x]; + max=x; + } + } + temp=height[0]; + int finalcount=0; + for(int x=1;xtemp){ + temp=height[x]; + }else{ + finalcount+=temp-height[x]; + } + } + temp=height[-1]; + for(int x=height.length;x>=max;x--){ + if(height[x]>temp){ + temp=height[x]; + }else{ + finalcount+=temp-height[x]; + } + } + return finalcount; + } +} diff --git a/src/com/hong/www/ListNode.java b/src/com/hong/www/ListNode.java new file mode 100644 index 0000000..0beaeff --- /dev/null +++ b/src/com/hong/www/ListNode.java @@ -0,0 +1,9 @@ +package com.hong.www; + +public class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int x) { val = x; } + ListNode(int x, ListNode next) { val = x; this.next = next; } +}