Leetcode
HeatDeath
Learn by doing!
展开
-
LeetCode 分类练习(3)—— 指针碰撞
167. Two Sum II - Input array is sortedGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum shoul原创 2017-11-23 15:33:20 · 1154 阅读 · 0 评论 -
Leetcode学习(37)—— Construct the Rectangle
For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L原创 2017-05-09 10:33:52 · 387 阅读 · 0 评论 -
Leetcode学习(36)—— Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2017-05-09 10:08:59 · 451 阅读 · 0 评论 -
Leetcode学习(35)—— Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The order原创 2017-05-09 09:37:57 · 475 阅读 · 0 评论 -
Leetcode学习(34)—— Move Zeroes
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct原创 2017-05-09 09:16:48 · 653 阅读 · 0 评论 -
Leetcode学习(33)—— 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-05-09 08:57:29 · 459 阅读 · 0 评论 -
Leetcode学习(32)—— Maximum Depth of Binary Tree
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.# Definition for a binary tree node.# clas原创 2017-05-09 00:25:35 · 574 阅读 · 0 评论 -
Leetcode学习(31)—— Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r原创 2017-05-08 23:58:28 · 362 阅读 · 0 评论 -
Leetcode学习(30)—— Distribute Candies
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these原创 2017-05-08 23:39:10 · 988 阅读 · 0 评论 -
Leetcode学习(29)—— Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st原创 2017-05-08 23:05:20 · 780 阅读 · 0 评论 -
Leetcode学习(28)—— Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.#原创 2017-05-08 20:34:35 · 421 阅读 · 0 评论 -
Leetcode学习(27)—— Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digi原创 2017-05-08 20:00:45 · 422 阅读 · 0 评论 -
Leetcode学习(38)—— First Unique Character in a String
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2. Note: You may assume原创 2017-05-09 18:07:37 · 480 阅读 · 0 评论 -
Leetcode学习(39)—— 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-09 23:31:27 · 584 阅读 · 0 评论 -
LeetCode 学习 LinkedList 专项(1)—— Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, t原创 2017-10-15 16:19:24 · 384 阅读 · 0 评论 -
LeetCode 学习 LinkedList 专项(2)—— Reverse Linked List
Reverse a singly linked listclass ListNode { int val; ListNode next; ListNode(int x) { val = x; }}public class reverseList { public ListNode reverseList(ListNode head) {原创 2017-10-15 16:52:29 · 321 阅读 · 0 评论 -
LeetCode 学习 LinkedList 专项(3)—— Remove Duplicates from Sorted List
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.删除链表中重复的元素class ListNode { int原创 2017-10-15 17:58:04 · 294 阅读 · 0 评论 -
LeetCode 学习 LinkedList 专项(4)—— Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.合并两个有序的链表,并返回其作为一个新的链表。新链表的节点应该通过拼接前两个链表的节点而成。public class m原创 2017-10-15 22:42:19 · 311 阅读 · 0 评论 -
LeetCode 学习 LinkedList 专项(5)—— Linked List Cycle
Given a linked list, determine if it has a cycle in it.给出一个链表,判断其是否为环形链表。public class hasCycle { public boolean hasCycle(ListNode head) { ListNode pre = head; ListNode p = head;原创 2017-10-15 23:47:22 · 409 阅读 · 0 评论 -
LeetCode 分类练习(4)—— set 和 map 在解题中的应用
349. Intersection of Two ArraysGiven two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be原创 2017-11-26 16:00:53 · 2034 阅读 · 0 评论 -
LeetCode 分类练习(5)—— 链表相关习题(1)
206. Reverse Linked ListReverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?package com.leetcode.linked原创 2017-11-27 22:59:19 · 651 阅读 · 0 评论 -
LeetCode 分类练习(2)—— 三向切分 partition 思想的应用
75. Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the原创 2017-11-22 23:53:46 · 634 阅读 · 0 评论 -
LeetCode 分类练习(1)—— 在数组中移动指定元素、删除指定元素、删除重复元素
283. Move ZeroesGiven an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after ca原创 2017-11-22 23:32:26 · 600 阅读 · 0 评论 -
Leetcode学习(41)—— Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger t原创 2017-05-10 14:59:56 · 504 阅读 · 0 评论 -
Leetcode学习(40)—— Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note: You may assume the str原创 2017-05-10 14:18:27 · 452 阅读 · 0 评论 -
Leetcode学习(26)—— Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".原创 2017-05-08 19:34:08 · 380 阅读 · 0 评论 -
Leetcode学习(25)—— Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2017-05-08 19:24:35 · 541 阅读 · 0 评论 -
Leetcode学习(24)—— Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed原创 2017-05-08 16:51:35 · 477 阅读 · 0 评论 -
Leetcode学习(11)—— 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-05-06 11:13:57 · 385 阅读 · 0 评论 -
Leetcode学习(10)—— 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-05-06 10:33:47 · 327 阅读 · 0 评论 -
Leetcode学习(9)——Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".class Solution(object): def reverseString(self, s): return s[::-1]原创 2017-05-06 10:32:13 · 336 阅读 · 0 评论 -
Leetcode学习(8)—— 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.给出一个单词 list, 返回那些可以在键盘上一行中输入的单词Input: ["Hello", "Alaska",原创 2017-05-06 01:23:07 · 497 阅读 · 0 评论 -
Leetcode学习(7)—— Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: 1、The given integer is guaranteed to fit within the range o原创 2017-05-06 00:29:54 · 487 阅读 · 0 评论 -
Leetcode学习(6)—— Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Input: "Let's take LeetCode contest"Output: "s'teL e原创 2017-05-06 00:02:55 · 376 阅读 · 0 评论 -
Leetcode学习(5)—— Reshape the Matrix
In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re given a matrix represented by a two-dimensio原创 2017-05-05 23:47:37 · 591 阅读 · 0 评论 -
Leetcode学习(3)—— 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-05 21:50:07 · 750 阅读 · 0 评论 -
Leetcode学习(4)—— 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.Hamming 距离是两个整数对应的 位 bit 不相原创 2017-05-05 23:04:05 · 426 阅读 · 0 评论 -
Leetcode学习(1)—— Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the sa原创 2017-05-05 12:59:22 · 565 阅读 · 0 评论 -
Leetcode学习(43)—— Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.Example 1:Input: [5,原创 2017-05-11 16:53:17 · 526 阅读 · 0 评论 -
Leetcode学习(12)—— Base 7
Given an integer, return its base 7 string representation.Example 1:Input: 100Output: "202"Example 2:Input: -7Output: "-10" Note: The input will be in range of [-1e7, 1e7].class Solution(object原创 2017-05-06 14:26:06 · 306 阅读 · 0 评论