31.输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。**
import java.util.ArrayList;
import java.util.*;
public class Solution {
publ ·4tring PrintMinNumber(int [] numbers) {
/5 74W ArrayList<Integer> list = new ArrayList();
if(numbers == null || numbers.length == 0) return "";
for(int i = 0;i<numbers.length;i++){
list.add(numbers[i]);
}
Collections.sort(list,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
String s1 = o1+"" +o2;
String s2 = o2+""+o1;
Integer i1 = Integer.parseInt(s1);
Integer i2 = Integer.parseInt(s2);
return i1.compareTo(i2);
}
});
String s = "";
for(int i = 0;i<list.size();i++) {
s+= list.get(i);
}
return s;
}
}
32.把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
public class Solution {
public int GetUglyNumber_Solution(int index) {
if(index <=0) return 0;
int [] result = new int[index];
int count = 0;
int i2 = 0;
int i3 = 0;
int i5 = 0;
result[0] = 1;
int tmp = 0;
while(count<index-1){
tmp = Math.min(result[i2]*2,Math.min(result[i3]*3,result[i5]*5));
if(tmp == result[i2]*2) i2++;
if(tmp == result[i3]*3) i3++;
if(tmp == result[i5]*5) i5++;
result[++count] =tmp;
}
return result[index-1];
}
}
33.在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
import java.util.*;
public class Solution {
public int FirstNotRepeatingChar(String str) {
LinkedHashMap <Character, Integer> map = new LinkedHashMap<Character, Integer>();
for(int i=0;i<str.length();i++){
if(map.containsKey(str.charAt(i))){
int time = map.get(str.charAt(i));
map.put(str.charAt(i), ++time);
}
else {
map.put(str.charAt(i), 1);
}
}
int pos = -1;
int i=0;
for(;i<str.length();i++){
char c = str.charAt(i);
if (map.get(c) == 1) {
return i;
}
}
return pos;
}
}
34.在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
public class Solution {
public int InversePairs(int [] array) {
if(array == null || array.length == 0) return 0;
int [] copy = new int[array.length];
for(int i = 0;i<array.length;i++){
copy[i] = array[i];
}
int count = InversePairsCore(array,copy,0,array.length-1);
return count;
}
private static int InversePairsCore(int[] array,int []copy,int low,int high){
if(low == high){
return 0;
}
int mid = (high+low)>>1;
int leftCount = InversePairsCore(array,copy,low,mid)%1000000007;
int rightCount = InversePairsCore(array,copy,mid+1,high)%1000000007;
int count = 0;
int i = mid;
int j = high;
int locCopy = high;
while(i>=low&&j>mid){
if(array[i]>array[j]){
count+= j-mid;
copy[locCopy--] = array[i--];
if(count>=1000000007){
count%=1000000007;
}
}else{
copy[locCopy--] = array[j--];
}
}
for(;i>=low;i--){
copy[locCopy--] = array[i];
}
for(;j>mid;j--){
copy[locCopy--] = array[j];
}
for(int s = low;s<=high;s++){
array[s] = copy[s];
}
return (leftCount+rightCount+count)%1000000007;
}
}
35.输入两个链表,找出它们的第一个公共结点。
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode p1 = pHead1,p2 = pHead2;
while(p1!=p2){
p1 = (p1==null)?pHead2:p1.next;
p2 = (p2 == null)?pHead1:p2.next;
}
return p1;
}
}
36.统计一个数字在排序数组中出现的次数。
public class Solution {
public int GetNumberOfK(int [] array , int k) {
if(array == null || array.length== 0) return 0;
int i = getFirstK(array,k);
int j = getLastK(array,k);
while(i!=-1&& j!=-1){
return (j-i+1);
}
return 0;
}
public int getFirstK(int[] array,int k){
int start = 0;
int end = array.length - 1;
while(start <= end){
int mid = (start +end)>>1;
if(array[mid]<k){
start = mid+1;
}else if(array[mid]>k){
end = mid -1;
}else if(mid-1>=0&& array[mid-1] == k){
end = mid - 1;
}else {
return mid;
}
}
return -1;
}
public int getLastK(int[] array,int k){
int start = 0;
int end = array.length - 1;
while(start <= end){
int mid = (start +end)>>1;
if(array[mid]<k){
start = mid+1;
}else if(array[mid]>k){
end = mid -1;
}else if(mid+1<array.length&& array[mid+1] == k){
start = mid+1;
}else {
return mid;
}
}
return-1;
}
}
37.输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
public class Solution {
public int TreeDepth(TreeNode root) {
if(root == null) return 0;
int left = TreeDepth(root.left);
int right = TreeDepth(root.right);
return Math.max(left,right)+1;
}
}
38.输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null) return true;
int left = isBalanced(root.left);
int right = isBalanced(root.right);
if(Math.abs(left-right)>1) return false;
return true;
}
public int isBalanced(TreeNode root){
if(root == null) return 0;
int left = isBalanced(root.left);
int right = isBalanced(root.right);
return Math.max(left,right)+1;
}
}
39.一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if(array == null || array.length<=1) {
num1[0] =0;
num2[0] = 0;
return ;
}
int sum =0,index =0,len = array.length;
for(int i = 0;i<array.length;i++){
sum^=array[i];
}
for(;index<32;index++){
if((sum&(1<<index))!=0) break;
}
for(int i = 0;i<array.length;i++){
if((array[i]&(1<<index))!=0){
num1[0]^=array[i];
}else {
num2[0]^=array[i];
}
}
}
}
/**
* 数组a中只有一个数出现一次,其他数都出现了2次,找出这个数字
* @param a
* @return
*/
public static int find1From2(int[] a){
int len = a.length, res = 0;
for(int i = 0; i < len; i++){
res = res ^ a[i];
}
return res;
}
/**
/**
* 数组a中只有一个数出现一次,其他数字都出现了3次,找出这个数字
* @param a
* @return
*/
public static int find1From3(int[] a){
int[] bits = new int[32];
int len = a.length;
for(int i = 0; i < len; i++){
for(int j = 0; j < 32; j++){
bits[j] = bits[j] + ( (a[i]>>>j) & 1);
}
}
int res = 0;
for(int i = 0; i < 32; i++){
if(bits[i] % 3 !=0){
res = res | (1 << i);
}
}
return res;
}
40.小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
if(sum<=2) return list;
int low = 1,high = 2;
int cur = 0;
while(low<high){
cur = (low+high)*(high-low+1)/2;
if(cur == sum){
ArrayList<Integer> list2 = new ArrayList<Integer>();
for(int i = low;i<=high;i++){
list2.add(i);
}
list.add(list2);
low++;
}
if(cur<sum){
high++;
}
if(cur>sum){
low++;
}
}
return list;
}
41.输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
对应每个测试案例,输出两个数,小的先输出。
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) {
ArrayList<Integer> list = new ArrayList<Integer>();
int low = 0;
int high = array.length-1;
while(low<high){
int total = array[low]+array[high] ;
if(total==sum){
list.add(array[low]);
list.add(array[high]);
break;
}else if(total < sum){
low++;
}else{
high--;
}
}
return list;
}
}
42.汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
public class Solution {
public String LeftRotateString(String str,int n) {
if(str == null || str.length() == 0) return "";
if(n == str.length()) return str;
if(n>str.length()) n%=str.length();
char [] c = str.toCharArray();
rol(c,0,n-1);
rol(c,n,str.length()-1);
rol(c,0,str.length()-1);
return new String(c);
}
public void rol(char []c,int start,int n){
int i = start;
int j = n;
while(i <j){
char temp = c[i];
c[i] = c[j];
c[j] = temp;
i++;
j--;
}
}
}
43.牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
import java.util.*;
public class Solution {
public static String ReverseSentence(String str) {
if(str == null ) return null;
if(str.trim().equals("") ) return str;
String [] s = str.split(" ");
StringBuilder sb = new StringBuilder();
for(int i = s.length-1;i>=0;i--){
if(i==0) {
sb.append(s[i]);
}else {
sb.append(s[i]).append(" ");
}
}
return sb.toString();
}
}
44.LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
import java.util.*;
public class Solution {
public boolean isContinuous(int [] numbers) {
if(numbers == null || numbers.length == 0) return false;
Arrays.sort(numbers);
int zero = 0,i = 0;
for(;i<numbers.length&&numbers[i]==0;i++){
zero++;
}
for(;i<numbers.length-1&&zero>=0;i++){
if(numbers[i]==numbers[i+1]) return false;
if(numbers[i]+1+zero>=numbers[i+1]){
zero-=numbers[i+1]-numbers[i]-1;
}else return false;
}
if(i == numbers.length-1) return true;
return false;
}
}
45.每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!_)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
如果没有小朋友,请返回-1
import java.util.ArrayList;
public class Solution {
public int LastRemaining_Solution(int n, int m) {
if(n<=0||m<=0) return -1;
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0;i<n;i++){
list.add(i);
}
int index = -1;
while(list.size()>1){
index = (index+m)%list.size();
list.remove(index);
index --;
}
return list.get(0);
}
}
46.求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 ans =(n>0)&&(sum+=Sum_Solution(n-1))>0;
return sum;
}
}
47.写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
public class Solution {
public int Add(int num1,int num2) {
int n1 = num1;
int n2 = num2;
n1 = (num1&num2)<<1;
n2 = num1^num2;
while((n1&n2)!=0){
int sum1 = n1;
int sum2 = n2;
n1 = (sum1&sum2)<<1;
n2 = sum1^sum2;
}
return n1|n2;
}
}
48.将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
public class Solution {
public int StrToInt(String str) {
String s = "[+-]?[\\d]+";
if(!str.matches(s)) return 0;
if (str.equals("") || str.length() == 0)
return 0;
char[] a = str.toCharArray();
int fuhao = 0;
if (a[0] == '-')
fuhao = 1;
int sum = 0;
for (int i = fuhao; i < a.length; i++)
{
if (a[i] == '+')
continue;
if (a[i] < 48 || a[i] > 57)
return 0;
sum = sum * 10 + a[i] - 48;
}
return fuhao == 0 ? sum : sum * -1;
}
}
49.在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
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) {
for(int i =0;i<length;i++){
int index = numbers[i];
if(index>=length){
index -=length;
}
if(numbers[index]>=length) {
duplication[0] = index;
return true;
}
numbers[index]=numbers[index]+length;
}
return false;
}
}
**50.给定一个数组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]。不能使用除法。
public class Solution {
public int[] multiply(int[] A) {
int length = A.length;
int[] B = new int[length];
if(length != 0 ){
B[0] = 1;
//计算下三角连乘
for(int i = 1; i < length; i++){
B[i] = B[i-1] * A[i-1];
}
int temp = 1;
//计算上三角
for(int j = length-2; j >= 0; j--){
temp *= A[j+1];
B[j] *= temp;
}
}
return B;
}
}