1.支持动态扩容
2.支持动态增删
public class MyArray {
int Size=0;
int capacity=0;
int[] p=null;
public MyArray(int len)
{
p=new int[len];
capacity=len;
}
public void add(int index,int num)
{
if(index<0||index>Size)
return ;
if(Size>=capacity)
resize(p);
p[index]=num;
Size++;
}
public void update(int index,int num)
{
if(index<0||index>Size)
return ;
p[index]=num;
}
public void resize(int[] p){
int[]temp=new int[capacity];
for(int i=0;i<capacity;i++){
temp[i]=p[i];
}
capacity=2*capacity;
p=new int[capacity];
for(int i=0;i<capacity/2;i++){
p[i]=temp[i];
}
}
public void delete(int index,int num)
{
if(index<0||index>Size)
return ;
for(int i=index+1;i<capacity;i++){
p[i]=p[i+1];
}
Size--;
}
public static void main(String[] args){
MyArray ma=new MyArray(3);
ma.add(0,0);
ma.add(1,1);
ma.add(2,2);
ma.add(3,3);
}
}
3.实现两个有序数组合并为一个有序数组
public class MergeArray {
public static void main(String[] args){
int[] arr1={1,3,6,8};
int[] arr2={2,4,9};
mergeArray(arr1,arr2);
}
public static void mergeArray(int[] arr1,int[] arr2){
int[] arr=new int[arr1.length+arr2.length];
int l1=0;int l2=0;
for(int i=0;i<arr.length;i++){
if(l1<arr1.length&&l2<arr2.length) {
if (arr1[l1] < arr2[l2]) {
arr[i] = arr1[l1];
l1++;
} else {
arr[i] = arr2[l2];
l2++;
}
}else if(l1==arr1.length){
arr[i]=arr2[l2];
l2++;
}else if(l2==arr2.length){
arr[i]=arr1[l2];
l1++;
}
}
}
}
LeetCode 1 TwoSum
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89087936
LeetCode 202 Happy Number
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89189586
3Sum也顺便写了一下。3Sum这个题有点东西的。
LeetCode 15 3Sum
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89113574
1.Trie树
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Trie{
private Vertex root;//一个Trie树有一个根节点
//内部类
protected class Vertex{//节点类
protected int words;
protected int prefixes;
protected Vertex[] edges;//每个节点包含26个子节点(类型为自身)
Vertex() {
words = 0;
prefixes = 0;
edges = new Vertex[26];
for (int i = 0; i < edges.length; i++) {
edges[i] = null;
}
}
}
public Trie () {
root = new Vertex();
}
/** *//**
* List all words in the Trie.
*
* @return
*/
public List< String> listAllWords() {
List< String> words = new ArrayList< String>();
Vertex[] edges = root.edges;
for (int i = 0; i < edges.length; i++) {
if (edges[i] != null) {
String word = "" + (char)('a' + i);
depthFirstSearchWords(words, edges[i], word);
}
}
return words;
}
/** *//**
* Depth First Search words in the Trie and add them to the List.
*
* @param words
* @param vertex
* @param wordSegment
*/
private void depthFirstSearchWords(List words, Vertex vertex, String wordSegment) {
Vertex[] edges = vertex.edges;
boolean hasChildren = false;
for (int i = 0; i < edges.length; i++) {
if (edges[i] != null) {
hasChildren = true;
String newWord = wordSegment + (char)('a' + i);
depthFirstSearchWords(words, edges[i], newWord);
}
}
if (!hasChildren) {
words.add(wordSegment);
}
}
public int countPrefixes(String prefix) {
return countPrefixes(root, prefix);
}
private int countPrefixes(Vertex vertex, String prefixSegment) {
if (prefixSegment.length() == 0) { //reach the last character of the word
return vertex.prefixes;
}
char c = prefixSegment.charAt(0);
int index = c - 'a';
if (vertex.edges[index] == null) { // the word does NOT exist
return 0;
} else {
return countPrefixes(vertex.edges[index], prefixSegment.substring(1));
}
}
public int countWords(String word) {
return countWords(root, word);
}
private int countWords(Vertex vertex, String wordSegment) {
if (wordSegment.length() == 0) { //reach the last character of the word
return vertex.words;
}
char c = wordSegment.charAt(0);
int index = c - 'a';
if (vertex.edges[index] == null) { // the word does NOT exist
return 0;
} else {
return countWords(vertex.edges[index], wordSegment.substring(1));
}
}
/** *//**
* Add a word to the Trie.
*
* @param word The word to be added.
*/
public void addWord(String word) {
addWord(root, word);
}
/** *//**
* Add the word from the specified vertex.
* @param vertex The specified vertex.
* @param word The word to be added.
*/
private void addWord(Vertex vertex, String word) {
if (word.length() == 0) { //if all characters of the word has been added
vertex.words ++;
} else {
vertex.prefixes ++;
char c = word.charAt(0);
c = Character.toLowerCase(c);
int index = c - 'a';
if (vertex.edges[index] == null) { //if the edge does NOT exist
vertex.edges[index] = new Vertex();
}
addWord(vertex.edges[index], word.substring(1)); //go the the next character
}
}
public static void main(String args[]) //Just used for test
{
Trie trie = new Trie();
trie.addWord("China");
trie.addWord("China");
trie.addWord("China");
trie.addWord("crawl");
trie.addWord("crime");
trie.addWord("ban");
trie.addWord("China");
trie.addWord("english");
trie.addWord("establish");
trie.addWord("eat");
System.out.println(trie.root.prefixes);
System.out.println(trie.root.words);
List< String> list = trie.listAllWords();
Iterator listiterator = list.listIterator();
while(listiterator.hasNext())
{
String s = (String)listiterator.next();
System.out.println(s);
}
int count = trie.countPrefixes("ch");
int count1=trie.countWords("china");
System.out.println("the count of c prefixes:"+count);
System.out.println("the count of china countWords:"+count1);
}
}
2.朴素的字符串匹配算法复杂度太高,不想写了,直接学着写了一套KMP算法。
KMP里面重点是next数组的创建。即前缀和后缀的公有长度。
LeetCode 28 Implement strStr()
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89214193
字符串这一块,顺便把manacher算法也写了一下,可以解决最长回文子串问题,感觉蛮经典的。
最长回文子串这个经典题,也可以用常规的中心检测法。
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89153260
切分字符串为回文子串的所有结果,可以用DFS.
LeetCode 131 Palindrome Partitioning
https://blog.csdn.net/susuxuezhang/article/details/89180500
切分字符串为回文子串的最少次数,可以用DP.
LeetCode 132 Palindrome Partitioning II
本人链接:https://blog.csdn.net/susuxuezhang/article/details/89182583