剑指offer 31到35题 ( JAVA )

第三十一题:

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

思路:
  根据当前位置为个位1 十位10 百位100等 来计算
  有如下规则,如果当前位为0,则1的个数仅有高位来计算=高位*当前位
  如果当前位为1,则1的个数=高位*当前位+低位+1

代码:

public class Solution {
    public int NumberOf1Between1AndN_Solution(int n) {
        //i=1开始计算,每次前移一位*10 直到n/i=0停止
        int current = 0;
        int before = 0;
        int after = 0;
        int count = 0;
        int i=1;
        while(n/i != 0){
            current = (n/i)%10; //计算当前位
            before = n/(i*10); //高位
            after = n - (n/i)*i; //低位
            if(current==0)
                count += before*i;
            else if(current==1)
                count = count + before*i + after + 1;
            else if(current > 1)
                count = count + (before+1)*i;
            i=i*10;
        }        
        return count;
    }
}

 

第三十一题:

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

思路:重写compare

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
    public String PrintMinNumber(int [] numbers) {
      int n;
      String s="";
      ArrayList<Integer> list= new ArrayList<Integer>();
      n=numbers.length;
      for(int i=0;i<n;i++){
       list.add(numbers[i]);   
      }
      Collections.sort(list, new Comparator<Integer>(){
    public int compare(Integer str1,Integer str2){
      String s1=str1+""+str2;
      String s2=str2+""+str1;
         return s1.compareTo(s2);
     }//重写compare方法
  });
      for(int j:list){
       s+=j;
      }
        return s;
    }
}

-------------------------------------------------------------------可爱的分界线------------------------------------------------------------------------------------

第三十二题:

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

思路:

P=2^x+3^y+5^z

基数为:1

开始插入2,3,5

每次插入的新数,它的2倍,3倍,5倍同样为丑数

数组index指向进行判断,取最小的插入

代码:

public class Solution {
   public int GetUglyNumber_Solution(int index) {
        if (index<=0) return 0;
        if (index==1) return 1;
        int[] ret = new int[index];
        ret[0]=1;
        int t2=0,t3=0,t5=0;
        for(int i=1;i<index;i++) {
            ret[i] = min(min(ret[t2]*2,ret[t3]*3),ret[t5]*5);
            if(ret[i] == ret[t2]*2) t2++;
            if(ret[i] == ret[t3]*3) t3++;
            if(ret[i] == ret[t5]*5) t5++;
        }
        return ret[index-1];
    }
    public static int min(int a,int b) {
        return a<b ? a : b; 
    }
}

-------------------------------------------------------------------可爱的分界线------------------------------------------------------------------------------------

第三十三题:

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

思路:

主要是由于A-Z对应的ASCII码为65-90,a-z对应的ASCII码值为97-122,而每个字母的index=int(word)-65

用数组存放,相应的值代表出现的次数,遍历一篇字符串,录入数组,然后遍历数组,值为1的时候返回。

代码:

public class Solution {
    public int FirstNotRepeatingChar(String str) {
         int[] words = new int[58];
    for(int i = 0;i<str.length();i++){
        words[((int)str.charAt(i))-65] += 1;
    }
    for(int i=0;i<str.length();i++){
        if(words[((int)str.charAt(i))-65]==1)
            return i;
    }
    return -1;
    }
}

-------------------------------------------------------------------可爱的分界线------------------------------------------------------------------------------------

第三十四题:

在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007

输入描述:

题目保证输入的数组中没有的相同的数字

数据范围:

对于%50的数据,size<=10^4

对于%75的数据,size<=10^5

对于%100的数据,size<=2*10^5

思路:

归并排序的改进,把数据分成前后两个数组(递归分到每个数组仅有一个数据项),
合并数组,合并时,出现前面的数组值array[i]大于后面数组值array[j]时;则前面
数组array[i]~array[mid]都是大于array[j]的,count += mid+1 - i
测试用例输出结果比较大,对每次返回的count mod(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 int InversePairsCore(int[] array,int[] copy,int low,int high)
    {
        if(low==high)
        {
            return 0;
        }
        int mid = (low+high)>>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;
    }
}

-------------------------------------------------------------------可爱的分界线------------------------------------------------------------------------------------

第三十五题:

输入两个链表,找出它们的第一个公共结点。

思路:

利用HashMap的特性

代码:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.HashMap;
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode current1 = pHead1;
        ListNode current2 = pHead2;
        HashMap<ListNode, Integer> hashMap = new HashMap<ListNode, Integer>();
        while (current1 != null) {
            hashMap.put(current1, null);//将链表1的结点装入HashMap
            current1 = current1.next;
        }
        while (current2 != null) {
            if (hashMap.containsKey(current2))//如果包含
                return current2;
            current2 = current2.next;
        }
        return null;
    }
}

-------------------------------------------------------------------可爱的分界线------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值