Tag Archives: algorithms

reverse word in a string

question Given an input string, reverse the string word by word. For example, Given s = “the sky is blue”, return “blue is sky the”. click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters constitutes … Continue reading

Posted in programming | Tagged | Leave a comment

check if a sequence is a output sequence of a stack or not

assume you have a stack, you have a sequence A[n] : {1,2,3,4,5}, check if sequence B[n] : {5,4,3,2,1} is an output of stack or not.   A : 1 2 3 4 5                  B : 5 4 3 2 1 … Continue reading

Posted in programming | Tagged | Leave a comment

the core algorithm of k-way merge

the code below is excerpt from an algorithm blog. This code snippet is the core algorithm of k-way merge, 1: while(true) 2: { 3: //求data中可用的最小的数字,并记录对应文件的索引 4: int min = data[0]; 5: // j 是文件索引 6: int j = 0; 7: … Continue reading

Posted in programming | Tagged | Leave a comment

A very concise solution for leetcode word ladder issue

here is the code, pretty concise, using bfs :   class Solution { public: int ladderLength(string start, string end, unordered_set<string> &dict) { queue<pair<string, int>> q; q.push(make_pair(start, 1)); while (!q.empty()) { pair<string, int> front = q.front(); q.pop(); string word = front.first; … Continue reading

Posted in programming | Tagged | Leave a comment

3 ways to generate subset

There are several ways to generate a subset, assume you want to generate the subset of (1——n ) : 1. directly put the element to the set code : void print_subset1( int n, int *A, int cur ) { for( … Continue reading

Posted in programming | Tagged | Leave a comment

3 ways to enumerate the subset

There are several ways to generate a subset, assume you want to generate the subset of (1——n ) : 1. directly put the element to the set code : void print_subset1( int n, int *A, int cur ) { for( … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

uva 297 wonderful code for build a quad tree

the problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=233 #include #include #include using namespace std ; const int maxn = 2048 + 10 ; struct Node { Node * a ; Node * b ; Node * c ; Node * d ; char data … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

how to get sum(n) – 2

In my previous post ( link is  here ), I post a very interesting question: question: get the sum from 1 to n (n is integer and n >=1). requirements : can not use * (mul) , / (div), bit … Continue reading

Posted in programming | Tagged | Leave a comment

Binary Search Tree and Double-linked List

Question: Convert a binary search tree to a sorted double-linked list. We can only change the target of pointers, but cannot create any new nodes.For example, if we input a binary search tree as shown on the left side of … Continue reading

Posted in programming | Tagged | 1 Comment

How to get sum(n)

In the forum, I see a very interest post question: get the sum from 1 to n (n is integer and n >=1). requirements : can not use *, /, bit operator, if-else, while, for, switch, ? , ternary operator, … Continue reading

Posted in programming | Tagged | 1 Comment