![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode算法
freya0608
我爱学习。
展开
-
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string`""`.Example 1:Input: ["flower","flow","flight"]Outpu...原创 2019-11-23 10:28:50 · 106 阅读 · 0 评论 -
leetcode 118. Pascal's Triangle
Given a non-negative integer*numRows*, generate the first*numRows*of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it.Input: 5Output:...原创 2019-11-21 13:45:13 · 102 阅读 · 0 评论 -
8. String to Integer (atoi)
mplement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.No转载 2016-04-26 20:55:18 · 200 阅读 · 0 评论 -
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: Elements in a quad转载 2016-04-26 20:51:58 · 192 阅读 · 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转载 2016-04-26 20:51:01 · 213 阅读 · 0 评论 -
58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.public class Solution {转载 2016-04-26 20:50:05 · 201 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.public class Solution { public String addBinary(String a, String b) { //int a转载 2016-04-26 20:48:49 · 330 阅读 · 0 评论 -
88. Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional转载 2016-04-25 21:09:56 · 190 阅读 · 0 评论 -
101. Symmetric Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution转载 2016-04-25 21:08:46 · 186 阅读 · 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 binary tree node.转载 2016-04-25 21:07:22 · 184 阅读 · 0 评论 -
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by转载 2016-04-25 21:06:29 · 197 阅读 · 0 评论 -
15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: Elements in a triplet (a,b,c) must be i转载 2016-04-26 20:52:43 · 298 阅读 · 0 评论 -
13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public int romanToInt(String s) { Map<Character, Integer> m转载 2016-04-26 20:53:31 · 199 阅读 · 0 评论 -
12. Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.public class Solution { public String intToRoman(int num) { Map<Integer, String> ma转载 2016-04-26 20:54:26 · 227 阅读 · 0 评论 -
Js-leetcode 867. Transpose Matrix
Given a matrix A, return the transpose of A.The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.Example 1:Input: [[1,2,3]...转载 2018-09-28 16:07:26 · 128 阅读 · 0 评论 -
344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".Subscribe to see which companies asked this questionpublic class转载 2016-09-03 23:41:59 · 235 阅读 · 0 评论 -
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the转载 2016-04-25 09:09:18 · 170 阅读 · 0 评论 -
169. 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转载 2016-04-25 09:07:53 · 172 阅读 · 0 评论 -
171. Excel Sheet Column Number
public class Solution { public int titleToNumber(String s) { int result = 0; for (int i = 0; i < s.length(); i++) { result = result * 26 + (s.charAt(i)转载 2016-04-24 14:13:54 · 173 阅读 · 0 评论 -
172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.public class Solution { public int trailingZeroes(int n) { int re转载 2016-04-24 14:10:53 · 203 阅读 · 0 评论 -
189. Rotate Array
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,4,5,6,7] is rotated to [5,6,7,1,2,3,4].public class Solution { public void rotate(int[] num转载 2016-04-24 14:09:23 · 191 阅读 · 0 评论 -
7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321public class Solution { public int reverse(int x) { int negative=1; //负数先转正,用negative保存其转载 2016-04-26 20:55:55 · 204 阅读 · 0 评论 -
118. Pascal's Triangle
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if(numRows == 0) return result;转载 2016-04-25 21:04:20 · 186 阅读 · 0 评论 -
119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> li转载 2016-04-25 21:03:05 · 201 阅读 · 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), desi转载 2016-04-25 21:01:44 · 196 阅读 · 0 评论 -
231. Power of Two
Given an integer, write a function to determine if it is a power of two.public class Solution { public boolean isPowerOfTwo(int n) { int x=0; while(n>0){ x += (n&1);原创 2016-03-11 00:30:11 · 175 阅读 · 0 评论 -
232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. empty()原创 2016-03-09 20:14:33 · 233 阅读 · 0 评论 -
234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1) space?/** * Definition for singly-linked list. * public class ListNode { * int val;原创 2016-03-09 00:01:11 · 194 阅读 · 0 评论 -
237. 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原创 2016-03-07 21:52:35 · 180 阅读 · 0 评论 -
242. 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.public class Solution {原创 2016-03-06 22:57:56 · 151 阅读 · 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 digit, r原创 2016-03-05 21:40:47 · 165 阅读 · 0 评论 -
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 since it in原创 2016-03-03 10:01:21 · 215 阅读 · 0 评论 -
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原创 2016-03-03 00:30:40 · 243 阅读 · 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原创 2016-03-03 00:24:26 · 165 阅读 · 0 评论 -
231. Power of Two(2)
Given an integer, write a function to determine if it is a power of two.public class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && ((n & (n - 1)) == 0 ); } }转载 2016-03-11 00:40:24 · 161 阅读 · 0 评论 -
226. Invert Binary Tree
Invert a binary tree. 4/ \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: Go转载 2016-03-11 23:12:23 · 185 阅读 · 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转载 2016-04-06 16:45:48 · 157 阅读 · 0 评论 -
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not a palin转载 2016-04-25 21:00:37 · 199 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//** * Definition for a bi转载 2016-04-25 20:59:42 · 180 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//** * Definition for a bi转载 2016-04-25 20:57:47 · 168 阅读 · 0 评论