java:字符串

来源:互联网 发布:中国网络诗歌网 编辑:程序博客网 时间:2024/04/29 22:30

字符串基本类

  • 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
原创粉丝点击