0.8
This commit is contained in:
1 parent
d424ed1891
commit
c03132a99d
8 files changed
+308
No files matched your search
@@ -0,0 +1,32 @@
|
||||
package com.hong.www;
|
||||
|
||||
public class A28 {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
int j=nums.length-1;
|
||||
while (j >= 0 && nums[j] == val) {
|
||||
j--;
|
||||
}
|
||||
if(j==0 && nums[0]==val){
|
||||
return 0;
|
||||
}
|
||||
for(int i=0;i<j;i++){
|
||||
if(nums[i]==val){
|
||||
nums[i]=nums[j];
|
||||
nums[j]=val;
|
||||
j--;
|
||||
/* if(j==i){
|
||||
break;
|
||||
} */
|
||||
while (nums[j]==val && j>i) {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return j+1;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
A28 a28 = new A28();
|
||||
int[] nums = {2};
|
||||
System.out.println(a28.removeElement(nums, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.hong.www;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class A88 {
|
||||
public void merge(int[] nums1, int m, int[] nums2, int n) {
|
||||
for (int i = n-1; i >= 0; i--) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (nums1[j] > nums2[i]) {
|
||||
int temp = nums1[j];
|
||||
nums1[j] = nums2[i];
|
||||
nums2[i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums1[m + i] = nums2[i];
|
||||
}
|
||||
/* Arrays.sort(nums1); */
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.hong.www;
|
||||
|
||||
public class Q1 {
|
||||
public void moveZeroes(int[] nums) {
|
||||
int left = 0;
|
||||
int right = nums.length-1;
|
||||
/* while (nums[right] == 0) {
|
||||
right--;
|
||||
}
|
||||
while (left < right) {
|
||||
if (nums[left] == 0) {
|
||||
nums[left] = nums[right];
|
||||
nums[right]=0;
|
||||
right--;
|
||||
while (nums[right] == 0&&right>left) {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
left++;
|
||||
} */
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if(nums[i]!=0){
|
||||
nums[left]=nums[i];
|
||||
left++;
|
||||
}
|
||||
}
|
||||
for (int i = left; i < nums.length; i++) {
|
||||
nums[i]=0;
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Q1 q1 = new Q1();
|
||||
int[] nums = {0,1,0,3,12};
|
||||
q1.moveZeroes(nums);
|
||||
for (int i : nums) {
|
||||
System.out.print(i+" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.hong.www;
|
||||
|
||||
|
||||
|
||||
public class Q12 {
|
||||
public ListNode dfs(ListNode root){
|
||||
ListNode next;
|
||||
ListNode temp=null;
|
||||
while (root!=null) {
|
||||
next=root.next;
|
||||
root.next=temp;
|
||||
root=next;
|
||||
temp=root;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
public ListNode reverseList(ListNode head) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
return dfs(head);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.hong.www;
|
||||
|
||||
public class Q2 {
|
||||
public boolean searchMatrix(int[][] matrix, int target) {
|
||||
int ms = 0;
|
||||
int ns = 0;
|
||||
int is=0;
|
||||
for (int m = 0; m < matrix.length; m++) {
|
||||
if (matrix[m][0] == target) {
|
||||
return true;
|
||||
} else if (matrix[m][0] > target) {
|
||||
ms = m - 1;
|
||||
is=1;
|
||||
break;
|
||||
}
|
||||
if(is==0&&m==matrix.length-1){
|
||||
ms = matrix.length - 1;
|
||||
}
|
||||
}
|
||||
is=0;
|
||||
for (int n = 0; n < matrix[0].length; n++) {
|
||||
if (matrix[0][n] == target) {
|
||||
return true;
|
||||
} else if (matrix[0][n]> target) {
|
||||
ns = n - 1;
|
||||
is = 1;
|
||||
break;
|
||||
}
|
||||
if(is==0&&n==matrix[0].length-1){
|
||||
ns = matrix[0].length - 1;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i <= ms; i++) {
|
||||
for (int j = 0; j <= ns; j++) {
|
||||
if(matrix[i][j]==target){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Q2 q2 = new Q2();
|
||||
int[][] matrix = {{1,4},{2,5}};
|
||||
System.out.println(q2.searchMatrix(matrix, 5));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hong.www;
|
||||
|
||||
public class Q22 {
|
||||
public int coinChange(int[] coins, int amount) {
|
||||
int[] dp = new int[amount + 1];
|
||||
dp[0]=0;
|
||||
for(int i=1;i<=amount;i++){
|
||||
int y=Integer.MAX_VALUE;
|
||||
for(int x:coins){
|
||||
if(i>=x){
|
||||
if(y>dp[i-x]&&dp[i-x]!=Integer.MAX_VALUE+1){
|
||||
y=dp[i-x];
|
||||
}
|
||||
}
|
||||
}
|
||||
dp[i]=y+1;
|
||||
}
|
||||
if(dp[amount]==Integer.MAX_VALUE+1){
|
||||
return -1;
|
||||
}
|
||||
return dp[amount];
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Q22 q22 = new Q22();
|
||||
int[] coins = {2};
|
||||
int amount = 3;
|
||||
int result = q22.coinChange(coins, amount);
|
||||
System.out.println(result); // Output: -1 (3 cannot be made with only 2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.hong.www;
|
||||
|
||||
public class Q3 {
|
||||
public int A(int from,int to,int delay,int n,int forget){
|
||||
int MOD=1000000007;
|
||||
if(to-from>=delay){
|
||||
int x=0;
|
||||
for(int i=0;i<to-from-delay;i++){
|
||||
x+=A(from+delay+i,Math.min(n+1,from+delay+i+forget),delay,n,forget);
|
||||
}
|
||||
x%=MOD;
|
||||
if(to<=n){
|
||||
return x;
|
||||
}
|
||||
return x+1;
|
||||
}else{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
public int peopleAwareOfSecret(int n, int delay, int forget) {
|
||||
int MOD=1000000007;
|
||||
//return A(1, Math.min(n+1, 1+forget), delay, n, forget) % MOD;
|
||||
int base=forget-1;
|
||||
int[] dp = new int[n + forget+1];
|
||||
dp[1+base] = 1;
|
||||
for(int i=2;i<=n;i++){
|
||||
for(int j=i+base-forget+1;j<=i+base-delay;j++){
|
||||
dp[i+base]+=dp[j];
|
||||
dp[i+base]%=MOD;
|
||||
}
|
||||
}
|
||||
int x=0;
|
||||
for(int i=n+base-forget+1;i<=n+base;i++){
|
||||
x+=dp[i];
|
||||
x%=MOD;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Q3 q3 = new Q3();
|
||||
|
||||
System.out.println(q3.peopleAwareOfSecret(684, 18, 496));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.hong.www;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q32 {
|
||||
public void nextPermutation(int[] nums) {
|
||||
for(int i=nums.length-1;i>=0;i--){
|
||||
if(nums[i]>nums[i-1]){
|
||||
for(int j=nums.length-1;j>i-1;j--){
|
||||
if(nums[j]>nums[i-1]){
|
||||
int temp=nums[j];
|
||||
nums[j]=nums[i-1];
|
||||
nums[i-1]=temp;
|
||||
Arrays.sort(nums,i,nums.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Arrays.sort(nums);
|
||||
/* int[] temp1=new int[2];
|
||||
Arrays.fill(temp1, Integer.MIN_VALUE);
|
||||
int up=0;
|
||||
int down=1;
|
||||
int temp=Integer.MIN_VALUE;
|
||||
for(int i=0;i<nums.length;i++){
|
||||
if(i==0){
|
||||
temp=nums[0];
|
||||
}else{
|
||||
if(nums[i]>temp&&up==0){
|
||||
up=1;
|
||||
down=0;
|
||||
|
||||
}else if(nums[i]>temp&&up==1){
|
||||
|
||||
}else if(nums[i]<=temp&&down==0){
|
||||
temp1[0]=i-1;
|
||||
temp1[1]=i-2;
|
||||
down=1;
|
||||
up=0;
|
||||
}else if(nums[i]<=temp&&down==1){
|
||||
|
||||
}
|
||||
if(i==nums.length-1){
|
||||
if(up==1){
|
||||
temp1[0]=i;
|
||||
temp1[1]=i-1;
|
||||
}
|
||||
}
|
||||
temp=nums[i];
|
||||
}
|
||||
|
||||
}
|
||||
if(temp1[0]!=Integer.MIN_VALUE){
|
||||
temp=nums[temp1[0]];
|
||||
nums[temp1[0]]=nums[temp1[1]];
|
||||
nums[temp1[1]]=temp;
|
||||
Arrays.sort(nums,temp1[0],nums.length);
|
||||
}else{
|
||||
Arrays.sort(nums);
|
||||
} */
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Q32 q32=new Q32();
|
||||
int[] nums={3,2,1};
|
||||
q32.nextPermutation(nums);
|
||||
System.out.println(Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user