
Leetcode练习
文章平均质量分 64
a1025461748
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode练习 476. 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原创 2017-03-12 20:37:40 · 172 阅读 · 0 评论 -
Leetcode练习 412. 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”.原创 2017-03-12 23:06:12 · 227 阅读 · 0 评论 -
Leetcode练习 500. 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", "Da原创 2017-03-12 22:10:44 · 207 阅读 · 0 评论 -
leetcode 283. 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 you原创 2017-05-02 22:51:46 · 178 阅读 · 0 评论 -
leetcode 492. 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-02 23:22:45 · 146 阅读 · 0 评论 -
leetcode 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtre原创 2017-05-02 23:59:31 · 314 阅读 · 0 评论 -
leetcode 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between iand j equals the distance between i and k (the order of t转载 2017-05-22 22:01:32 · 166 阅读 · 0 评论 -
leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm shoul原创 2017-05-22 22:48:05 · 168 阅读 · 0 评论 -
leetcode 551. Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters:'A' : Absent.'L' : Late.'P' : Present.A student could be原创 2017-05-22 23:35:45 · 210 阅读 · 0 评论 -
leetcode 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of th原创 2017-05-23 09:33:00 · 307 阅读 · 0 评论 -
leetcode 543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may原创 2017-05-23 12:11:07 · 159 阅读 · 0 评论 -
leetcode 572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this no转载 2017-05-23 13:20:39 · 285 阅读 · 0 评论 -
leetcode 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.先根据数组数建立完全BST,然后对树进行中序遍历将数组的数赋值到节点上/** * Definition for a binary tree node. * public class原创 2017-05-23 16:21:15 · 160 阅读 · 0 评论 -
leetcode 415. 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 Both num1 and num2 contains only digits 0-9.原创 2017-05-23 17:10:14 · 208 阅读 · 0 评论 -
leetcode 405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note:All letters in hexadecimal (a-f) must be in lowercase.The hexade原创 2017-05-24 11:00:03 · 167 阅读 · 0 评论 -
leetcode 121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-05-24 14:21:58 · 254 阅读 · 0 评论 -
leetcode 202. Happy Number
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 squares原创 2017-05-24 15:48:34 · 199 阅读 · 0 评论 -
leetcode 326. Power of Three
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?直接考虑后面的Follow up本来想直接“作弊”的方法把所有值列出来,觉得不太好,结果真有这种方法,转载 2017-05-24 16:31:00 · 195 阅读 · 0 评论 -
leetcode 231. Power of Two
Given an integer, write a function to determine if it is a power of two.除了之前的用法,还可以针对二进制的特性使用位操作public boolean isPowerOfTwo(int n) { return ((n & (n-1))==0 && n>0);}public class Solut转载 2017-05-24 19:11:51 · 160 阅读 · 0 评论 -
leetcode 83. 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.不太难,注意边界和判断条件防止越界/**原创 2017-05-24 19:28:19 · 154 阅读 · 0 评论 -
leetcode 70. Climbing Stairs
ou 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?Note: Given n will be a posit原创 2017-05-24 20:00:48 · 152 阅读 · 0 评论 -
leetcode 437. Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it转载 2017-05-24 21:42:05 · 189 阅读 · 0 评论 -
leetcode 35. Search Insert Position
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.原创 2017-05-24 22:16:55 · 169 阅读 · 0 评论 -
leetcode 53. Maximum Subarray
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] ha原创 2017-05-24 23:09:50 · 171 阅读 · 0 评论 -
leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,1原创 2017-05-25 09:18:45 · 180 阅读 · 0 评论 -
leetcode 191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 00000000原创 2017-05-25 09:44:13 · 222 阅读 · 0 评论 -
leetcode 263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2017-05-25 10:17:02 · 178 阅读 · 0 评论 -
leetcode 561. 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 a原创 2017-04-24 21:01:39 · 1480 阅读 · 1 评论 -
leetcode 557. 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.Example 1:Input: "Let's take LeetCode contes原创 2017-04-24 22:10:10 · 271 阅读 · 0 评论 -
leetcode 344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".public class Solution { public String reverseString(String s) {原创 2017-04-24 22:19:59 · 170 阅读 · 0 评论 -
leetcode 21. 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.可以用递归的方法来做,每次递归返回下一个节点/** * Definition for singl原创 2017-05-25 21:45:50 · 151 阅读 · 0 评论 -
leetcode 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee原创 2017-05-25 23:15:28 · 197 阅读 · 0 评论 -
leetcode 459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Engli原创 2017-05-26 09:25:57 · 194 阅读 · 0 评论 -
leetcode 496. 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 nums原创 2017-04-25 10:02:14 · 180 阅读 · 0 评论 -
leetcode 463. 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原创 2017-04-25 11:42:04 · 150 阅读 · 0 评论 -
leetcode 292. 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-04-25 13:52:04 · 151 阅读 · 0 评论 -
leetcode 485. 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: 3Explanation: The first two digits or the last three digits are consecutiv原创 2017-04-25 14:21:07 · 147 阅读 · 0 评论 -
leetcode 136. Single Number
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 ext原创 2017-04-25 15:06:46 · 142 阅读 · 0 评论 -
leetcode 198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2017-05-26 10:23:22 · 183 阅读 · 0 评论 -
leetcode 27. 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.原创 2017-05-26 10:45:53 · 158 阅读 · 0 评论