String字串反轉的兩種方法 藉助byte[]
阿新 • • 發佈:2018-12-18
package com.interview.datastructure; import java.util.Arrays; public class TwoStringAsSameChar { /* 方法1.先將兩個String 型別的,轉換為byte字元陣列, 排序後再次轉為String 再使用equals方法比較兩個物件的內容是不是一樣; 方法1.中 的陣列排序可以使用空間換時間的方法來減多少時間 方法2 使用所有的單個字元的ASCII範圍都是在0-266 之間,使用空間換時間的方法,新建陣列,遍歷String 轉化的byte(i)(byte值作為 266陣列中的索引;有++ ,另外一個數組有-- ;最後遍歷陣列,存在不為0的則兩個String不一樣; */ public static void main(String[] args) { String s1 = new String("aabbccff"); String s2 = new String("bbccffaa"); byte[] b1 = s1.getBytes(); Arrays.sort(b1); String s3 = new String(b1); System.out.println(s3); for (byte b : b1) { System.out.println(b); } }}