NC_150_BINARY_TREE_COUNT NC_151_GCD NC_156_FIND_ONCE_NUMBER
package org.example.interview.practice; /** * @author xianzhe.ma * @date 2021/8/22 * 左子树的数量乘以右子树的数量 */ public class NC_150_BINARY_TREE_COUNT { private static int mod = 1000000007; public int numberOfTree (int n) { // 直接返回 if (n == 1 || n == 2){ return n; } long[] dp = new long[n+1]; dp[0] = 1; dp[1] = 1; dp[2] = 2; for (int i = 3;i < dp.length;++i){ long count = 0; for (int j = 1;j <= i;++j){ count += ((dp[i-j]*dp[j-1])%mod); } dp[i] = (int) (count % mod); } return (int) dp[n]; } }
package org.example.interview.practice; import java.util.regex.Pattern; /** * @author xianzhe.ma * @date 2021/8/18 */ public class NC_151_GCD { public static int gcd (int a, int b) { // write code here if (a%b == 0) { return b; } else { return gcd(b,a%b); } } public static void main (String[] args) { int a = 12; int b = 10; System.out.println(gcd(a,b)); } }
package org.example.interview.practice; import org.redisson.misc.Hash; import java.util.HashSet; import java.util.Set; /** * @author xianzhe.ma * @date 2021/7/14 */ public class NC_156_FIND_ONCE_NUMBER { public static int foundOnceNumber (int[] arr, int k) { // write code here if (arr == null || arr.length == 0) { return -1; } Set<Integer> set = new HashSet<>(); Set<Integer> nominate = new HashSet<>(); int result = arr[0]; for (int num : arr) { if (!set.contains(num)) { set.add(num); nominate.add(num); } else { nominate.remove(num); } } return nominate.stream().findFirst().get(); } public static void main (String[] args) { int[] arr = {5,4,1,1,5,1,5}; System.out.println(foundOnceNumber(arr, 3)); } }