瓦力冫
喜欢看点书,跑跑步,热爱游戏编程
展开
-
leetcode 反转链表 python
反转一个单链表。示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?# Definition for singly-linked list.# class ListNode:# def __init__(self...原创 2018-06-30 20:26:10 · 816 阅读 · 0 评论 -
LeetCode Binary Tree Inorder Traversal
1.题目Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial, coul原创 2015-03-09 10:48:31 · 1113 阅读 · 0 评论 -
LeetCode Unique Binary Search Trees
1.题目Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2015-03-08 20:07:54 · 1307 阅读 · 0 评论 -
LeetCode Excel Sheet Column Number
1.题目Given a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 2.解决方案class Solu原创 2015-03-08 17:41:18 · 1272 阅读 · 0 评论 -
LeetCode Search Insert Position
1.题目Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2015-03-06 16:47:48 · 1164 阅读 · 0 评论 -
LeetCode Convert Sorted Array to Binary Search Tree
1.题目Given an array where elements are sorted in ascending order, convert it to a height balanced BST.2.解决方案1 struct Node{ TreeNode* t; int l; int r; Node(vector &num, int l, int r)原创 2015-03-08 10:19:12 · 1184 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List
1.题目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.2.解决方案class Solution {public:原创 2015-03-06 17:24:06 · 1107 阅读 · 0 评论 -
LeetCode Excel Sheet Column Title
1.题目Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 2.解决方案1class原创 2015-03-08 17:36:23 · 1362 阅读 · 0 评论 -
LeetCode Binary Tree Postorder Traversal
1.题目Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivial, co原创 2015-03-08 11:09:43 · 1206 阅读 · 0 评论 -
LeetCode Climbing Stairs
1.题目You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?2.解决方案class Solution {public:原创 2015-03-06 17:42:00 · 1163 阅读 · 0 评论 -
Leetcode Same Tree
1.题目Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.2.解决方案1class S原创 2015-03-06 14:34:33 · 1182 阅读 · 0 评论 -
leetcode Rotate List
1. 题目描述Given a list, rotate the list to the right by k places, where k is non-negative.For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.2.解决方案1class Solution {public: Li原创 2015-03-06 09:42:35 · 1050 阅读 · 0 评论 -
LeetCode Binary Tree Preorder Traversal
1.题目Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, cou原创 2015-03-06 16:14:26 · 1315 阅读 · 0 评论 -
LeetCode Linked List Cycle
1. 题目 Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?2.解决方案class Solution {public: bool hasCycle(ListNode *head) { if(!head){原创 2015-03-06 15:40:37 · 1051 阅读 · 0 评论 -
LeetCode Maximum Depth of Binary Tree
1.题目描述Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.2.解决方案1class Solution {public:原创 2015-03-06 10:46:33 · 1248 阅读 · 0 评论 -
LeetCode Length of Last Word
1. 题目Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi原创 2015-03-17 15:20:30 · 1312 阅读 · 0 评论 -
LeetCode Maximum Subarray
1.题目Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has原创 2015-03-12 10:20:03 · 1478 阅读 · 2 评论 -
LeetCode Majority Element
1.题目Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element al原创 2015-03-12 20:42:36 · 1213 阅读 · 0 评论 -
Leetcode 有效的括号字符串 python
class Stack: """模拟栈""" def __init__(self): self.items = [] def isEmpty(self): return len(self.items)==0 def push(self, item): s...原创 2018-06-24 13:35:59 · 879 阅读 · 0 评论 -
两数相加 leetcode Python
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807# Definition for singly-lin...原创 2018-06-24 13:35:21 · 1651 阅读 · 0 评论 -
python 搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。示例 1:输入: [1,3,5,6], 5输出: 2示例 2:输入: [1,3,5,6], 2输出: 1示例 3:输入: [1,3,5,6], 7输出: 4示例 4:输入: [1,3,5,6], 0输出: 0class Soluti...原创 2018-05-27 09:19:19 · 1228 阅读 · 0 评论 -
python 寻找重复的数
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间,包括 1 和 n ,可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。示例 1:输入: [1,3,4,2,2] 输出: 2示例 2:输入: [3,1,3,4,2]输出: 3说明:不能更改原数组(假设数组是只读的)。只能使用额外的 O(1) 的空间。时间复杂度小于 O(n2) 。数组中只有一个重复...原创 2018-05-27 07:55:54 · 3122 阅读 · 0 评论 -
python 两个数组的交集 intersection of two arrays
给定两个数组,写一个函数来计算它们的交集。例子:给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示:每个在结果中的元素必定是唯一的。我们可以不考虑输出结果的顺序。class Solution(object): def intersection(self, nums1, nums2): """ :type nums...原创 2018-05-27 07:44:24 · 1464 阅读 · 0 评论 -
python 找不同 Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。请找出在 t 中被添加的字母。示例:输入: s = "abcd" t = "abcde"输出: e解释: 'e' 是那个被添加的字母。class Solution(object): def findTheDifference(self, s, t): """ ...原创 2018-05-27 07:02:16 · 1299 阅读 · 0 评论 -
数组拆分 I array-partition leetcode python
1. 题目 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。示例 1:输入: [1,4,3,2]输出: 4解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).提示:n 是正整数,范围在 [1, 10000].数...原创 2018-04-26 08:23:30 · 788 阅读 · 0 评论 -
C++ string 简单实现
#includeusing namespace std; class String{ friend ostream& operator<< (ostream&,String&);public: String(const char* str=NULL); //赋值构造兼默认构造函数(char) String(const String &o转载 2015-05-05 18:35:44 · 1664 阅读 · 0 评论 -
LeetCode Compare Version Numbers
1.题目Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and cont原创 2015-05-01 21:10:01 · 1467 阅读 · 0 评论 -
LeetCode Valid Palindrome
1.题目Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a pali原创 2015-05-01 17:46:06 · 1680 阅读 · 0 评论 -
LeetCode Happy Number
1. 题目Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the square原创 2015-05-01 14:03:19 · 2109 阅读 · 0 评论 -
LeetCode Add Binary
1.题目Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".2.解答class Solution {public: char intToChar(int input){ char strBuffer[1] = {0原创 2015-05-02 15:52:20 · 1781 阅读 · 0 评论 -
LeetCode Remove Element
1.题目Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.2.解决方原创 2015-03-13 21:25:18 · 1286 阅读 · 0 评论 -
LeetCode Find Minimum in Rotated Sorted Array
1.题目Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the array.原创 2015-03-13 20:40:59 · 1021 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
1.题目Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may原创 2015-03-06 09:55:34 · 1136 阅读 · 0 评论 -
LeetCode Two Sum
1.题目Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-03-06 09:39:37 · 1553 阅读 · 2 评论 -
找到单独的数字 Single Number
Given an array of integers, every element appearstwice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extr原创 2014-01-26 18:01:35 · 2003 阅读 · 0 评论 -
C++ 取石子游戏
1.游戏图文说明这个游戏是这样玩的:开始有两堆石子,第一堆有2个,第二堆有1个,开始是玩家1开始取石子,然后是玩家2取石子,规则是只能取某一堆的至少一颗石子,两人轮流取石子,直到有人取完石子,谁就输了。上图列出了游戏的所有可能:假设玩家1取了第一堆中的一颗石子,那么在接下来的回合,玩家2无论取第一堆或者第二堆石子中的一颗,那么玩家1都将会原创 2013-10-15 09:05:42 · 4353 阅读 · 0 评论 -
C++ Queue Example Rearranging RailRoad Cars 火车车厢重排问题
Before in article C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题 we use three stacks to solve the "Rearranging RailRoad Cars" problem. In this article, we will use two queues to solve the prob原创 2013-05-08 13:48:59 · 3097 阅读 · 0 评论 -
C++ Stack Example Rearranging RailRoad Cars 火车车厢重排问题
Look at figure 1. The railroad cars in the "Input track" is not sorted. We need them in a sorted way in "Output track" like cars in figure2.There are three holding tracks we can use to r原创 2013-05-07 17:07:39 · 3349 阅读 · 0 评论 -
The Towers of Hanoi recursion 汉诺塔 C++
What is Towers of Hanoi ?Figure1 shows an tower of hanoi setup with three discs on the first pole. Note that all the discs are a different size. The a The RulesThe goal is move all dis原创 2013-05-03 11:32:17 · 1778 阅读 · 0 评论 -
Circular Doubly Linked List 双向循环链表 C++ 例子
What a circular doubly linked list looks like?Look at Figure1, It is a circular doubly linked list have 8 nodes. If you compare it and the array in Figure2, you will find that each node in dou原创 2013-04-15 10:42:46 · 4261 阅读 · 0 评论