classTrieNode{ // Initialize your data structure here. boolean isLeaf; TrieNode[] nodes; publicTrieNode(){ isLeaf = false; nodes = new TrieNode[26]; } }
publicclassTrie{ private TrieNode root;
publicTrie(){ root = new TrieNode(); }
// Inserts a word into the trie. publicvoidinsert(String word){ TrieNode node = root; for(int i = 0; i < word.length(); ++i) { int index = word.charAt(i) - 'a'; /***错误的写法,node信息丢失 * node = node.nodes[index]; * if(node == null) * node = new TrieNode(); ***/ if(node.nodes[index] == null) node.nodes[index] = new TrieNode(); node = node.nodes[index]; if(i == word.length() - 1) node.isLeaf = true; } }
// Returns if the word is in the trie. publicbooleansearch(String word){ TrieNode node = root; for(int i = 0; i < word.length(); ++i) { int index = word.charAt(i) - 'a'; node = node.nodes[index]; if(node == null) returnfalse; if(i == word.length() - 1 && node.isLeaf) returntrue; } returnfalse; }
// Returns if there is any word in the trie // that starts with the given prefix. publicbooleanstartsWith(String word){ TrieNode node = root; for(int i = 0; i < word.length(); ++i) { int index = word.charAt(i) - 'a'; node = node.nodes[index]; if(node == null) returnfalse; } returntrue; } }
// Your Trie object will be instantiated and called as such: // Trie trie = new Trie(); // trie.insert("somestring"); // trie.search("key");
Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
For example, Given words = [“oath”,”pea”,”eat”,”rain”] and board =
[ [‘o’,’a’,’a’,’n’], [‘e’,’t’,’a’,’e’], [‘i’,’h’,’k’,’r’], [‘i’,’f’,’l’,’v’] ] Return [“eat”,”oath”]. Note: You may assume that all inputs are consist of lowercase letters a-z. https://leetcode.com/problems/word-search-ii/
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library’s sort function for this problem.
The Java platfrom is designed from ground up to support concurrent. There are two units of excution: Processes and Threads. A system normally has multipul processes and threads, even if there is only single excution core, because processing time can be shared among threads and processes through time clicing.
Giving an array of values, say the ith value is the price of stock of the ith day. If you can perform at most one transaction (ie. Buy one and sell one share of stock), find the maximum profit.
Keep tracing the minimum price before the ith day and calculating the maximum profit during scanning.
1 2 3 4 5 6 7 8 9
publicintmaxProfit(int[] prices){ if(prices == null || prices.length <= 1) return0; int result = 0, min = prices[0]; for(int i = 1; i < prices.length; ++i) { result = Math.max(result, prices[i] - min); min = Math.min(min, prices[i]); } return result; }
Best Time to Buy and Sell stock(Greed)
Giving an array of values, say the ith value is the price of stock of the ith day. If you can perform as many transactions (ie. Buy one and sell one share of stock) as you like, find the maximum profit.
Using greed algorithm, perform the transaction only if there is profit to make between two continuous days.
1 2 3 4 5 6 7 8 9
publicintmaxProfit(int[] prices){ if(prices == null || prices.length <= 1) return0; int result = 0; for(int i = 1; i < prices.length; ++i) { if(prices[i] > prices[i - 1]) result += prices[i] - prices[i - 1]; } return result; }
Best Time to Buy and Sell stock(Devide and Conquer & DP)
Giving an array of values, say the ith value is the price of stock of the ith day. If you can perform at most two transactions (ie. Buy one and sell one share of stock), find the maximum profit.