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 · 382 阅读 · 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 · 324 阅读 · 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 · 328 阅读 · 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 · 238 阅读 · 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 · 264 阅读 · 0 评论 -
LeetCode Minimum Moves to Equal Array Elements
#coding=utf-8 class Solution(object): def minMoves(self, nums): """ :type nums: list[int] :rtype: int """ #该问题与每次自减一个元素,等价 res = 0 m = min(nu原创 2017-03-28 12:24:04 · 373 阅读 · 0 评论 -
LeetCode First Unique Character in a String
#coding=utf-8 import string class 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 · 363 阅读 · 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 · 432 阅读 · 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 · 438 阅读 · 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 · 279 阅读 · 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 · 348 阅读 · 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 · 410 阅读 · 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 · 430 阅读 · 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 · 351 阅读 · 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 · 415 阅读 · 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 · 417 阅读 · 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 · 388 阅读 · 0 评论 -
LeetCode Construct the Rectangle
import math class 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 · 374 阅读 · 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 · 412 阅读 · 0 评论 -
LeetCode Sum of Two Integers
# coding=utf-8 class 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 · 459 阅读 · 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 · 296 阅读 · 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 · 316 阅读 · 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 · 326 阅读 · 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 · 233 阅读 · 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 · 1902 阅读 · 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 · 1831 阅读 · 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 · 4032 阅读 · 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 · 3587 阅读 · 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 · 695 阅读 · 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 · 800 阅读 · 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 · 777 阅读 · 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 · 739 阅读 · 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 · 713 阅读 · 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 · 2402 阅读 · 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 · 4163 阅读 · 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 · 1520 阅读 · 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 · 251 阅读 · 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 · 283 阅读 · 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 · 820 阅读 · 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 · 1219 阅读 · 0 评论