s******n 发帖数: 226 | 1 练习一下,第一次写的时候居然有bug,郁闷死了,这可怎么办呢~~~
// some corner case and sign process
long prev = 0;
long cur = y;
int k=0;
while(cur
prev = cur;
cur = cur<<1;
k++;
}
if(cur==x)
return (1<
long l = 1<<(k-1);
long r = 1<
while(true){
if(l>=r) return l;
long mid = (r+l)>>1;
long mid_v = (cur+prev)>>1;
if(x == mid_v) return mid;
if(x < mid_v){
r = mid;
... 阅读全帖 |
|
k***t 发帖数: 276 | 2 本人一直只写C,看过一点STL。欢迎大家 Code Review。谢谢。
觉得has_next_level flag 部分不够简单明了,但没有它不好
在输出中换行和终止加dummy node.
#include
using namespace std;
typedef struct TreeNode_ {
int value;
struct TreeNode_ *left;
struct TreeNode_ *right;
} TreeNode;
int level (TreeNode *root) {
queue Q;
TreeNode *cur, dummy;
bool has_next_level;
if (!root) return -1;
Q.push(root); Q.push(&dummy);
has_next_level = false;
while(!Q.empty()) {
cur = Q.front();
... 阅读全帖 |
|
d****o 发帖数: 1055 | 3 写了一个增加一个空头结点的简单代码。
node* reverse(node* head, int begin, int end){
assert(begin
if(head==NULL) return NULL;
node* newHead = new node();
newHead->next = head;
node* cur = newHead;
int cnt = end-begin+1; //3
while(begin>=2){ //
cur=cur->next;
if(!cur) return NULL;
begin--; //1
}
node* end=cur;
while(cnt>0){//
end = end->next;
if(!end) return NULL;
cnt--;
... 阅读全帖 |
|
l****c 发帖数: 782 | 4 写的蠢了点,但是也算是个思路吧,面试官
int compareVersion(string s1, int index1, string s2, int index2) {
//if s1 is larger, return 1, if s2 is larger return 2, else return 0
if (index1 < s1.size() && index2 < s2.size()) {
int tmpIndex1 = index1;
int prev1 = 0;
while (tmpIndex1 < s1.size()&&s1[tmpIndex1]!='.') {
int cur = (int)(s1[tmpIndex1] - '0');
prev1 = prev1*10 + cur;
tmpIndex1++;
}
int prev2 = 0;
int tmpIndex2 = index... 阅读全帖 |
|
d**********x 发帖数: 4083 | 5 实际上是O(logn)的空间,但是既然长度为2^32的数列可以借助一个int来作为辅助空间
,这点overhead基本可以忽略不计了。欢迎找bug:
#include
#include
#include
using namespace std;
#include
static int get_index(int i) {
if (i == 0) {
return 1;
}
return (1 << i) + 1;
}
void rearrange(vector& vec) {
int bitmap = 0; //"O(1)" space for length < (1 << 32)
int k = vec.size() / 2;
int n = vec.size();
for (int i = 0; get_index(i) < k; ++i) {
int cur = get_index(i);
... 阅读全帖 |
|
c********t 发帖数: 5706 | 6 是不是我改java改的有问题
bitmap判断那句改成下面的是不是对的?
if (((1 << i) & bitmap) != 0) {
continue;
}
static void rearrange(ArrayList list) {
int bitmap = 0; // "O(1)" space for length < (1 << 32)
int k = list.size() / 2;
int n = list.size();
for (int i = 0; get_index(i) < k; ++i) {
int cur = get_index(i);
if (((1 << i) & bitmap) != 0) {
continue;
}
int next = cur * 2;
String tmp = list.get(cur);
while (true) {
if... 阅读全帖 |
|
k*******t 发帖数: 144 | 7 class Solution {
public:
int longestConsecutive(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
unordered_map mapping;
for(int i = 0; i < num.size(); ++i)
{
mapping[num[i]] = true;
}
int longest = 0;
for(int i = 0; i < num.size(); ++i)
{
int len = 0;
int cur = num[i];
while(mapping.find(cur) != mapping.end... 阅读全帖 |
|
p****n 发帖数: 4 | 8 Get the Sum of 对角线 of 螺旋(线) in n X n
Starting with the number 1 and moving to the right in a counter-clockwise
direction a 5 by 5 .
The issue is that the 1 is in the middle.
Normally:
for example :螺旋(线) (3X3)
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
[leetcode]Spiral Matrix II
(2013-03-12 15:14:57)
转载▼
标签:
分类: leetcode
Given an integer n, generate a square matrix filled with elements from 1 to
n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8,... 阅读全帖 |
|
c***0 发帖数: 449 | 9 bool isBST(node* cur, int lowBound, int highBound){
if(cur){
if(cur->value > lowBound && cur->value < highBound)
return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
right, cur->value, highBound);
else
return false;
}
return true;
}
请指教。 |
|
c***0 发帖数: 449 | 10 bool isBST(node* cur, int lowBound, int highBound){
if(cur){
if(cur->value > lowBound && cur->value < highBound)
return isBST(cur->left, lowBound, cur->Value) && isBST(cur->
right, cur->value, highBound);
else
return false;
}
return true;
}
请指教。 |
|
T*******e 发帖数: 4928 | 11 应该不需要。
void bstToDListHelper(Node *cur, Node *&head, Node *&pre){
if(!cur) return;
bstToDListHelper(cur->left, head, pre);
cur->left=pre;
if(pre){
pre->right=cur;
}else{
head=cur;
}
pre=cur;
bstToDListHelper(cur->right, head, pre);
}
Node *bstToDList(Node *root){
if(!root) return NULL;
Node *head=NULL, *pre=NULL;
bstToDListHelper(root, head, pre);
return head;
} |
|
a****r 发帖数: 87 | 12 An iterative version:
TreeNode *transform(TreeNode *root){
if(root == nullptr) return nullptr;
TreeNode *parent = nullptr;
TreeNode *cur = root;
TreeNode *b1 = nullptr;
TreeNode *b2 = nullptr;
while(cur){
// save the left and right child info before wiping them out.
TreeNode *a = cur->left;
b2 = cur->right;
cur->left = b1;
cur->right = parent;
// move to the next step
b1 = b2;
parent = cur;
... 阅读全帖 |
|
l*****a 发帖数: 14598 | 13 while(1) {
if( cur.left!=null){ push(cur);cur=cur.left; }
else if( cur.right!=null){ push(cur);cur=cur.right; }
else { }
} |
|
a***e 发帖数: 413 | 14 这道题和1我都是觉得iterative的比recursive的好写,看别人答案也更容易懂,(压
根儿没写出通过的recursion来)。下面这个都不知道思路是怎么地,特别是怎么能
connect(dummy.next);就保证 dummy.next 就是下一层的第一个了呢?见笑哈。
如果明天还看不出来就去VS里面调调。。。。。。
void connect(TreeLinkNode *root) {
if (root==NULL) return;
TreeLinkNode dummy(-1);
for (TreeLinkNode *cur = root, *prev=&dummy; cur; cur = cur->next)
{
if (cur->left!=NULL){
prev->next = cur->left;
prev = prev->next;
}
... 阅读全帖 |
|
l*****a 发帖数: 14598 | 15 需要递归吗?
Node greater=null;
Node cur=root;
while(cur!=null) {
if(cur.val<=target) { cur=cur.right; }
else { greater=cur; cur=cur.left; }
}
if (greater==null) // no result
else return greater.val;
不需要15分钟把 |
|
l*****a 发帖数: 14598 | 16 List> func(int n) {
List> result= new ArrayList>();
List cur=new ArrayList<>();
get(n,2,cur,result);
return result;
}
public get(int target,int start,List cur,List> result
) {
if(target==1) {
result.add(new ArrayList(cur));
return;
}
for(int i=start;i<=target/2;i++) {
if(target%i==0) {
cur.add(i);
get(target/i,i,c... 阅读全帖 |
|
m*****k 发帖数: 731 | 17 public static int[] plusOne(int[] arr){
//nonsense check ...
int cur = arr.length - 1;
while(cur>=0 && arr[cur]==9){
arr[cur]=0;
cur--;
}
if(cur==-1){
int[] re = Arrays.copyOf(arr, arr.length + 1);
re[0]=1;
return re;
}
else{
arr[cur] = arr[cur]+1;
return arr;
}
} |
|
C****e 发帖数: 27 | 18 我今天也正好在做这题,看的是网络答案,但是不知道main函数要怎么写才能实现调用
输出结果呢?
public class clonegraph {
public static void main(String[] args){
}
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node==null)
return null;
LinkedList queue = new LinkedList<
UndirectedGraphNode>();
HashMap map = new HashMap<
UndirectedGraphNode, UndirectedGraphNode>();
UndirectedGraphNode copy = new UndirectedGraphNode(nod... 阅读全帖 |
|
I**********s 发帖数: 441 | 19 来自主题: JobHunting版 - 问个面试题 完整代码:
/**
* 一个Cube有6个面,每一个面有一个color,这6个面的color是可以
* 任意的,而且可以重复。输入一系列的cube,判断这些cube能否组成一个四面都是
同样
* 颜色的tower(这些cube一个接一个竖着摞,没有并排的cube)。
*/
#include
using namespace std;
// Cube class. Color of cube is given in constructor.
class cube {
public:
int sides[6]; // {top, side1, side2, side3, side4, bottom}
int colors[6];
int config;
cube() {}
void init(int color[6]) {
for (int i = 0; i < 6; ++ i) colors[i] = color[i];
config = 0;
}
// given a cube ... 阅读全帖 |
|
g*******y 发帖数: 1930 | 20 可以判断i/=10之后,如果i是0了,就不继续递归了。
不管i是不是0,i%10是肯定会成为一个节点的。
如果输入是0,那么就输出一个单节点就结束了
如果输入不是0,最后i变成0以后也就结束了。
pair construct(int i){
Node *cur = new Node;
cur->data = i%10;
cur->next = 0;
i/=10;
if(i){
pair r = construct(i);
r->second->next = cur;
return make_pair(r->first,cur);
}else{
return make_pair(cur,cur);
}
} |
|
l*********3 发帖数: 26 | 21 1. Maze Problem
set the farest reachable position, Max = 0;
cur = 0;
while ( cur <= Max )
do
if ( cur + Maze[cur] > Max )
Max = cur + Maze[cur];
if ( Max >= length(Maze-1) )
return reachable;
cur++;
endwhile
return unreachable;
The time complexity is O(N).
2. Popular 3-hit problem:
Build two hash tables, the first Hc for the customers, the second Hp for
the 3-hit links.
Scan the hitting records, adding the hit reco... 阅读全帖 |
|
d*******l 发帖数: 338 | 22 来自主题: JobHunting版 - 问个面试题 #include
using namespace std;
int f[110];
int p[50];
int cur = 1;
int ans;
void solve(int n, int m, int s)
{
if(m == 0) {
/* for(int i = 0; i < cur; i++)
cout << p[i]+1 << " ";
cout << endl;
*/ ans += n-1-p[cur-1];
return ;
}
for(int i = s; i < n; i++) {
if(!f[i]) {
p[cur++] = i;
for(int j = cur-1; j >= 0; j--)
if(2*i-p[j] < n)
f[2*i-p[j]]++;
solve(n, m... 阅读全帖 |
|
K*******g 发帖数: 26 | 23 跟你的思路一样,不过找第一个结点可以优化以下
struct Node{
int value;
Node *left;
Node *right;
Node *sibling;
};
void connect_sibling(Node *root){
root->sibling = 0;
Node *cur, *dummy;
dummy = new Node;
while(root){
cur = dummy;
while(root){
if(root->left){
cur->sibling = root->left;
cur = root->left;
}
if(root->right){
cur->sibling = root->right;
cur = root->right;
... 阅读全帖 |
|
d*******u 发帖数: 186 | 24 这个算法太牛了,不知我的理解对不对?
void connect_sibling( Node *root ) {
root->sibling = null;
Node *cur, *dummy;
dummy = new Node;
while( root ) { //dummy->slibling是指向上次下一层的最左节点。
cur = dummy; //这样保证dummy->slibling总指向最左节点
while( root ) { // 找上一层的最右边节点, 假设当前层sibling已连好。
if( root->left ) {
cur->sibling = root->left;
cur = root->left;
}
if(root->right){
cur->sibling = root->right;
... 阅读全帖 |
|
n****r 发帖数: 120 | 25 public static void removeDuplicats(Node x){
if(x == null || x.next == null) return;
Node cur = x;
while(cur != null && cur.next != null){
Node next = cur.next;
while(cur.value == next.value){
next = next.next;
if(next == null)
break;
}
cur.next = next;
cur = next;
}
} |
|
m*****k 发帖数: 731 | 26 我觉得就是inOrder travesal break into 2 parts 吧?
void init()
{
pushLeftDown(root )
}
void pushLeftDown(Node cur){
if(cur == null) return;
stack.push(cur);
while(cur.left!=null){
stack.push(cur.left);
cur = cur.left;
}
}
boolean hasNext()
{
return stack.isEmpty();
}
Node next(){
if hasNext(){
Node top = stack.pop();
pushLeftDown(top.right);
return top;
}
else{
return null;
}
}
then hasNext() just retur... 阅读全帖 |
|
K*********n 发帖数: 2852 | 27 不要用recursion,用queue
Node cur = root;
q.enqueue(cur);
while(!q.isEmpty()){
cur = q.dequeue();
System.out.println(cur.data);
if (cur.left != null)
q.enqueue(cur.left);
if (crur.right != null)
q.enqueue(cur.right);
} |
|
K*********n 发帖数: 2852 | 28 不要用recursion,用queue
Node cur = root;
q.enqueue(cur);
while(!q.isEmpty()){
cur = q.dequeue();
System.out.println(cur.data);
if (cur.left != null)
q.enqueue(cur.left);
if (crur.right != null)
q.enqueue(cur.right);
} |
|
w****g 发帖数: 3 | 29 我用的是先sort的解法。
http://www.yangsheep.net/wuyang/wordpress/?p=727
class Solution {
public:
struct mycmpclass {
bool operator() (Interval i,Interval j) { return (i.start < j.start); }
} mycmpobject;
vector merge(vector &intervals) {
vector ans;
if(intervals.empty()) return ans;
sort(intervals.begin(), intervals.end(), mycmpobject);
Interval cur=intervals[0];
for(int i = 1; i < intervals.size(); i++) {
if(cur.end >= intervals[i].start)... 阅读全帖 |
|
w****g 发帖数: 3 | 30 我用的是先sort的解法。
http://www.yangsheep.net/wuyang/wordpress/?p=727
class Solution {
public:
struct mycmpclass {
bool operator() (Interval i,Interval j) { return (i.start < j.start); }
} mycmpobject;
vector merge(vector &intervals) {
vector ans;
if(intervals.empty()) return ans;
sort(intervals.begin(), intervals.end(), mycmpobject);
Interval cur=intervals[0];
for(int i = 1; i < intervals.size(); i++) {
if(cur.end >= intervals[i].start)... 阅读全帖 |
|
a******8 发帖数: 90 | 31 我顺便练个:只贴关键的设计部分,欢迎大家提改进意见
class Node
{
char last_c;
int num;
Node* parent;
Node* child[26];
list lsugg;
hash_map msugg;
};
class PrefixTree
{
private:
Node* m_root;
public:
PrefixTree(){m_root = NULL;}
list giveSugg(string word)
{
Node* cur = m_root;
list empty;
for(int i = 0; i < word.size(); i++)
{
cur = cur->child[word[i]];
if(!cur)return empty;
}
... 阅读全帖 |
|
b*****6 发帖数: 179 | 32 前些天我也看到这个题,顺手写了一下:思路是找到s.length()的所有prime factor,
然后看根据这个factor移位是不是满足题的条件,如果是,结果加length/factor - 1
public static int cyclic(String s)
{
int len = s.length();
if (len <= 1)
return 1;
int result = 1;
ArrayList factors = get_factors(len);
for (int fctr : factors)
{
if (valid_cyclic(s, fctr))
result += len / fctr - 1;
}
return result;
}
sta... 阅读全帖 |
|
b*****6 发帖数: 179 | 33 前些天我也看到这个题,顺手写了一下:思路是找到s.length()的所有prime factor,
然后看根据这个factor移位是不是满足题的条件,如果是,结果加length/factor - 1
public static int cyclic(String s)
{
int len = s.length();
if (len <= 1)
return 1;
int result = 1;
ArrayList factors = get_factors(len);
for (int fctr : factors)
{
if (valid_cyclic(s, fctr))
result += len / fctr - 1;
}
return result;
}
sta... 阅读全帖 |
|
f****s 发帖数: 74 | 34 有个不需要的版本,顺便再练一遍。
void inorder(Node* root){
if(!root)
return;
stack s;
Node* cur=root;
while(1){
while(cur){
s.push(cur);
cur=cur->left;
}
if(s.empty())
break;
cout<val;
s.pop();
cur=cur->right;
}
}
|
|
M******7 发帖数: 30 | 35 public Node getMedian(Node head){
if(head==null)return head;
Node p1=head;
Node p2=head;
while(p2.next!=null&&p2.next.next!=null){
p1=p1.next;
p2=p2.next.next;
}
return p1;
}
public Node Merge(Node n1, Node n2){
Node cur=new Node(-1);
Node head=cur;
while(n1!=null&&n2!=null){
if(n1.value<=n2.value){
cur.next=n1;
n1=n1.next;
}else{
... 阅读全帖 |
|
g********E 发帖数: 178 | 36 1,第一层循环的时候跳过重复的;
2,第二层循环从两头跳过循环的;
vector< vector > threeSum(vector &num){
int n = num.size();
vector< vector > res;
vector cur;
sort(num.begin(), num.end());
for(int i = 0; i < n-2; i++){
if(i > 0 && num[i] == num[i-1]) continue;
cur.push_back(num[i]);
int start = i + 1, end = n - 1;
while(start < end){
if(start > i+1 && num[start] == num[start-1]) {
start++; continue;
... 阅读全帖 |
|
b******7 发帖数: 92 | 37 #define BLOCK_SIZE 1024
#define ALIGN(size,unit) (((size)+(unit-1))/(unit)*(unit))
struct ChunkHead{
ChunkHead * next;
};
#define CHUNK_SIZE ALIGN(sizeof(ChunkHead) + BLOCK_SIZE, sizeof(int))
ChunkHead * firstChunk;
void initChunk(void * addr, size_t size)
{
firstChunk = NULL;
Chunk * pre = NULL;
for(int i = 0; i < size/CHUNK_SIZE; i++)
{
ChunkHead * cur = (ChunkHead *)(addr + i* CHUNK_SIZE);
if( pre != NULL)
... 阅读全帖 |
|
f*******r 发帖数: 180 | 38 只需要把从last到cur之前value是9的置0就可以了把, 最坏情况下需要把整个list重
新走一边。
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), last = dummy, cur = dummy;
while (l1 != null && l2 != null) {
int sum = l1.value + l2.value;
cur.next = new ListNode(sum % 10);
cur = cur.next;
l1 = l1.next;
l2 = l2.next;
if (sum / 10 > 0) { // has carry
last.value++;
las... 阅读全帖 |
|
T*******e 发帖数: 4928 | 39 所以你会用一个变量存着p的子树的最大和值maxSoFar,然后比较p的子树的最大值
maxSoFar与那四个通过p的最大和值maxAcrossCurNode,来更新从下往上走到p为止的最
大值maxSoFar。
int maxPathSumHelper(TreeNode *cur, int &maxSoFar){
if(!cur) return 0;
int maxLeft=maxPathSumHelper(cur->left, maxSoFar);
int maxRight=maxPathSumHelper(cur->right, maxSoFar);
int maxAcrossCurNode=cur->val;
if(maxLeft>0) maxAcrossCurNode+=maxLeft;
if(maxRight>0) maxAcrossCurNode+=maxRight;
maxSoFar=max(maxAcrossCurNode, maxSoFar); //update maxSoFar !!!!
return max(cur->val,... 阅读全帖 |
|
r**h 发帖数: 1288 | 40 但是我就是直接替换的呀
将
if(cur->left != NULL){
cur = cur->left;
st.push(cur);
}
换成
if(cur->left != NULL){
st.push(cur->left);
}
应该不存在cur的值被改变的问题吧? |
|
J*****a 发帖数: 4262 | 41 这是linkedin第一轮电面的一位国人大哥给我的“warm-up”题,首先是序列化
我写成了tree类的成员函数,所以没有把root当成方法的参数
public void serialize(){
File file = new File("test.txt");
if(root == null) return;
FileWriter fw = null;
try{
fw = new FileWriter(file);
BufferedWriter bf = new BufferedWriter(fw);
Queue q = new LinkedList();
q.add(root);
while(!q.isEmpty()){
Node cur = q.poll();
if(cur == null) bf.write("# ");
else{
bf.write(cur.data + " ");
... 阅读全帖 |
|
m**p 发帖数: 189 | 42 为什么泥?
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* dummy = new ListNode(-1);
ListNode* pivot = new ListNode(x);
ListNode* dummy_tail = dummy;
ListNode* pivot_tail = pivot;
ListNode* cur = head;
while (cur) {
if (cur->val < x) {
dummy_tail->next = cur;
dummy_tail = dummy_tail->next;
} else {
... 阅读全帖 |
|
e*******s 发帖数: 1979 | 43 Copy List with Random Pointer
A linked list is given such that each node contains an additional random
pointer which could point to any node in the list or null.
Return a deep copy of the list.
下面注释里面是别人写的能跑过的 上面的是我的 到现在都改得几乎一模一样了
可是还是超时
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomList... 阅读全帖 |
|
b*********s 发帖数: 115 | 44 非大牛
我是在最开始加一个dummy head
public class Solution {
public ListNode insertionSortList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode preNode = dummy;
ListNode curNode = dummy.next;
while (curNode != null) {
ListNode pre = dummy;
ListNode cur = dum... 阅读全帖 |
|
k****r 发帖数: 807 | 45 我是这样写的,但是不能通过,
ListNode *insertionSortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (head == NULL || head->next == NULL) return head;
ListNode dummyHead(INT_MIN);
(&dummyHead)->next = head;
ListNode *cur = head->next;
while (cur) {
ListNode *pre = &dummyHead;
while(pre->next && pre->next->val < cur->val) pre = pre... 阅读全帖 |
|
n*******s 发帖数: 482 | 46 validation string DP (2d)
M[CurrentLength, Last2Index]
Last2Index:
0: AA
1: AB
..
8: CC
M[i+1, 0 - 9] is depends on M[i, 0 - 9]
也可以用两个数组pre, current来简化
pre=[1,1,1,1,1,1,1,1,1]
for(j = 3 to n)
cur= [0]*9
for i = 0 to 8
tmp = pre[i] +1
switch(i)
case 0 :
cur[0]+= tmp // AA -> AAA
cur[1]+= tmp // AA => AAB
cur[2]+= tmp // AA => AAC
break;
case... 阅读全帖 |
|
y*******x 发帖数: 40 | 47 似乎可以用stack?
如果当前读到的level等于前一个节点的level+1,则当前节点是前一个节点的左节点。
否则,从栈中重新获取前一个节点,直到满足判断条件,则当前节点是前一个节点的右
节点。最后把当前节点压栈。循环直到栈为空,构建完成。
主要流程的伪代码大致如下(没考虑null等细节)
stack.push(root);
while(!stack.isEmpty()){
read value and level from input.
TreeNode cur = new TreeNode(value,level);
if(cur.level=pre.level+1){
pre.left=cur;
}else{
while(cur.level!=pre.level+1){
pre = stack.pop();
}
pre.right = cur;
}
pre = cur;
stack.push(cur);
} |
|
A****L 发帖数: 138 | 48 这个事我以前开的帖子。
http://www.mitbbs.com/article_t0/JobHunting/32682961.html
代码如下:
public class Solution {
public class Ladder {//Define Ladder class it's important
public ArrayList parentList;
public String word;
public Ladder(String w, Ladder parent) {word=w; parentList = new
ArrayList(); parentList.add(parent);}
}
ArrayList> ladders=new ArrayList>();;
public ArrayList> findLadders(String sta... 阅读全帖 |
|
A****L 发帖数: 138 | 49 这个事我以前开的帖子。
http://www.mitbbs.com/article_t0/JobHunting/32682961.html
代码如下:
public class Solution {
public class Ladder {//Define Ladder class it's important
public ArrayList parentList;
public String word;
public Ladder(String w, Ladder parent) {word=w; parentList = new
ArrayList(); parentList.add(parent);}
}
ArrayList> ladders=new ArrayList>();;
public ArrayList> findLadders(String sta... 阅读全帖 |
|
k****f 发帖数: 19 | 50 不知道有什么错误没有
vector Print(int a[], int n)
{
if (n == 0)
{
string s = "0-99";
return vector(1, s);
}
int pre = 0;
vector ret;
for (int i = 0; i <= n; i++)
{
int cur;
if (i == n)
cur = 100;
else
cur = a[i];
if (cur > pre)
{
if (cur - pre > 2)
ret.push_back(to_string(pre) + "-" + to_string(cur - 1));
else
... 阅读全帖 |
|