leetcode
Jessie_马萍
web前端爱好者。
展开
-
231./ 326. /342. Power of Four/ Three /Two
Given an integer (signed 32 bits), write a function to check whether it is a power of 4/3/2原创 2016-12-01 19:01:46 · 228 阅读 · 0 评论 -
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. 这个没什么还说的,按位计算。突然想到还可能有一个方法,按照splict把字符串分开,取最长的。哈哈/** * @param {number[]} nums * @return {number} */var findMaxConse原创 2017-04-03 16:15:32 · 198 阅读 · 0 评论 -
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 of a 32-b原创 2017-04-03 13:20:36 · 204 阅读 · 0 评论 -
448. 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原创 2017-01-11 20:01:13 · 209 阅读 · 0 评论 -
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. 拿到这个题目感觉会比较简单,但是看着自己写的内容觉得步骤有点多啊~/** * @param {strin原创 2017-03-16 13:56:33 · 207 阅读 · 0 评论 -
461. 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.原创 2017-03-16 15:57:40 · 354 阅读 · 0 评论 -
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 nums2. T原创 2017-04-03 16:46:38 · 200 阅读 · 0 评论 -
190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110原创 2017-03-18 14:52:24 · 251 阅读 · 0 评论 -
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 0000000000000原创 2017-03-16 18:44:29 · 184 阅读 · 0 评论 -
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 contest”原创 2017-04-19 22:14:28 · 183 阅读 · 0 评论 -
520. Detect Capital
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-04-19 22:34:18 · 228 阅读 · 0 评论 -
104. 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 bina原创 2017-04-19 23:36:34 · 165 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 求二叉树最小深度,注意是叶子节点距离跟节点的最小深度。/** * Defin原创 2017-04-19 23:57:17 · 155 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. 题目解读:给出一个字符串的最长连续不重复字符串。注意,dvdf 给出的结果是3! 所以基本思想就是:/** * @param {string} s * @return {number} */var len原创 2017-04-20 15:39:15 · 206 阅读 · 0 评论 -
258. 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 d原创 2017-04-20 23:14:40 · 264 阅读 · 0 评论 -
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 of i原创 2017-04-23 14:46:33 · 244 阅读 · 0 评论 -
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 数字 字母 1 -> A 2 -> B 26 -> Z 27 -> AA 这题目使用递归就可以,但需要注意数组不是从0开始的。/原创 2017-04-23 21:53:36 · 304 阅读 · 0 评论 -
503. Next Greater Element II
Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first grea原创 2017-04-03 17:12:38 · 250 阅读 · 0 评论 -
226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1 这个是二叉树,先去复习一下二叉树的遍历方法:前序、中序、后序遍历都采用的是递归函数的方法,那么这里调换左右树枝也可以采用递归函数的方法。/** * Def原创 2017-04-03 22:01:31 · 136 阅读 · 0 评论 -
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. 因为是二叉查找树,所以比跟节点大的都在右边,比跟节点小的都在左边,这样子找起来就会方便些。/** * Definition for a binary tree node. * funct原创 2017-04-08 23:40:45 · 133 阅读 · 0 评论 -
leetcode刷题记录
这个月开始刷leetcode,很认真的写,完了还纪录note,今天才发现原来写过的题目是不会保存的啊!所以想在这里记录一下,也算一种监督学习和进步吧~原创 2016-11-30 19:58:12 · 293 阅读 · 0 评论 -
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),原创 2016-12-02 22:34:28 · 199 阅读 · 0 评论 -
263. Ugly Number
Write a program to check whether a given number is an ugly number.原创 2016-12-02 22:57:14 · 177 阅读 · 0 评论 -
66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list.原创 2016-12-03 20:21:18 · 207 阅读 · 0 评论 -
349/350. Intersection of Two Arrays
Given 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 unique. - The原创 2016-12-04 16:12:03 · 175 阅读 · 0 评论 -
387. 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原创 2016-12-04 16:51:11 · 182 阅读 · 0 评论 -
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 them.原创 2017-04-10 19:02:30 · 151 阅读 · 0 评论 -
344. Reverse String
Write a function that takes a string as input and returns the string reversed. Example: Given s = “hello”, return “olleh”. 这个题目如果使用JS的数组就很方便,因为数组有一个reverse 的方法。var reverseString = function(s) {原创 2017-04-10 17:54:13 · 147 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
Given 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 should return indices of the two numbers suc原创 2017-04-10 11:54:31 · 158 阅读 · 0 评论 -
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 your funct原创 2017-04-10 11:14:45 · 147 阅读 · 0 评论 -
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.The order原创 2017-04-10 11:00:42 · 176 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. 这个题目本想采用递归的方法,但是发现当前节点的前一个节点不好获取,所以采用非递归的方法。 在while循环里,每次判断当前节点的下一个节点值是否为所需值,然后删除起来就比较方便了/** * Definition for singly-linke原创 2017-04-09 23:00:50 · 170 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. 这个题目采用了两种方法,一种递归,一种循环,其实原理都差不多。就是一个个找下一个的值是否和当前节点的值相同,若相同则删除,若不同,则继续检查下一个节点。//方法一var deleteDuplicates = fu原创 2017-04-09 21:04:14 · 278 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 注意这里是普通的二叉树,没有顺序的,所以就要老老实实的查找。 如果跟节点和要查找的节点为空,就返回空,如果要查找的节点不存在于左边,就在右边找,如果不存在于右边,就在左边找,否则就是一左一右,那么就返回当前跟节原创 2017-04-08 23:51:53 · 151 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这个题目和 108 很像,只是108本身就是有序数组转化为平衡二叉树,这里是链表转化为平衡二叉树,所以首先的想法就是把有序链表转化为有序数组,在使用 108 的函数得到答案。原创 2017-04-09 16:23:11 · 335 阅读 · 0 评论 -
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. 题目是说将一个排好序的数组变成一个平衡二叉树,那么采用递归的思想就是: 当数组为空,则返回空,当数组长度为1,则返回这一个元素所创造的TreeNode,当数组长度大于1,那么就将数组分成两个部分,原创 2017-04-09 12:54:12 · 249 阅读 · 0 评论 -
171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.For example: 数字 字母 A -> 1 B -> 2 Z -> 26 AA -> 27 这题目使用递归就可以,但需要注意数组不是从0开始的。/**原创 2017-04-23 22:53:22 · 268 阅读 · 0 评论