java:字符串


字符串基本类

  • String, StringBuffer,StringBuilder
    1. String是不可变类
    2. StringBuffer是可变,通过方法:append+insert+reverse+setCharAt+setLength

正则表达式


字符串的排序

  • LSD(低位优先检查字符串)从右到左
  • MSD(高位优先检查字符串)从左到右

索引计数法

  • 适用于小整数

lsd

public class LSD {
    private static final int BITS_PER_BYTE = 8;

    // do not instantiate
    private LSD() { }

   /**  
     * Rearranges the array of W-character strings in ascending order.
     *
     * @param a the array to be sorted
     * @param W the number of characters per string
     */
    public static void sort(String[] a, int W) {
        int N = a.length;
        int R = 256;   // extend ASCII alphabet size
        String[] aux = new String[N];

        for (int d = W-1; d >= 0; d--) {
            // sort by key-indexed counting on dth character

            // compute frequency counts
            int[] count = new int[R+1];
            for (int i = 0; i < N; i++)
                count[a[i].charAt(d) + 1]++;

            // compute cumulates
            for (int r = 0; r < R; r++)
                count[r+1] += count[r];

            // move data
            for (int i = 0; i < N; i++)
                aux[count[a[i].charAt(d)]++] = a[i];

            // copy back
            for (int i = 0; i < N; i++)
                a[i] = aux[i];
        }
    }

msd

import java.util.*;

 /*
  * 1.频数统计
  * 2.将频数转换为索引
  * 3.数据分类
  * 4.回写
  * 
  * 
  * MSD算法       
  * */

class Solution{
    private static final int BITS_PER_BYTE=8;
    private static final int BITS_PER_INT=32;
    private static final int R=256;
    private static final int CUTOFF=15; //插入排序的切换阈值

    private Solution(){}

    public static void sort(String[] a){
        int N=a.length;
        String[] aux=new String[N];
        sort(a,0,N-1,0,aux);

    }

    // dth character of s, -1 if d=length of string
    private static int charAt(String s,int d){
        assert d>=0 &&d<=s.length();
        if(d==s.length()) return -1;
        return s.charAt(d); //返回第d个字符

    }

    // 以第d个字符为键将a[lo]到a[hi]排序
    private static void sort(String[] a,int lo,int hi,int d,String[] aux){
        // 分割成子串排序
        if(hi<=lo+CUTOFF){
            insertion(a,lo,hi,d);
            return;
        }


    //计算频数
    int[] count=new int[R+2];

    for(int i=lo; i<=hi;i++){
        int c=charAt(a[i],d);
        count[c+2]++;
    }

    //转换为索引
    for(int r=0;r<R+1;r++)
        count[r+1]+=count[r];

    //数据分类
    for(int i=lo;i<=hi;i++){
        int c=charAt(a[i],d);
        aux[count[c+1]++]=a[i];
    }

    //copy back

    for(int i=lo;i<=hi;i++){
        a[i]=aux[i-lo];
    }

    //recursively sort for each character (excludes sentinel-1)
    for(int r=0;r<R;r++)
        sort(a,lo+count[r],lo+count[r+1]-1,d+1,aux);

    }
    //insertion sort a[lo..hi] starting at dth character
    private static void insertion(String[] a,int lo,int hi,int d){
        for(int i=lo;i<=hi;i++)
            for(int j=i;j>lo && less(a[j],a[j-1],d);j--){
                exch(a,j,j-1);

            }
    }
}

字符串的查找

暴力算法

  • 算法
    复杂度:O(m*n)
import java.util.*;

/*
 * i:跟踪文本
 * j:跟踪 模式
 * */

class Solution{
    public static int search(String pat, String txt){
        int M=pat.length();
        int N=txt.length();
        for(int i=0;i<=N-M;i++){
            int j;
            for( j=0;j<M;j++)
             if(txt.charAt(i+j)!=pat.charAt(j))
                break;
            if(j==M)  return i; // 找到匹配
        }
        return N; //为找到匹配
    }
}

KMP

http://blog.csdn.net/yutianzuijin/article/details/11954939/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值