LeetCode刷题
文章平均质量分 61
lemon2631
这个作者很懒,什么都没留下…
展开
-
java之yield(),sleep(),wait()区别
原文链接:http://dylanxu.iteye.com/blog/13220661、sleep()使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁。也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据。注意该方法要捕捉异常。例如有两个线程同时执行(没有synchronized)一个线程优先级为MAX_PRIORIT转载 2016-09-30 11:24:09 · 156 阅读 · 0 评论 -
LeetCode-3. Longest Substring Without Repeating Characters
1.问题描述:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer i原创 2016-10-27 21:44:49 · 181 阅读 · 0 评论 -
LeetCode
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2016-10-27 22:01:55 · 170 阅读 · 0 评论 -
理解Java对象序列化
理解Java对象序列化原文连接:http://www.blogjava.net/jiangshachina/archive/2012/02/13/369898.html关于Java序列化的文章早已是汗牛充栋了,本文是对我个人过往学习,理解及应用Java序列化的一个总结。此文内容涉及Java序列化的基本原理,以及多种方法对序列化形式进行定制。在撰写本文时,既参考了Thinking转载 2016-12-27 10:01:55 · 180 阅读 · 0 评论 -
LeetCode--8 String to Integer
一、问题描述Implement 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 in原创 2016-12-20 17:43:15 · 238 阅读 · 0 评论 -
LeetCode-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.二、解题思路将1-3999之内的任一数字转化成罗马字符形式。罗马数字共有7个,即Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、Ⅼ(50)、Ⅽ(100)、Ⅾ(500)和Ⅿ(1原创 2017-02-06 19:25:45 · 194 阅读 · 0 评论 -
LeetCode-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.二、解题思路将1-3999范围内的罗马数字转化成阿拉伯数字。罗马数字共有7个,即Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、Ⅼ(50)、Ⅽ(100)、Ⅾ(500)和Ⅿ(1原创 2017-02-06 19:49:08 · 153 阅读 · 0 评论 -
LeetCode-9 Palindrome Number
一、题目描述:Determine whether an integer is a palindrome. Do this without extra space.二、解题思路:其实就是将数字通过取余的方式倒置,然后和原数字相比较,如果相等则是回文数字三、源代码public class Solution { public boolean isPalindrome(int原创 2017-01-16 21:10:46 · 153 阅读 · 0 评论 -
LeetCode-11 Container With Most Water
一、算法概述Given n non-negative integers a1,a2, ..., an, where each represents a point at coordinate (i,ai). n vertical lines are drawn such that the two endpoints of linei is at (i, ai) and (i, 0)原创 2017-01-18 09:21:12 · 165 阅读 · 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原创 2017-02-17 19:51:22 · 255 阅读 · 0 评论 -
LeetCode-26. Remove Duplicates from Sorted Array
一、问题描述Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in pla原创 2017-02-18 16:26:53 · 169 阅读 · 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 m原创 2017-02-18 16:55:41 · 185 阅读 · 0 评论 -
程序员面试金典-4.2有向路径检查
一、问题描述对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径。给定途中的两个节点的指针UndirectedGraphNode*a,UndirectedGraphNode* b(请不要在意数据类型,图是有向图),请返回一个bool,代表两点之间是否存在一条路径(a到b或b到a)。二、思路分析有向图的路径查找一般分为深度优先遍历和广度优先遍历,深度优先遍历用堆栈或者递归来实原创 2017-02-28 09:47:55 · 225 阅读 · 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.二、问题分析求解haystack字符串中最先出现needle字符串的位置,变量 i 代表haystack字符串字符的位原创 2017-02-18 19:24:46 · 152 阅读 · 0 评论 -
LeetCode-29. Divide Two Integers
一、问题描述Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 二、问题分析写一个算法不使用乘法、除法和取余运算来实现除法运算。首先想到的是被除数不断的减去除数,并不断的判断新的被除数和除数的大小来实现,但是如果原创 2017-02-19 15:37:26 · 326 阅读 · 0 评论 -
LeetCode-14. Longest Common Prefix
一、问题描述Write a function to find the longest common prefix string amongst an array of strings.求字符串数组中的最大前缀。二、解题思路1.判断字符串数组是不是空数组或者是否为null,是则返回空字符串。2.以字符串数组中的第1个字符串strs[0]作为预设前缀pre,然后从strs[1]开始原创 2017-02-10 13:42:12 · 165 阅读 · 0 评论 -
程序员面试金典-4.3高度最小的BST
一、题目描述对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树。给定一个有序序列int[] vals,请返回创建的二叉查找树的高度。二、题目思路可以利用递归构建起整棵树,然后再计算数的高度;或者不去构建树,直接利用递归去求解高度。三、代码import java.util.*;class TreeNode{ int val;原创 2017-02-28 19:30:27 · 232 阅读 · 0 评论 -
程序员面试金典-4.4 输出单层节点
一、题目描述对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表。 给定二叉树的根结点指针TreeNode* root,以及链表上结点的深度,请返回一个链表ListNode,代表该深度上所有结点的值,请按树上从左往右的顺序链接,保证深度不超过树的高度,树上结点的值为非负整数且不超过100000。二、解题思路1. 如上图所示,如果说4,5,6,7这一层是待求的高度,节原创 2017-02-28 22:03:58 · 295 阅读 · 0 评论 -
LeetCode-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.二、解题思路求解int数组中满足三数之和为0的组合。首先判断数组原创 2017-02-11 19:39:12 · 187 阅读 · 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 ha原创 2017-02-12 11:18:10 · 156 阅读 · 0 评论 -
LeetCode-17. Letter Combinations of a Phone Number
一、问题描述Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit原创 2017-02-12 16:24:17 · 149 阅读 · 0 评论 -
Java变量类型间的相互转换
原文地址:http://blog.sina.com.cn/s/blog_7014ad5c01019s3e.html我们知道,Java的数据类型分为三大类,即布尔型、字符型和数值型,而其中数值型又分为整型和浮点型;相对于数据类型,Java的变量类型为布尔型boolean;字符型char;整型byte、short、int、long;浮点型float、double。其中四种整型变量和两种浮点型变量分转载 2017-02-12 16:35:34 · 2685 阅读 · 1 评论 -
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) ins that is a concatenation of each word in words exactly on原创 2017-02-21 08:56:56 · 164 阅读 · 0 评论 -
程序员面试金典-4.5检查是否为BST
一、题目描述请实现一个函数,检查一棵二叉树是否为二叉查找树。给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树。二、解题思路1.首先判断一棵树是否为二叉查找树,准则:保证每个节点的左子树值和左子树的右子树的值都小于当前节点的值;每个节点的右子树的值和右子树的左子树的值大于当前节点的值。2.首先想到的是采用递归的方式,从上往下的方式判原创 2017-03-01 17:17:23 · 228 阅读 · 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 po原创 2017-02-21 20:21:02 · 146 阅读 · 0 评论 -
程序员面试金典-4.6寻找下一个节点
一、题目描述请设计一个算法,寻找二叉树中指定结点的下一个结点(即中序遍历的后继)。给定树的根结点指针TreeNode* root和结点的值int p,请返回值为p的结点的后继结点的值。保证结点的值大于等于零小于等于100000且没有重复值,若不存在后继返回-1。二、解题思路1.本题是寻找指定节点的下一个节点(按照中序遍历),首先应该明白什么是中序遍历,后序遍历和前序遍历。原创 2017-03-02 10:31:59 · 257 阅读 · 0 评论 -
LeetCode-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.For exam原创 2017-02-14 11:08:25 · 156 阅读 · 0 评论 -
LeetCode-34. Search for a Range
一、问题描述Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).原创 2017-02-23 09:46:22 · 175 阅读 · 0 评论 -
LeetCode-19. Remove Nth Node From End of List
一、问题描述Given a linked list, remove the nth node from the end of list and return its head.For Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the原创 2017-02-14 17:05:47 · 151 阅读 · 0 评论 -
LeetCode-20. Valid Parentheses
一、问题描述Given a string containing just the characters '(', ')','{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are a原创 2017-02-14 18:40:24 · 155 阅读 · 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 th原创 2017-02-23 10:19:35 · 151 阅读 · 0 评论 -
LeetCode-36. Valid Sudoku
一、问题描述Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.二、问题思路检查每行每列,以原创 2017-02-23 14:48:13 · 188 阅读 · 0 评论 -
程序员面试金典-5.3.最接近的数
一、题目描述有一个正整数,请找出其二进制表示中1的个数相同、且大小最接近的那两个数。(一个略大,一个略小)给定正整数int x,请返回一个vector,代表所求的两个数(小的在前)。保证答案存在。测试2返回:[1,4]样例:二、解题思路1.如果求解比原数字较小的数,首先需要从左往右开始找到0出现后的第一个1出现的位置(因为求的是大小最接近,所以寻找0出现后的第一原创 2017-03-03 11:10:01 · 413 阅读 · 0 评论 -
LeetCode-22. Generate Parentheses
一、问题描述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-02-15 19:42:21 · 153 阅读 · 0 评论 -
程序员面试金典-5.4整数转化
一、问题描述编写一个函数,确定需要改变几个位,才能将整数A转变成整数B。给定两个整数int A,int B。请返回需要改变的数位个数。测试样例:10,5返回:4二、思路描述首先对A和B取异或运算,然后计算该数字中的1的个数,本人采用的思路是先判断这个数字是不是负数,然后诸位的判断是不是1。有种更简单的方法是使用n=n&(n-1)的方法,效率更高原创 2017-03-03 14:27:13 · 225 阅读 · 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.二、解题思路将两个排过序的链表合并成一个链表,没啥好说的,题目很简单三、代码/**原创 2017-02-16 09:56:19 · 145 阅读 · 0 评论 -
程序员面试金典-5.6找出缺失的部分
一、题目描述数组A包含了0到n的所有整数,但其中缺失了一个。对于这个问题,我们设定限制,使得一次操作无法取得数组number里某个整数的完整内容。唯一的可用操作是询问数组中第i个元素的二进制的第j位(最低位为第0位),该操作的时间复杂度为常数,请设计算法,在O(n)的时间内找到这个数。给定一个数组number,即所有剩下的数按从小到大排列的二进制各位的值,如A[0][1]表示剩下的第二原创 2017-03-03 17:16:27 · 425 阅读 · 0 评论 -
程序员面试金典-5.7像素设定
一、问题描述有一个单色屏幕储存在一维数组中,其中数组的每个元素代表连续的8位的像素的值,请实现一个函数,将第x到第y个像素涂上颜色(像素标号从零开始),并尝试尽量使用最快的办法。给定表示屏幕的数组screen(数组中的每个元素代表连续的8个像素,且从左至右的像素分别对应元素的二进制的从低到高位),以及intx,int y,意义如题意所述,保证输入数据合法。请返回涂色后的新的屏幕数组原创 2017-03-03 20:11:39 · 476 阅读 · 0 评论 -
程序员面试金典-8.反转字串
一、题目描述链接:https://www.nowcoder.com/practice/bc12808a2b0f445c96a64406d5513e96?tpId=8&tqId=11001&rp=1&ru=/ta/cracking-the-coding-interview&qru=/ta/cracking-the-coding-interview/question-ranking来源:牛客网原创 2017-02-25 14:57:12 · 407 阅读 · 0 评论 -
程序员面试金典-7.清除行列
一、题目描述链接:https://www.nowcoder.com/profile/5266727/codeBookDetail?submissionId=9096451来源:牛客网请编写一个算法,若N阶方阵中某个元素为0,则将其所在的行与列清零。给定一个N阶方阵int[][](C++中为vector)mat和矩阵的阶数n,请返回完成操作后的int[][]方阵(C++中为vec原创 2017-02-25 15:07:55 · 249 阅读 · 0 评论