This commit is contained in:
xiongraorao 2018-08-31 21:35:50 +08:00
parent 2f000929d8
commit 2c7460783c
10 changed files with 313 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.raorao.interview.tencent;
import java.util.Scanner;
/**
* 8.31 模拟题.
*
* @author Xiong Raorao
* @since 2018-08-31-20:52
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int x =scanner.nextInt();
int y = scanner.nextInt();
System.out.println(process(k,a,b,x,y));
}
private static long process(int k, int a, int b, int x, int y) {
long ans = 0;
int mod = 1000000007;
int[][] c = new int[105][105];
c[0][0] = 1;
for (int i = 1; i <= 100; i++) {
c[i][0] = 1;
for (int j = 1; j <= 100; j++) {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
}
}
for (int i = 0; i <= x; i++) {
if (i * a <= k && (k - a * i) % b == 0 && (-a * i) / b <= y) {
ans = (ans + (c[x][i] * c[y][(k - a * i) / b]) % mod) % mod;
}
}
return ans;
}
}

View File

@ -0,0 +1,19 @@
package com.raorao.leetcode.q172;
/**
* .
*
* @author Xiong Raorao
* @since 2018-08-31-12:49
*/
public class TrailingZeros {
public static void main(String[] args) {
int res = trailingZeroes(10);
System.out.println(res);
}
public static int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
}

View File

@ -0,0 +1,32 @@
package com.raorao.leetcode.q204;
/**
* 统计素数的数量.
*
* @author Xiong Raorao
* @since 2018-08-31-11:33
*/
public class CountPrime {
public static void main(String[] args) {
CountPrime prime = new CountPrime();
int res = prime.countPrimes(10);
System.out.println(res);
}
public int countPrimes(int n) {
boolean[] notPrimes = new boolean[n + 1];
int count = 0;
for (int i = 2; i < n; i++) {
if (notPrimes[i]) {
continue;
}
count++;
// i * i 开始因为如果 k < i那么 k * i 在之前就已经被去除过了
for (long j = (long) (i) * i; j < n; j += i) {
notPrimes[(int) j] = true;
}
}
return count;
}
}

View File

@ -0,0 +1,29 @@
package com.raorao.leetcode.q217;
import java.util.HashMap;
import java.util.Map;
/**
* 判断是否存在重复元素.
*
* @author Xiong Raorao
* @since 2018-08-31-10:31
*/
public class ContainsDuplicate {
public static void main(String[] args) {
}
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int i : nums) {
if (map.containsKey(i)) {
return true;
} else {
map.put(i, 1);
}
}
return false;
}
}

View File

@ -0,0 +1,36 @@
package com.raorao.leetcode.q238;
import java.util.Arrays;
/**
* 除自身以外数组的乘积.
*
* @author Xiong Raorao
* @since 2018-08-31-16:16
*/
public class ProductExceptSelf {
public static void main(String[] args) {
int[] input = new int[]{1,2,3,4};
ProductExceptSelf self = new ProductExceptSelf();
int[] res = self.productExceptSelf(input);
System.out.println(Arrays.toString(res));
}
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] products = new int[n];
Arrays.fill(products, 1);
int left = 1;
for (int i = 1; i < n; i++) {
left *= nums[i - 1];
products[i] *= left;
}
int right = 1;
for (int i = n - 2; i >= 0; i--) {
right *= nums[i + 1];
products[i] *= right;
}
return products;
}
}

View File

@ -0,0 +1,17 @@
package com.raorao.leetcode.q326;
/**
* 判断一个数是否是3的幂.
*
* @author Xiong Raorao
* @since 2018-08-31-16:11
*/
public class PowerOfThree {
public boolean isPowerOfThree(int n) {
while (n >= 3 && n % 3 == 0) {
n = n / 3;
}
return n == 1;
}
}

View File

@ -0,0 +1,22 @@
package com.raorao.leetcode.q367;
/**
* 判断有效的完全平方数.
*
* @author Xiong Raorao
* @since 2018-08-31-16:04
*/
public class PerfectSquare {
/**
* 利用平方数的差为等差数列的性质来计算
*/
public boolean isPerfectSquare(int num) {
int sub = 1;
while (num > 0) {
num -= sub;
sub += 2;
}
return num == 0;
}
}

View File

@ -0,0 +1,69 @@
package com.raorao.leetcode.q462;
/**
* 改变数组元素使所有的数组元素都相等求最少的总移动次数.
*
* 思路找出中位数可以采用排序然后计算也可以采用找中位数的方法来计算
*
* @author Xiong Raorao
* @since 2018-08-31-15:22
*/
public class MinMove {
public static void main(String[] args) {
int[] input = new int[]{1,2,3};
int res = new MinMove().minMoves2(input);
System.out.println(res);
}
public int minMoves2(int[] nums) {
int median = getMedian(nums);
int count = 0;
for (int num : nums) {
count += Math.abs(num - median);
}
return count;
}
private int getMedian(int[] nums) {
int low = 0;
int high = nums.length - 1;
while (low < high) {
int m = partition(nums, low, high);
if (m == nums.length / 2) {
break;
}
if (m < nums.length / 2) {
low = m + 1;
} else {
high = m - 1;
}
}
return nums[nums.length / 2];
}
private int partition(int[] nums, int low, int high) {
int i = low;
int j = high + 1;
while (true) {
while (nums[++i] < nums[low] && i < high) {
;
}
while (nums[--j] > nums[low] && j > low) {
;
}
if (i >= j) {
break;
}
swap(nums, i, j);
}
swap(nums, low, j);
return j;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

View File

@ -0,0 +1,44 @@
package com.raorao.leetcode.q504;
/**
* 10进制转7进制.
*
* @author Xiong Raorao
* @since 2018-08-31-11:54
*/
public class Base7 {
public static void main(String[] args) {
Base7 base7 = new Base7();
String res = base7.convertToBase7(100);
System.out.println(res);
}
public String convertToBase7(int num) {
StringBuilder sb = new StringBuilder();
convertBase(sb, num);
if (num < 0) {
sb.append("-");
}
return sb.reverse().toString();
}
private void convertBase(StringBuilder sb, int num) {
num = Math.abs(num);
if (num == 0) {
sb.append(0);
return;
}
int a = num / 7;
int b = num % 7;
sb.append(b);
if (a >= 7) {
convertBase(sb, a);
} else if( a > 0) {
sb.append(a);
}
}
}

View File

@ -47,6 +47,9 @@ Intel | 8.23 | 8.23(新投递) | 电面 | 8.24一面
唯品会 | | 8.30(实时计算工程师) |
珍爱网 | | 8.30(新投递) |
寒武纪 | 8.30 | 8.30(新投递) |
商汤 | 8.31 | 8.31(新投递) |
Keep | 8.31 | 8.31(新投递)
小红书 | 8.31 | 8.31(新投递)
# 2 复习内容