
leetcode For Java
JackZhangNJU
未来的路还很长
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode 225. Implement Stack using Queues 双队列实现栈
Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whet...原创 2017-09-25 10:57:14 · 498 阅读 · 0 评论 -
神奇数字的数量 DFS深度优先搜索
题意如上图,这个题主要就是如何判断一个数字是不是神奇数字,这个技术拆分出所有的位,然后DFS深度优先遍历判断。import java.util.ArrayList;import java.util.List;public class Solution{ /* * 神奇数字的判断 * */ boolean getCount(int num) {原创 2017-09-08 20:19:50 · 328 阅读 · 0 评论 -
leetcode 133. Clone Graph 图遍历BFS + 避免循环
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s undirected graph serialization: Nodes are labeled uniquely.We use # as a separator for each nod...原创 2017-09-17 09:42:55 · 433 阅读 · 0 评论 -
leetcode 138. Copy List with Random Pointer 链表复制 + HashMap
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.本题题意就是复制链表,注意使用HashMap等结构保存相关信息,这样...原创 2017-09-17 10:55:38 · 405 阅读 · 0 评论 -
leetcode 146. LRU Cache 需要深入学习Java的Map的内部实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key if the k原创 2017-09-18 09:15:19 · 1373 阅读 · 0 评论 -
leetcode 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...原创 2017-09-25 11:30:21 · 420 阅读 · 0 评论 -
leetcode 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 t...原创 2017-09-18 10:29:21 · 1196 阅读 · 0 评论 -
leetcode 284. Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that will be retur...原创 2017-09-28 09:42:43 · 321 阅读 · 0 评论 -
leetcode 173. Binary Search Tree Iterator 二叉树中序遍历非递归
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and...原创 2017-09-20 09:05:08 · 356 阅读 · 0 评论 -
leetcode 43. Multiply Strings 字符串乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.Note:The length of both num1 and num2 is < 110. Both num1 and num2 contains only digit...原创 2017-09-03 15:39:52 · 391 阅读 · 0 评论 -
leetcode 44. Wildcard Matching (需要好好想一下)
Implement wildcard pattern matching with support for ‘?’ and ‘*’.‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence).The matching should cover ...原创 2017-09-03 16:23:27 · 456 阅读 · 0 评论 -
leetcode 303. Range Sum Query - Immutable 字串求和 + DP
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 su...原创 2017-09-29 10:27:14 · 443 阅读 · 0 评论 -
leetcode 304. Range Sum Query 2D - Immutable 子矩阵求和 + DP
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Range Sum Query 2D The above rectang...原创 2017-09-29 10:31:34 · 451 阅读 · 0 评论 -
leetcode 211. Add and Search Word - Data structure design 字典树的简单应用
Design a data structure that supports the following two operations:void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letter...原创 2017-09-21 13:02:21 · 387 阅读 · 0 评论 -
leetcode 212. Word Search II 单词矩阵搜索 + DFS + 字典树
Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horiz...原创 2017-09-21 13:18:10 · 936 阅读 · 0 评论 -
leetcode 112. Path Sum DFS深度优先遍历
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and sum = 2原创 2017-09-13 09:58:16 · 523 阅读 · 0 评论 -
leetcode 114. Flatten Binary Tree to Linked List 扁平化二叉树+前序遍历
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3原创 2017-09-14 12:40:52 · 391 阅读 · 0 评论 -
leetcode 116. Populating Next Right Pointers in Each Node BFS广度优先遍历
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,原创 2017-09-14 13:13:34 · 368 阅读 · 0 评论 -
leetcode 118. Pascal's Triangle 杨辉三角形
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]这个就是中国最伟大的杨辉三角形的问题。代码如下:import原创 2017-09-14 13:20:31 · 311 阅读 · 0 评论 -
leetcode 15. 3Sum 以及2Sum的问题的处理和求解
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: The solution set must not contain duplic原创 2017-08-30 16:27:00 · 398 阅读 · 0 评论 -
leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly原创 2017-08-31 14:33:25 · 306 阅读 · 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.Seen this question in a real interview before? Yes简单的归并排序,原创 2017-08-31 16:40:22 · 245 阅读 · 0 评论 -
leetcode 22. Generate Parentheses DFS深度优先遍历
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ “((()))”, “(()())”, “(())()”, “()(())”,原创 2017-08-31 16:59:28 · 465 阅读 · 0 评论 -
leetcode 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.提议很简单,就是归并排序。首先想到的即使逐个归并得到最终的结果,但是会超时,这是因为这种会造成数组的size大小不一样,导致归并排序的时间变长;最好的做法是两两合并,然后在两两合并,这样不会超时, 需要注原创 2017-08-31 17:19:41 · 406 阅读 · 0 评论 -
leetcode 119. Pascal's Triangle II 杨辉三角形2
Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].就是中国最伟大的杨辉三角形。代码如下:import java.util.ArrayList;import java.util.List;public class So原创 2017-09-14 13:22:55 · 324 阅读 · 0 评论 -
leetcode 120. Triangle 杨辉三角形3 + BFS广度优先遍历
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle [ [2], [3,4], [6原创 2017-09-14 13:29:56 · 499 阅读 · 0 评论 -
leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no原创 2017-08-31 17:44:53 · 297 阅读 · 0 评论 -
leetcode 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nod原创 2017-08-31 19:33:42 · 295 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array 堆排序的简单应用 + 快速排序
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.这...原创 2017-09-22 09:53:48 · 734 阅读 · 0 评论 -
leetcode 217. Contains Duplicate 遍历 + HashSet
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2017-09-22 10:04:33 · 396 阅读 · 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 extra me原创 2017-09-15 11:01:17 · 392 阅读 · 0 评论 -
leetcode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you i原创 2017-09-17 10:42:55 · 547 阅读 · 0 评论 -
leetcode 143. Reorder List 双指针
Given a singly linked list L: L0?L1?…?Ln-1?Ln, reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}原创 2017-09-17 13:34:24 · 411 阅读 · 0 评论 -
leetcode 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort.本题就是要做链表的插入排序。代码如下:/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}*//* * 链表的插入排序 * */public class Solution {原创 2017-09-15 11:09:42 · 366 阅读 · 0 评论 -
leetcode 148. Sort List 链表归并排序
Sort a linked list in O(n log n) time using constant space complexity.本题就是考察的是链表的归并排序。代码如下:/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}*/public class So原创 2017-09-18 09:26:52 · 595 阅读 · 0 评论 -
leetcode 150. Evaluate Reverse Polish Notation 逆波兰表达式的计算
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: [“2”, “1”, “+”, “3”, ““]原创 2017-09-18 09:39:23 · 499 阅读 · 0 评论 -
leetcode 28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串,我这里是直接循环判断的。代码如下:public class Solut原创 2017-09-01 17:07:12 · 242 阅读 · 0 评论 -
leetcode 29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这种题我不喜欢,这个是网上查阅的答案,这道题就这样吧。代码如下:public class Solution { public int divide(int dividend,原创 2017-09-01 17:33:20 · 336 阅读 · 0 评论 -
leetcode 30. Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wit原创 2017-09-02 15:21:14 · 353 阅读 · 0 评论 -
leetcode 31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde原创 2017-09-02 15:53:12 · 300 阅读 · 0 评论