This commit is contained in:
Mrhongxing committed 2026-10-13 11:44:32 +08:00
1 parent b3c7a28b1c
commit 55faca1edf
5 files changed
+196

No files matched your search

+38
View File
@@ -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));
}
}
+53
View File
@@ -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;
}
}
}
+64
View File
@@ -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<String> wordList) {
HashSet<String> history = new HashSet<>(wordList);
ArrayList<String> now = new ArrayList<>();
int times=0;
now.add(beginWord);
while (true) {
ArrayList<String> 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<String> 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));
}
}
+32
View File
@@ -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<height.length;x++){
if(height[x] > temp){
temp = height[x];
max=x;
}
}
temp=height[0];
int finalcount=0;
for(int x=1;x<max;x++){
if(height[x]>temp){
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;
}
}
+9
View File
@@ -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; }
}