LeetCode
wdkirchhoff
这个作者很懒,什么都没留下…
展开
-
LeetCode Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possibl原创 2017-05-12 10:30:30 · 404 阅读 · 0 评论 -
LeetCode Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could you原创 2017-03-22 11:13:17 · 347 阅读 · 0 评论 -
LeetCode Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-03-21 19:15:57 · 348 阅读 · 0 评论 -
LeetCode Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2017-03-21 19:06:29 · 250 阅读 · 0 评论 -
LeetCode Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely s原创 2017-03-21 19:05:01 · 276 阅读 · 0 评论 -
LeetCode Minimum Moves to Equal Array Elements
#coding=utf-8class Solution(object): def minMoves(self, nums): """ :type nums: list[int] :rtype: int """ #该问题与每次自减一个元素,等价 res = 0 m = min(nu原创 2017-03-28 12:24:04 · 395 阅读 · 0 评论 -
LeetCode First Unique Character in a String
#coding=utf-8import stringclass Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ k_v = {} t = [s.find(i) for i in str原创 2017-03-27 23:56:25 · 404 阅读 · 0 评论 -
LeetCode Ransom Note
class Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ magazine_k_v = {原创 2017-03-27 23:34:18 · 458 阅读 · 0 评论 -
LeetCode Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2.The原创 2017-03-20 20:38:55 · 452 阅读 · 0 评论 -
LeetCode Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For n原创 2017-03-20 20:22:54 · 300 阅读 · 0 评论 -
LeetCode Detect Captital
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word原创 2017-03-23 19:10:32 · 365 阅读 · 0 评论 -
LeetCode Delete Node in a Linked List
# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def deleteNode(self, node): """原创 2017-03-30 19:31:46 · 442 阅读 · 0 评论 -
LeetCode Base 7
class Solution(object): def convertToBase7(self, num): """ :type num: int :rtype: str """ res= "" flag = 0 if num == 0: return "0原创 2017-03-30 19:41:10 · 452 阅读 · 0 评论 -
LeetCode Majority Element
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 always原创 2017-05-12 10:14:24 · 374 阅读 · 0 评论 -
LeetCode Assign Cookies
class Solution(object): def findContentChildren(self, g, s): """ :type g: list[int] :type s: list[int] :rtype: int """ res = 0 g.sort()原创 2017-03-27 14:20:24 · 447 阅读 · 0 评论 -
LeetCode Relative Ranks
class Solution(object): def findRelativeRanks(self, nums): """ :type nums: list[int] :rtype: list[str] """ origin_nums = nums[:] nums.sort()原创 2017-03-26 15:43:25 · 443 阅读 · 0 评论 -
LeetCode Move Zeroes
class Solution(object): def moveZeroes(self, nums): """ :type nums: list[int] :rtype: void Do not return anything, modify nums in-place instead. """ i,j= 0,原创 2017-03-26 00:50:15 · 420 阅读 · 0 评论 -
LeetCode Construct the Rectangle
import mathclass Solution(object): def constructRectangle(self, area): """ :type area: int :rtype: list[int] """ #x^2=area x = math.sqrt(area)原创 2017-03-26 00:31:31 · 401 阅读 · 0 评论 -
LeetCode Add Digits
class Solution(object): def addDigits(self, num): """ :type num: int :rtype: int """ while num % 10 != num: s = str(num) num = sum([i原创 2017-03-25 23:54:33 · 450 阅读 · 0 评论 -
LeetCode Sum of Two Integers
# coding=utf-8class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ MAX_INT = 0x7FFFFFFF MIN_INT = 0原创 2017-03-25 23:36:39 · 477 阅读 · 0 评论 -
LeetCode Invert Binary Tree
# Definition for a binary tree node.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution(object): def invert原创 2017-03-25 23:35:36 · 313 阅读 · 0 评论 -
LeetCode Find the Difference
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added in原创 2017-03-23 20:15:19 · 364 阅读 · 0 评论 -
LeetCode Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.Example 1: Input: [“Hello”, “Alaska”, “Dad”, “Peace”] O原创 2017-03-20 20:03:57 · 346 阅读 · 0 评论 -
LeetCode Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.Note: The given integer is guaranteed to fit within the range of a 32-b原创 2017-03-19 22:06:53 · 251 阅读 · 0 评论 -
LeetCode Reverse Integer
Reverse Integer Total Accepted: 61132 Total Submissions: 219035 My Submissions Question Solution Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321click to sh原创 2015-03-20 20:53:06 · 1916 阅读 · 0 评论 -
LeetCode Number of 1 Bits
Number of 1 Bits Total Accepted: 10567 Total Submissions: 28552 My Submissions Question Solution Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known原创 2015-03-20 19:59:10 · 1856 阅读 · 0 评论 -
LeetCode Same Tree
Same Tree Total Accepted: 52651 Total Submissions: 125166 My Submissions Question Solution Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered e原创 2015-03-20 22:50:12 · 4058 阅读 · 0 评论 -
LeetCode Reverse Bits
Reverse Bits Total Accepted: 8148 Total Submissions: 29905 My Submissions Question Solution Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary a原创 2015-03-20 20:12:11 · 3616 阅读 · 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原创 2014-12-04 08:43:48 · 710 阅读 · 0 评论 -
LeetCode N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.这个比N-Queens简单点,但还是一样的class Solution {public: int totalNQ原创 2014-12-03 23:22:59 · 818 阅读 · 0 评论 -
LeetCode N-Queens
又是一个八皇后问题:Given an integer n, return all distinct solutions to the n-queens puzzle.Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both原创 2014-12-03 21:08:38 · 787 阅读 · 0 评论 -
LeetCode Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2014-12-04 08:44:58 · 752 阅读 · 0 评论 -
LeetCode Pow(x,n)
class Solution {public:double pow(double x,int n){ if(n<0)return 1.0/power(x,-n); return power(x,n); } double power(double x, int n) { if(n == 0) return 1; double temp =原创 2014-12-03 20:59:19 · 721 阅读 · 0 评论 -
LeetCode Rotate Array
Rotate Array Total Accepted: 12759 Total Submissions: 73112 My Submissions Question Solution Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,原创 2015-03-20 21:52:03 · 2437 阅读 · 3 评论 -
LeetCode Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution Say you have an array for which the ith element is the price of a given stock on da原创 2015-03-21 21:24:11 · 4177 阅读 · 0 评论 -
LeetCode Excel Sheet Column Title
Excel Sheet Column Title Total Accepted: 18284 Total Submissions: 103291 My Submissions Question Solution Given a positive integer, return its corresponding column title as appear in an Excel sheet.F原创 2015-03-21 22:17:38 · 1534 阅读 · 0 评论 -
LeetCode Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Now your job is to find the total Hamming distance between all pairs of the given numb原创 2017-03-19 21:26:48 · 270 阅读 · 0 评论 -
LeetCode Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note: 0 ≤ x, y < 231.Examp原创 2017-03-19 20:59:55 · 301 阅读 · 0 评论 -
LeetCode Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 50423 Total Submissions: 173779 My Submissions Question Solution Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2原创 2015-04-15 21:10:34 · 839 阅读 · 0 评论 -
找到轮转后的有序数组中第K小的数
我们可以通过二分查找法,在log(n)的时间内找到最小数的在数组中的位置,然后通过偏移来快速定位任意第K个数。 此处还是假设数组中没有相同的数,原排列顺序是递增排列。 在轮转后的有序数组中查找最小数的算法如下:int findIndexOfMin(int num[],int n) { int l = 0; int r = n-1; while(l原创 2015-04-15 22:35:20 · 1239 阅读 · 0 评论