w******j 发帖数: 185 | 1 来个java的吧,带parent pointer和不带的,preorder和inorder的
import java.util.NoSuchElementException;
import java.util.Stack;
public class BST {
private Node root;
private static class Node {
int key;
Node left, right, parent;
Node(int key) {
this.key = key;
}
}
public BST(){
}
public BSTIterator inorderIterator() {
return new InorderIterator();
}
public BSTIterator preorderIterator() {
return new PreorderI... 阅读全帖 |
|
w******j 发帖数: 185 | 2 来个java的吧,带parent pointer和不带的,preorder和inorder的
import java.util.NoSuchElementException;
import java.util.Stack;
public class BST {
private Node root;
private static class Node {
int key;
Node left, right, parent;
Node(int key) {
this.key = key;
}
}
public BST(){
}
public BSTIterator inorderIterator() {
return new InorderIterator();
}
public BSTIterator preorderIterator() {
return new PreorderI... 阅读全帖 |
|
发帖数: 1 | 3 R里面一般不这样定义class, 用s3和s4写了个prototype,也可以用environemnt实现
# s3 class
node_s3 <- function(value, nextNode){
UseMethod('node_s3',value)
}
node_s3.numeric <- function(value,nextNode){
if ((is.null(nextNode) || class(nextNode)=="node_s3")){
res <- list(value=value,nextNode=nextNode)
class(res) <- 'node_s3'}else stop("nextNode wrong type")
res
}
left_s3=node_s3(3,NULL)
root_s3=node_s3(2,left_s3)
root_s3$nextNode
# s4 class
setClass("node_s4", slots = list(value = "numeric", nextNode = "ANY"))
... 阅读全帖 |
|
w****r 发帖数: 15252 | 4 /*
* Sort a linked list in O(n log n) time using constant space
complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
//get the length of the liklist
int count = 0;
ListNode node = head;
while(node!=null){
count++;
node = node.next;
}
//break up to two list
int middle = count / 2;
... 阅读全帖 |
|
j********2 发帖数: 82 | 5 照你的建议写了一个,没测过
void restore(AugListNode *head, unordered_map
&ht)
{
if (!head) return;
AugListNode *curNode, *nextNode;
curNode = head; nextNode = curNode->next;
while (1)
{
if (!curNode) break;
if (ht.find(nextNode) != ht.end()) // a child list found
{
// separate the child list
curNode->child = nextNode;
AugListNode *nextNodeAfterChildList = ht[nextNode]->next;
curNode->... 阅读全帖 |
|
m***p 发帖数: 86 | 6 single linked list版本:
void reverse(Node head){
Node cur = head;
Node prev = null;
while(cur != null){
Node nextNode = cur.next;
cur.next = prev;
prev = cur;
cur = nextNode;
}
return prev;
}
double linked list版本:
void reverse(Node head){
Node cur = head;
Node prev = null;
while(cur != null){
Node nextNode = cur.next;
cur.next = prev;
cur.prev = nextNode;
prev = cur;
cur = nextNode;
}
re... 阅读全帖 |
|
f*********i 发帖数: 197 | 7 public static LinkList deleteLastIndex(LinkList root)
{
if (root == null || root.next == null)
return null;
LinkList returnRoot = root, nextNode = root.next;
while (nextNode.next != null)
{
nextNode = nextNode.next;
root = root.next;
}
root.next = null;
return returnRoot;
} |
|
s*******s 发帖数: 1031 | 8 follow一下我的面经。
http://www.mitbbs.com/article_t/JobHunting/32517841.html
整理了我的几个解答的算法,分享一下。欢迎批评指正。
多谢!
1. 写一个程序,找出 5^1234566789893943的从底位开始的1000位数字。
我用的递归+数组大数乘法。
// Caclulate (m^n)%(10^k). Keep the k integer numbers in an array.
// Note: the integer numbers are in reversed in the array
// Assume: m>0, n>0, k>0
// Need to check validity outside of this function.
// call calculate(5, 1234566789893943, 1000) to get result.
// Time complexity: O((log n) * k * k)
// Space complexity: O((log n) * k)
ve... 阅读全帖 |
|
s*******s 发帖数: 1031 | 9 follow一下我的面经。
http://www.mitbbs.com/article_t/JobHunting/32517841.html
整理了我的几个解答的算法,分享一下。欢迎批评指正。
多谢!
1. 写一个程序,找出 5^1234566789893943的从底位开始的1000位数字。
我用的递归+数组大数乘法。
// Caclulate (m^n)%(10^k). Keep the k integer numbers in an array.
// Note: the integer numbers are in reversed in the array
// Assume: m>0, n>0, k>0
// Need to check validity outside of this function.
// call calculate(5, 1234566789893943, 1000) to get result.
// Time complexity: O((log n) * k * k)
// Space complexity: O((log n) * k)
ve... 阅读全帖 |
|
b******i 发帖数: 914 | 10 贴个code,不过只test了几种case
/*
给一个list of string 和一个string s, 返回s是不是在list当中, s里可以包含一
个*代表一个任意字符。
*/
#include
#include
#include
using namespace std;
struct TrieNode {
TrieNode():is_word(false),children(26,nullptr) {}
bool is_word;
vector children;
};
class Solution {
public:
bool string_in_list(vector strings, string s) {
if(s.length() == 0 || strings.size() == 0)
return false;
// construct trie
... 阅读全帖 |
|
x**8 发帖数: 1939 | 11 我试图这么写,但是不对,试了好多,都不知道该怎样,
setClass("node", representation(value='numeric', nextNode='node'), prototype
(value=NA_real_, nextNode=NA))
请大侠指点, |
|
x**8 发帖数: 1939 | 12 我试图这么写,但是不对,试了好多,都不知道该怎样,
setClass("node", representation(value='numeric', nextNode='node'), prototype
(value=NA_real_, nextNode=NA))
请大侠指点, |
|
x**8 发帖数: 1939 | 13 我试图这么写,但是不对,试了好多,都不知道该怎样,
setClass("node", representation(value='numeric', nextNode='node'), prototype
(value=NA_real_, nextNode=NA))
请大侠指点, |
|
m******9 发帖数: 968 | 14 删除以后,需要重新把list连接起来。 所以需要处理preNode, currentNode,
nextNode.
不过楼上给的答案好帮,怪我以前没复习到位,没想到copy的方法, |
|
c******w 发帖数: 102 | 15 Node* nextNode(Node* root, int data)
{
Node* result = NULL;
while(root)
{
if( root->value > data )
{
result = root;
root = root -> left;
}
else
root = root -> right;
}
return result;
}
inorder |
|
s******n 发帖数: 3946 | 16 inorder网上有,另外两个找不到现成答案,帮看看对不对?
Node* preOrderNext(Node* n)
{
if (n->left) return n->left;
if (n->right) return n->right;
Node* p = n->parent;
while (p && p->right==n) {
n = p;
p = n->parent;
}
if (!p) return p;
if (p->right) return p->right;
return p;
}
Node* left_most(Node* n) {
assert(n);
while(n->left) {
n=n->left;
}
return n;
}
Node* postOrderNext(Node* n)
{
Node* p = n->parent;
if (!p) reutrn p;
if (n==p->right) return p;
if (p->right) return left_most(p->right);
return p... 阅读全帖 |
|
z*y 发帖数: 1311 | 17 帮你改了改
Node* preOrderNext(Node* n)
{
if (n->left) return n->left;
if (n->right) return n->right;
Node* p = n->parent;
while (p && (p->right == NULL || p->right == n))
{
n = p;
p = n->parent;
}
if (!p)
return p;
return p->right;
}
给个包子吧,好吗? |
|
|