![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
刷遍数
sjphiChina
代码是最为耐心、最能忍耐和最令人愉快的伙伴,在任何艰难困苦的时刻,它都不会抛弃你。
展开
-
Best Time to Buy and Sell Stock
public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE; //int max = Integer.MIN_VALUE; int max = 0; for (int p: prices) { min = Math.min(min, p);原创 2016-06-26 07:50:31 · 212 阅读 · 0 评论 -
Two Sum
public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums原创 2016-06-26 08:02:31 · 223 阅读 · 0 评论 -
Valid Parentheses
public class Solution { public boolean isValid(String s) { if (s == null || s.length() % 2 == 1) { return false; } Stack stack = new Stack<>(); for (int原创 2016-06-26 08:44:45 · 186 阅读 · 0 评论 -
Add Two Numbers
一定要小心,不能互相影响// carry = (num + carry)/10;// num = (num + carry)%10; int res = num + carry; carry = res/10; num = res%10;public ListNode addTwoNumbers(ListNode l原创 2016-06-26 09:16:48 · 276 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
public class Solution { public int lengthOfLongestSubstring(String s) { if (s == null || s.length() < 1) { return 0; } char[] chars = s.toCharArray(); i原创 2016-06-26 09:35:41 · 207 阅读 · 0 评论 -
Longest Palindromic Substring
public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; } if (s.length() == 1) { ret原创 2016-06-26 09:58:20 · 221 阅读 · 0 评论 -
Merge Two Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode merg原创 2016-06-26 12:33:05 · 198 阅读 · 0 评论 -
Trapping Rain Water
public class Solution { public int trap(int[] height) { if (height == null || height.length < 3) { return 0; } int start = 0, end = height.length - 1; i原创 2016-06-26 13:05:08 · 229 阅读 · 0 评论 -
Rotate Image
置换规律:参考点击打开链接int offset = i - start;public class Solution { public void rotate(int[][] matrix) { int start = 0, end = matrix.length - 1; while (start < end) { for (原创 2016-06-23 07:26:34 · 262 阅读 · 0 评论 -
Gray Code
public class Solution { public List grayCode(int n) { List list = new LinkedList<>(); list.add(0); for (int i = 0; i < n; i++) { int highbit = 1 << i;原创 2016-06-26 13:46:57 · 200 阅读 · 0 评论 -
Validate Binary Search 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-06-26 14:09:22 · 202 阅读 · 0 评论 -
Binary Tree Level Order Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-06-26 14:19:02 · 181 阅读 · 0 评论 -
Word Ladder
public class Solution { public int ladderLength(String beginWord, String endWord, Set wordList) { Queue queue = new LinkedList<>(); WordNode wn = new WordNode(beginWord, 1);原创 2016-06-26 14:40:58 · 234 阅读 · 0 评论 -
Word Break
public class Solution { public boolean wordBreak(String s, Set wordDict) { boolean[] res = new boolean[s.length() + 1]; res[0] = true; for (int i = 0; i < s.length(); i++)原创 2016-06-26 14:53:07 · 220 阅读 · 0 评论 -
Word Ladder II
public class Solution { public List> findLadders(String beginWord, String endWord, Set wordList) { List> res = new LinkedList<>(); Set visited = new HashSet<>(); Set unVisi原创 2016-06-26 15:19:09 · 342 阅读 · 0 评论 -
Linked List Cycle
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class原创 2016-06-26 16:00:44 · 156 阅读 · 0 评论 -
Copy List with Random Pointer
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * };原创 2016-06-26 16:14:10 · 199 阅读 · 0 评论 -
LRU Cache
定义的Node中的key是有用的:用于从map中删掉the least used Node。public class LRUCache { private class Node{ Node prev; Node next; int value; int key; public Node(int key,原创 2016-06-26 17:03:38 · 249 阅读 · 0 评论 -
MinStack
public class MinStack { Stack stack; Stack miniStack; /** initialize your data structure here. */ public MinStack() { stack = new Stack<>(); miniStack = new Stack<>()原创 2016-06-26 17:16:02 · 265 阅读 · 0 评论 -
Intersection of Two Linked Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public原创 2016-06-26 17:34:01 · 244 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree
碰见了就刷一次,去吃午饭/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public clas原创 2016-06-12 03:51:31 · 205 阅读 · 0 评论 -
Count and Say
public class Solution { public String countAndSay(int n) { if (n <= 0) { return ""; } String res = "1"; int num = 1; for (int i = 0; i < n - 1;原创 2016-07-09 19:26:52 · 173 阅读 · 0 评论 -
Integer to English Words
public class Solution { private final String[] less20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "原创 2016-07-09 20:34:35 · 193 阅读 · 0 评论 -
One Edit Distance
public class Solution { public boolean isOneEditDistance(String s, String t) { int sl = s.length(), tl = t.length(); if (sl == tl) { return isModified(s, t); }原创 2016-07-09 20:46:55 · 220 阅读 · 0 评论 -
Number of Islands
public class Solution { public int numIslands(char[][] grid) { int num = 0; if (grid == null || grid.length == 0 || grid[0].length == 0) { return num; }原创 2016-07-09 21:38:59 · 205 阅读 · 0 评论 -
Word Search
public class Solution { public boolean exist(char[][] board, String word) { if (board == null || board.length == 0 || board[0].length == 0 || word == null || word.length() == 0) {原创 2016-07-09 22:01:14 · 184 阅读 · 0 评论 -
Sparse Matrix Multiplication
public class Solution { public int[][] multiply(int[][] A, int[][] B) { int row = A.length, column = B[0].length; int[][] res = new int[row][column]; for (int i = 0; i < ro原创 2016-07-09 22:20:50 · 242 阅读 · 0 评论 -
Validate Binary Search 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-07-09 22:36:38 · 174 阅读 · 0 评论 -
Binary Search Tree Iterator
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class BSTIterator {原创 2016-07-09 23:01:35 · 216 阅读 · 0 评论 -
LRU Cache
public class LRUCache { private class Node { int key; int val; Node next; Node prev; public Node(int key, int val) { this.key = key;原创 2016-07-09 23:39:56 · 239 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary 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-07-10 00:45:31 · 203 阅读 · 0 评论 -
Implement strStr()
public class Solution { public int strStr(String haystack, String needle) { if (haystack == null || needle == null) { return -1; } for (int i = 0; i <= haystack原创 2016-07-10 00:59:32 · 231 阅读 · 0 评论 -
Implement Trie (Prefix Tree)
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isExisted;}public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } //原创 2016-07-10 12:10:28 · 206 阅读 · 0 评论 -
Add and Search Word - Data structure design
public class WordDictionary { private class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isExisted; } private TrieNode root; public WordDictionary() {原创 2016-07-10 12:39:06 · 183 阅读 · 0 评论 -
Insert Interval
/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */原创 2016-07-10 16:28:07 · 179 阅读 · 0 评论 -
Binary Tree Vertical Order Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-07-10 16:53:14 · 209 阅读 · 0 评论 -
Expression Add Operators
public class Solution { public List addOperators(String num, int target) { List res = new LinkedList<>(); helper(res, num, target, "", 0, 0); return res; } p原创 2016-07-10 18:05:09 · 226 阅读 · 0 评论 -
Read N Characters Given Read4
/* The read4 API is defined in the parent class Reader4. int read4(char[] buf); */public class Solution extends Reader4 { /** * @param buf Destination buffer * @param n Maximum原创 2016-07-10 18:28:28 · 162 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE; int max = 0; for (int price: prices) { min = Math.min(min, price);原创 2016-07-10 18:37:23 · 254 阅读 · 0 评论 -
Roman to Integer
public class Solution { public int romanToInt(String s) { if (s == null || s.length() == 0) { return 0; } Map map = new HashMap<>(); map.put('I', 1);原创 2016-07-10 18:54:52 · 164 阅读 · 0 评论