Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
s50600822 committed Nov 1, 2023
1 parent b74d0cf commit ef0ce92
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hoa.can.code.ez;

/**
* https://leetcode.com/problems/add-binary/
*/
public class AddBinary {
public String addBinary(String a, String b) {
StringBuilder result = new StringBuilder();
int carry = 0;
int i = a.length() - 1;
int j = b.length() - 1;

while (i >= 0 || j >= 0 || carry > 0) {
int sum = carry;
if (i >= 0) {
sum += a.charAt(i) - '0';
i--;
}
if (j >= 0) {
sum += b.charAt(j) - '0';
j--;
}

carry = sum / 2;
result.insert(0, sum % 2);
}

return result.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package hoa.can.code.gg;

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.min;

public class JumpingDP implements Jumping {
public boolean canJump(int[] nums) {
return minJumps(nums) != Integer.MAX_VALUE;
return minJumps(nums) != MAX_VALUE;
}

public int minJumps(int[] arr) {
int n = arr.length;
int[] minCostToIdx = new int[n];
int[] cost = new int[n];
int i, j;

if (n == 0 || arr[0] == 0)
return Integer.MAX_VALUE;

minCostToIdx[0] = 0;
return MAX_VALUE;

cost[0] = 0;
for (i = 1; i < n; i++) {
minCostToIdx[i] = Integer.MAX_VALUE;
cost[i] = MAX_VALUE;
for (j = 0; j < i; j++) {
if (i <= j + arr[j] && minCostToIdx[j] != Integer.MAX_VALUE) {
minCostToIdx[i] = Math.min(minCostToIdx[i], minCostToIdx[j] + 1);
if (i <= j + arr[j] && cost[j] != MAX_VALUE) {
cost[i] = min(cost[i], cost[j] + 1);
break;
}
}
}
return minCostToIdx[n - 1];
return cost[n - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hoa.can.code;

import hoa.can.code.ez.AddBinary;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AddBinaryTest {
AddBinary sol = new AddBinary();

@Test
@DisplayName("11 + 1 = 100")
public void test1() {
assertEquals("100", sol.addBinary("11","1"));
}

@Test
@DisplayName("1 + 11 = 100")
public void test2() {
assertEquals("100", sol.addBinary("1","11"));
}

@Test
@DisplayName("11 + 1 = 100")
public void test3() {
assertEquals("10101", sol.addBinary("1010","1011"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testMapcoloring() {
Map<String, String> solution = csp.backtrackingSearch();

assertEquals(territories.size(), solution.size(), "solution found!");
System.out.println(solution);//preview
//System.out.println(solution);//preview
}

@Test
Expand Down

0 comments on commit ef0ce92

Please sign in to comment.