牛客网做题总结:剑指offer中题目,java版一

1、数组中重复的数字

在一个长度为n的数组里的所有数字都在0到n-1的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。

 

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 arrayis 1,so using duplication[0] = ? in implementation;

    //    Hereduplication like pointor in C/C++, duplication[0] equal *duplication in C/C++

    //   这里要特别注意~返回任意重复的一个,赋值duplication[0]

    //       Returnvalue:       true if the input is valid, andthere are some duplications in the array number

//                    otherwise false

 

    boolean duplicate(int numbers[],int length,int [] duplication) {

         

        if(length<0) return false;

         

        int[]a = new int[length];

         

        for(int i=0;i<length;i++){

            a[numbers[i]]++;

        }

         

        for(int i=0; i<length;i++) {

             

                if(a[i]>1){

                    duplication[0]=i;

                    return true;

                }

            

        }

        return false;

    }

}

 

2、字符流中第一个不重复的字符

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

 

public class Solution {

    //Insertone char from stringstream

    Stringstr = "";

    int[] a= new int[256];

    void Insert(char ch)

    {

        str+=ch;

        a[(int)ch]++;

         

    }

  //return the firstappearence once char in current stringstream

    char FirstAppearingOnce()

    {

        for(int i=0;i<str.length(); i++) {

            if(a[(int)str.charAt(i)]==1){

                return str.charAt(i);

            }

        }

        return '#';

    }

}

 

3、正则表达式匹配

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

 

import java.util.regex.Pattern;

import java.util.regex.Matcher;

public class Solution {

    boolean match(char[]str, char[] pattern)

    {

        Strings = new String(str);

        Stringpa = new String(pattern);

        Patternp = Pattern.compile(pa);

        Matcherm  = p.matcher(s);

        return m.matches();

    }

}

4、构建成绩数组

给定一个数组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]。不能使用除法。

 

import java.util.ArrayList;

public class Solution {

    int[] multiply(int[]A) {

         

          int length = A.length;

        int[]B = new int[length];

        if(A== null || length <= 0)

            return null;

        B[0]= 1;

        for(int i= 1;i<length;i ++){

            B[i]= B[i-1] * A[i-1];

        }

        int temp = 1;

        for(int i = length- 2;i>= 0;i --){

            temp*= A[i+1];

            B[i]*= temp;

        }

          

        return B;

         

 

    }

}

5、数组中只出现一次的数字

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

 

//num1,num2分别为长度为1的数组。传出参数

//将num1[0],num2[0]设置为返回结果

import java.util.*;

public class Solution {

    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {

         

        if(array.length<=1)  return;

        ArrayList<Integer>l = new ArrayList<Integer>();

        for(int i=0;i<array.length; i++) {

                if(l.contains(array[i])){

                    l.remove(l.indexOf(array[i]));

                } else{

                    l.add(array[i]);

                }

        }

         

        num1[0]=l.get(0);

        num2[0]=l.get(1);

    }

}

6、数字在排序数组中出现的次数

统计一个数字在排序数组中出现的次数。

 

public class Solution {

    public int GetNumberOfK(int [] array , int k) {

         

        int count = 0;

        for(int i=0;i<array.length; i++) {

            if(k==array[i])count++;

        }

       return count;

    }

}

 

 

7、数组中的逆序对

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。

 

public class Solution {

    public int InversePairs(int [] array) {

         

        if(array.length<2) return 0;

        int count = 0;

        for(int i=0;i<array.length; i++) {

            for(int j=i+1;j<array.length; j++ ) {

                if(array[i]>array[j])count++;

                 

            }

        }

        return count;

    }

}

 

8、第一个只出现一次的字符

在一个字符串(1<=字符串长度<=10000)中找到第一个只出现一次的字符。

返回:

1.字符在字符串的位置

2.当字符串长度为0时,返回-1

import java.util.*;

public class Solution {

    public int FirstNotRepeatingChar(Stringstr) {

         

        Map<Character,Integer>m=new HashMap<Character,Integer>();

        if(str.length()==0)

            return -1;

        //第一次扫描

        for(int i=0;i<str.length();i++)

        {

            //得到出现次数

            Integerfreq=m.get(str.charAt(i));

              

            m.put(str.charAt(i),freq==null?1:freq+1);

                  

        }

        //第二次扫描

        for(int i=0;i<str.length();i++)

        {

            if(m.get(str.charAt(i))==1)

            {

                return i;

            }

        }

        return -1;

    

    }

}

 

9、把数组排成最小的数

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

 

import java.util.*;

 

public class Solution {

    public StringPrintMinNumber(int [] numbers) {

         

          if(numbers==null||numbers.length<=0)

        return "";

        String[]ss=new String[numbers.length];

        for(int i=0;i<numbers.length;i++){

            ss[i]=numbers[i]+"";

        }

        Arrays.sort(ss,new Comparator<String>(){

            public int compare(Stringo1,String o2){

                return(o1+o2).compareTo(o2+o1);

            }

        });

        StringBuildersb=new StringBuilder();

        for(Stringeach:ss){

            sb.append(each);

               

        }

        return sb.toString();

    }

}

 

10、整数1出现的次数(从1到n整数中1出现的次数)

求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数

 

public class Solution {

    public int NumberOf1Between1AndN_Solution(int n) {

         

       int x=1;

        int count=0,k;

        for(int i=1;n/i>0;i*=10)

        {

            k=n/i;

            //k/10为高位数字

            count+=(k/10)*i;

            int cur=k%10;

            if(cur>x)

            {

                count+=i;

                  

            }

            else if(cur==x)

            {

                //n-k*i为低位数字

                count+=n-k*i+1;

            }

        }

        return count;

         

         

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值