劍指offer(Java)

目錄

一、數組

1.二維數組的查找

2.數組中重複的數字

3.構建乘積數組

二.字符串

1.替換空格

2.正則表達式匹配

3.字符流中第一個不重複的字符

4.表示數值的字符串


一、數組

1.二維數組的查找

public class Solution {
    public boolean Find(int target, int [][] array) {
        int rows= array.length;
        int cols= array[0].length;
        int i =rows -1;
        int j = 0;
        while( i>=0 && j< cols){
            if (target > array[i][j]){
                j++;
            }
            else if (target< array[i][j]){
                 i--;
           }
            else
                return true;
    }
    return false;
    }
}

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) {
        if(length==0) return false;
        int[] count=new int[length];
        for(int i=0;i<length;i++){
            count[numbers[i]]++;
            if(count[numbers[i]]==2) {
                duplication[0]=numbers[i];
                return true;
            }
        }
        return false;
    }
}

3.構建乘積數組

import java.util.ArrayList;
public class Solution {
    public int[] multiply(int[] A) {
        int l = A.length;
        int inst = 1;
        int[] left = new int[l];
        int[] right = new int[l];
        int[] result = new int[l];
        left[0] = 1;
        for(int i = 1; i < l; i++){
            left[i] = left[i-1] * A[i-1];
        }
        right[l-1] = 1;
        for(int i = l-2; i >= 0; i--){
            right[i] = right[i+1] * A[i+1];
        }
        for(int i = 0; i < l; i++){
            result[i] = left[i] * right[i];
        }
        return result;
    }
}

二.字符串

1.替換空格

public class Solution {
    public String replaceSpace(StringBuffer str) {
        // 邊界條件
        if (str == null || str.equals("")){
            return null;
        }
        StringBuilder newStr = new StringBuilder();
        for (int i=0;i<str.length();i++){
            if (str.charAt(i) == ' '){
                newStr.append('%');
                newStr.append('2');
                newStr.append('0');
            }else {
                newStr.append(str.charAt(i));
            }
        }
         
        return newStr.toString();
    }
}

2.正則表達式匹配

import java.util.Arrays;

public class 字符串_正則表達式匹配 {
    public static void main(String[] args){
        char[] st1 = {'a','b','c'};
        char[] st2 = {'a','b','.'};
        System.out.println(match(st1, st2));
    }

    public static boolean match(char[] str, char[] pattern)
    {
        if(str.length == 0 && pattern.length == 0){
            return true;
        }
        if(str.length > 0 && pattern.length == 0){
            return false;
        }
        if(pattern.length > 1 && pattern[1] == '*'){
            if (str.length > 0 && (str[0] == pattern[0] || pattern[0] == '.')){
                return match(str, Arrays.copyOfRange(pattern, 2, pattern.length))
                        || match(Arrays.copyOfRange(str, 1, str.length), Arrays.copyOfRange(pattern, 2, pattern.length))
                        || match(Arrays.copyOfRange(str, 1, str.length), pattern);
            }
            else{
                return match(str, Arrays.copyOfRange(pattern, 2, pattern.length));
            }
        }
        else{
            if(str.length > 0 && (str[0] == pattern[0] || pattern[0] == '.')){
                return match(Arrays.copyOfRange(str, 1, str.length), Arrays.copyOfRange(pattern, 1, pattern.length));
            }
            else{
                return false;
            }
        }
    }
}

3.字符流中第一個不重複的字符

import java.util.*;

public class 字符串_字符流中第一個不重複的字符 {

    private HashMap<Character, Integer> map=new HashMap();
    private ArrayList<Character> list=new ArrayList<Character>();

    //Insert one char from stringstream
    public void Insert(char ch)
    {
        if(map.containsKey(ch)){
            map.put(ch,map.get(ch)+1);
        }else{
            map.put(ch,1);
        }

        list.add(ch);
    }

    //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {   char c='#';
        for(char key : list){
            if(map.get(key)==1){
                c=key;
                break;
            }
        }

        return c;
    }
}

4.表示數值的字符串


import java.util.Arrays;

public class 字符串_表示數值的字符串 {
    public static void main(String[] args){
        char[] a = {'1','2','3','.','4','5','e','+','6'};
        boolean bool =isNumeric(a);
        System.out.println(bool);

    }
    public static boolean isNumeric(char[] str){
        if(str.length <= 0){
            return false;
        }
        for(int i = 0; i < str.length; i++){
            if(str[i] == 'E'){
                str[i] = 'e';
            }
        }
        if(contain(str,'e')){
            int index = getindex(str, 'e');
            char[] left = Arrays.copyOfRange(str, 0, index);
            char[] right = Arrays.copyOfRange(str,index + 1,  str.length);
            if(contain(right, '.') || right.length == 0){
                return false;
            }
            return isDigit(left) && isDigit(right);
        } else{
            return isDigit(str);
        }
    }
    public static boolean isDigit(char[] str1){
        char[] bz = {'0','1','2','3','4','5','6','7','8','9','+','-','.'};
        char[] fh = {'+','-'};
        int count = 0;
        for(int i = 0; i < str1.length; i++) {
            if (contain(bz, str1[i])){
                if (str1[i] == '.') {
                    count += 1;
                }
                if(contain(fh, str1[i]) && i != 0){
                    return false;
                }
            } else{
                return false;
            }
        }
        if(count > 1) {
            return false;
        }
        return true;
    }
    public static boolean contain(char[] a, char b){
        for(int i = 0; i < a.length; i++){
            if(a[i] == b){
                return true;
            }
        }
        return false;
    }
    public static int getindex(char[] a, char b){
        for(int i = 0; i < a.length; i++){
            if(a[i] == b){
                return i;
            }
        }
        return -1;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章