问题一:
孩子们的游戏 :
每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
如果没有小朋友,请返回-1
法一:
f[1]=0;
f[i]=(f[i-1]+ k + 1)%n = (f[i-1]+ (m - 1)%n + 1)%n = (f[i-1]+ m - 1 + 1 + n)%n = (f[i-1]+ m)%n; (i>1)
public class Solution {
public int LastRemaining_Solution(int n, int m) {
if (n == 0) {
return -1;
}
int ans = 0;
for (int i = 2; i < n + 1; i++) {
ans = (ans + m) % i;
}
return ans;
}
}
法二:
用循环链表模拟游戏过程
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public class Solution {
public int LastRemaining_Solution(int n, int m) {
if (n == 0) {
return -1;
}
ListNode head = new ListNode(0);
ListNode node = head;
for (int i = 1; i < n; i++) {
node.next = new ListNode(i);
node = node.next;
}
node.next = head;
int k = 0;
while (node.next != node) {
if ((k++ % m) == m - 1) {
node.next = node.next.next;
}
else {
node = node.next;
}
}
return node.val;
}
}
问题二:
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
法一:
public class Solution {
public int Sum_Solution(int n) {
int sum = n;
boolean isStop = (sum > 0) && ((sum += Sum_Solution(n - 1)) > 0);
return sum;
}
}
法二:
模拟二进制乘法计算 sum =
public class Solution {
int [] mask = {0x00000000, 0xFFFFFFFF};
public int Sum_Solution(int n) {
return product(n + 1, n) >> 1;
}
private int product(int m, int n) {
int ans = 0;
boolean isStop = (m > 0) && (ans += (n & mask[m & 1]) + product(m >> 1, n << 1)) > 0;
return ans;
}
}
问题三:
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
public class Solution {
public int Add(int num1,int num2) {
return num2 != 0 ? Add(num1 ^ num2, (num1 & num2) << 1) : num1;
}
}
问题四:
题目描述
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0
输入描述:
输入一个字符串,包括数字字母符号,可以为空
输出描述:
如果是合法的数值表达则返回该数字,否则返回0
示例1
输入
+2147483647
1a33
输出
2147483647
0
public class Solution {
public int StrToInt(String str) {
if (!str.matches("[1-9,+,-]\\d*")) {
return 0;
}
int len = str.length() - 1;
long ans = 0;
for (int i = len; i >= 0 && Character.isDigit(str.charAt(i)); i--) {
ans += Math.pow(10, len - i) * (str.charAt(i) - '0');
}
ans = (str.charAt(0) == '-' ? -ans : ans);
if (ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) {
return 0;
}
return (int)ans;
}
}
问题五:
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
1. 排序
import java.util.*;
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if (numbers == null || length == 0) {
return false;
}
Arrays.sort(numbers);
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] == numbers[i + 1]) {
duplication[0] = numbers[i];
return true;
}
}
return false;
}
}
时间复杂度:O(nlogn)
空间复杂度:O(1)
2. HashSet
import java.util.*;
public class Solution {
public boolean duplicate(int numbers[],int length,int [] duplication) {
if (numbers == null || length == 0) {
return false;
}
HashSet<Integer> s = new HashSet<>();
for (int i = 0; i < numbers.length; i++) {
if (s.contains(numbers[i])) {
duplication[0] = numbers[i];
return true;
}
else {
s.add(numbers[i]);
}
}
return false;
}
}
import java.util.*;
public class Solution {
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
if (numbers == null || length == 0) {
return false;
}
for (int i = 0; i < numbers.length; i++) {
while (numbers[i] != i) {
if (numbers[i] == numbers[numbers[i]]) {
duplication[0] = numbers[i];
return true;
}
else {
int temp = numbers[numbers[i]];
numbers[numbers[i]] = numbers[i];
numbers[i] = temp;
}
}
}
return false;
}
}
问题六:
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。(注意:规定B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2];)
import java.util.ArrayList;
public class Solution {
public int[] multiply(int[] A) {
int [] ans = new int[A.length];
if (A.length == 0) {
return ans;
}
ans[0] = 1;
//从左到右算 B[i]=A[0]*A[1]*...*A[i-1]
for (int i = 1; i < A.length; i++) {
ans[i] = ans[i - 1] * A[i - 1];
}
int temp = 1;
//从右到左算B[i]*=A[i+1]*...*A[n-1]
for (int j = A.length - 2; j >= 0; j--) {
temp *= A[j + 1];
ans[j] *= temp;
}
return ans;
}
}