ITeye博客

coolgo,just go!

cozilla · 共 88 篇文章

sort link list

题目描述 对一个单链表原地(in-place)排序。即直接对链表结点排序。返回排序后链表的头结点。 链表结点的定义为(请不要在代码中再次定义该结构): C/C++ struct ListNode { int val; ListNode *next; } 渣做法!N^2 ListNode* insert(ListNode* head, ListNode* item) { if (item == NULL) return head; if (head == NULL) { item->next = NULL; return item; } if (item->val < head->val) {…

2013-10-13 20:11 · 评论(0)

[Leetcode] Gas Station

Gas Station AC Rate: 1017/4717 My Submissions There are N gas stations along a circular route, where the amount of gas at station i is gas[i] . You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station ( i +1). You begin the journey with an e…

2013-10-07 00:02 · LeetCode · Algorithm · 评论(0)

[Leetcode] Word Break 1 & 2

Word Break AC Rate: 2/13 My Submissions Given a string s and a dictionary of words dict , determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode" , dict = ["leet", "code"] . Return true because "leetcode" can be segmented as…

2013-10-05 23:26 · LeetCode · Algorithm · 评论(0)

[Leetcode] Single Number 1 & 2

Single Number AC Rate: 1243/2713 My Submissions Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? class Solution { public: int singleNumber(in…

2013-10-05 22:50 · LeetCode · Algorithm · 评论(0)

[Leetcode] Copy List with Random Pointer

Copy List with Random Pointer AC Rate: 350/1573 My Submissions 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 …

2013-10-05 22:43 · LeetCode · Algorithm · 评论(0)

[Leetcode] Path Sum

Path Sum AC Rate: 872/2770 My Submissions Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22 , 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as …

2013-10-03 23:52 · LeetCode · Algorithm · 评论(0)

[Leetcode] Remove Duplicates from Sorted List 1 & 2

Remove Duplicates from Sorted List AC Rate: 984/2755 My Submissions Given a sorted linked list, delete all duplicates such that each element appear only once . For example, Given 1->1->2 , return 1->2 . Given 1->1->2->3->3 , return 1->2->3 . /** * Definition for singly-linked list. * struct ListNod…

2013-10-03 23:34 · LeetCode · Algorithm · 评论(0)

[Leetcode] Scramble String

Scramble String AC Rate: 318/1523 My Submissions Given a string s1 , we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great" : great / \ gr eat / \ / \ g r e at / \ a t To scramble the string, we may choos…

2013-10-03 16:55 · LeetCode · Algorithm · 评论(0)

[Leetcode] Partition List

Partition List AC Rate: 428/1694 My Submissions Given a linked list and a value x , partition it such that all nodes less than x come before nodes greater than or equal to x . You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->2->…

2013-09-29 23:38 · LeetCode · Algorithm · 评论(0)

[Leetcode] Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can 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. For example, Given board = [ ["ABCE…

2013-09-21 23:21 · LeetCode · Algorithm · 评论(0)

[Leetcode] Decode Ways

Decode Ways Jun 25 '126747 / 26583 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded message "12" , it…

2013-09-11 17:33 · LeetCode · Algorithm · 评论(0)

[Leetcode] Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree Oct 3 '125768 / 16298 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NU…

2013-09-11 16:06 · LeetCode · Algorithm · 评论(0)

前k大个数

#include <cstdlib> #include <cstdio> #include <vector> #include <map> #include <algorithm> using namespace std; void qfind(vector<int>& a, int k, int left, int right, vector<int>&res ) { if (left > right || k <= 0) return; if (right - left + 1 <= k) { while (left <= right) res.push_back(a[left++]);…

2013-09-11 14:48 · 评论(0)

大整数运算

#include <cstdlib> #include <cstdio> #include <vector> #include <map> #include <algorithm> using namespace std; void reverse(char*s) { if (s == NULL ) return; char *e = s + strlen(s) - 1; while (s < e) swap(*s, *e), s++, e--; } char* add(char* a, char* b) { if (a == NULL || b == NULL) return NULL; …

2013-09-11 14:47 · 评论(0)

[Leetcode] Binary Tree Level Order Traversal

Binary Tree Level Order Traversal Sep 29 '125832 / 14746 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7} , 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], […

2013-09-10 14:39 · LeetCode · Algorithm · 评论(0)