数组
public class MyArray {
public static Object[ ] addArray ( Object[ ] oldArr, int size) {
Object[ ] newArr = new Object [ size] ;
if ( size > oldArr. length) {
for ( int i = 0 ; i < oldArr. length; i++ ) {
newArr[ i] = oldArr[ i] ;
}
return newArr;
} else if ( size == oldArr. length) {
return oldArr;
} else {
for ( int i = 0 ; i < size; i++ ) {
newArr[ i] = oldArr[ i] ;
}
return newArr;
}
}
public static void main ( String[ ] args) {
Integer[ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ;
System. out. println ( arr. length) ;
Object[ ] obj = MyArray. addArray ( arr, 20 ) ;
System. out. println ( obj. length) ;
}
}
遍历数组 增删移位 扩容同上
public class MyArray {
public static int [ ] merge2array ( int [ ] arr1, int [ ] arr2) {
int [ ] newArr = new int [ arr1. length + arr2. length] ;
int arr1_index = 0 ;
int arr2_index = 0 ;
for ( int i = 0 ; i < newArr. length; i++ ) {
if ( arr1. length >= arr2_index) {
if ( ( arr1_index < arr1. length) && ( arr1[ arr1_index] <= arr2[ arr2_index] ) ) {
newArr[ i] = arr1[ arr1_index] ;
arr1_index++ ;
} else {
newArr[ i] = arr2[ arr2_index] ;
arr2_index++ ;
}
} else {
if ( ( arr2_index < arr2. length) && ( arr2[ arr2_index] <= arr1[ arr1_index] ) ) {
newArr[ i] = arr2[ arr2_index] ;
arr2_index++ ;
} else {
newArr[ i] = arr1[ arr1_index] ;
arr1_index++ ;
}
}
}
return newArr;
}
public static void main ( String[ ] args) {
int [ ] arr1 = { 1 , 3 , 7 , 8 , 10 } ;
int [ ] arr2 = { 2 , 5 , 6 , 8 , 11 , 12 } ;
int [ ] res = MyArray. merge2array ( arr1, arr2) ;
for ( int i = 0 ; i < res. length; i++ ) {
System. out. println ( res[ i] ) ;
}
}
}
学习哈希表思想,并完成leetcode上的两数之和(1)及Happy Number(202)!(要求全部用哈希思想实现!)
public class Solution {
public static void main ( String[ ] args) {
int [ ] test = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ;
int target = 15 ;
int [ ] result = twoSum2 ( test, target) ;
for ( int i = 0 ; i < result. length; i++ ) {
System. out. print ( result[ i] + " " ) ;
}
}
public static int [ ] twoSum1 ( int [ ] array, int target) {
HashMap< Integer, Integer> hashMap = new HashMap < > ( ) ;
for ( int i = 0 ; i < array. length; i++ ) {
hashMap. put ( array[ i] , i) ;
}
for ( int i = 0 ; i < array. length; i++ ) {
int need = target - array[ i] ;
if ( hashMap. containsKey ( need) && hashMap. get ( need) != i) {
return new int [ ] { i, hashMap. get ( need) } ;
}
}
throw new IllegalArgumentException ( "no solution" ) ;
}
}
字符串
实现一个字符集,只包含 a~z 这 26 个英文字母的 Trie 树
public class TrieNode {
private char ch;
private TrieNode[ ] childNodes = new TrieNode [ 26 ] ;
public char getCh ( ) {
return ch;
}
public void setCh ( char ch) {
this . ch = ch;
}
public TrieNode[ ] getChildNodes ( ) {
return childNodes;
}
public void setChildNodes ( TrieNode[ ] childNodes) {
this . childNodes = childNodes;
}
@Override
public String toString ( ) {
return "TrieNode{" +
"ch=" + ch +
'}' ;
}
public static void main ( String[ ] args) {
TrieNode root = new TrieNode ( ) ;
TrieNode[ ] child = root. getChildNodes ( ) ;
for ( int i = 0 ; i < 26 ; i++ ) {
child[ i] = new TrieNode ( ) ;
child[ i] . setCh ( ( char ) ( 'a' + i) ) ;
System. out. println ( child[ i] ) ;
}
}
}
public class MyString {
public static int [ ] matcher ( String str, String pattern) {
char [ ] ch = str. toCharArray ( ) ;
char [ ] re = pattern. toCharArray ( ) ;
int [ ] res = new int [ ch. length] ;
int j = 0 ;
for ( int i = 0 ; i < ch. length - re. length + 1 ; i++ ) {
if ( ch[ i] == re[ 0 ] ) {
for ( j = 1 ; j < re. length; j++ ) {
if ( ch[ i + j] != re[ j] ) {
break ;
}
}
if ( j == re. length) {
res[ i] = i + 1 ;
}
}
}
return res;
}
public static void main ( String[ ] args) {
int [ ] res = matcher ( "abababab" , "ab" ) ;
for ( int i = 0 ; i < res. length; i++ ) {
System. out. println ( res[ i] ) ;
}
}
}