转载请注明出处:z_zhaojun的博客
原文地址:http://blog.csdn.net/u012975705/article/details/50408975
题目地址:https://leetcode.com/problems/reverse-linked-list-ii/Reverse Linked List IIReverse a linked list from position m to n.Do it in-placeandin one-pass.For example:
Given 1->2->3->4->5->NULL, m =2and n =4,return1->4->3->2->5->NULL.Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ ...
题目描述题干:
给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。
如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。
更正式地说,root.val = min(root.left.val, root.right.val) 总成立。
给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。示例1:
输入:root = [2,2,5,null,null,5,7]
输出:5
解释:最小的值是 2...
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return[["aa","b"],["a","a","b"]
]
递归求解,没什么难度。publicclass Solution {List list = new ArrayList<ArrayList<String>>();char[] word ;String ss;String[] result;public List<List<String>> partition(String s) {int len = s.length();...
题目描述:You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong pos...
Problem:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2...
题目:https://leetcode-cn.com/problems/length-of-last-word/submissions/本题比较简单,只需从字符串的最后一个字符向前遍历即可,但是需要注意空格,不管末尾有多少个空格,从后向前遍历的时候从第一个非空格的字符开始计数,直到再次遇见空格,退出循环。 代码:class Solution {publicint lengthOfLastWord(String s) {if(s.length()==0) return 0;int sum=0,f=0; //sum用来记录最后一个单词长度,f用...
题目描述:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button to reset your code definition.
解题思路:从开头开始,...
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 node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.The graph has a total of three nodes, and therefore contains three parts...
题目:接雨水难度:hard题目内容:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!Example:Input: [0,1,0,2,1,0,1,3...
题目:有一堆石头,每块石头的重量都是正整数。每一回合,从中选出两块最重的石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:如果 x == y,那么两块石头都会被完全粉碎;如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 0。 提示:1 <= stones.length <= 301 <= stones[i] ...
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode035. Search for a Range (Medium) 链接:题目:https://leetcode.com/problems/search-for-a-range/
代码(github):https://github.com/illuz/leetcode题意:在有序数组中找到一个数的范围。(由于数有反复)分析:还是二分搜索变形。(C++)直接用 C++ STL 的 lower_bound 和 upper_bound 偷懒。(Java)直接从普通的二分改一下...
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3. 原始思路:保存前一位的指针,这样在重复的时候,可以直接用next指向新的位置,需要注意的是开始时候,需要进行额外的判断是否有重复,才能形成错位的指针。原始代码:publicclass Solution {p...
题目:
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
Only one letter can be changed at a timeEach intermediate word must exist in the dictionary
For example,Given:start = "hit"end = "cog"dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its...
【139-Word Break(单词拆分)】【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because "leetcode" can be segmented as "leet code". 题目大意 给定一个字符串s和单词字典...
题目: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 line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
题解: 这道题挺类似二分查找法的,这道题是先从两头开始...