[置顶] 判断一个单链表是否有环，如果有，找出环的起始位置 [No. 36]

How can one determine whether a singly linked list has a cycle? 第一种方法是从单链表head开始，每遍历一个，就把那个node放在hashset里，走到下一个的时候，把该node放在hashset里查找，如果有相同的，就表示有环，如果走到单链表最后一个node，在hashset里都没有重复的node，就表示没有环。 这种方法需要O(...

Sum of a tree

You are given a tree, and the nodes in the tree may have more than two child nodes, calculate the sum of the tree where the root to the leaf is considered as a number. public class TreeSum { priv...

Group of 1s in a Matrix

Given a matrix with 1s and 0s, please find the number of groups of 1s. A group is defined by horizontally or vertically adjacent 1s. For example, there are four groups of 1s in figure below. Anal...

Split string to palindrome

Question: Give a string, use minimum number of splits to divide the string into multiple parts in which each part a palindrome. For example, if the string is "abbad", we can split it into "abba" and...

O变X

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order do...

Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",...

Word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n...

Best Time to Buy and Sell Stock III

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may no...

Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be n...

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 number) { int[] values...

Sort Colors

Question: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the...

Spiral Matrix

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [...

Search for a Range

Given a sorted array of integers, 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). If the target is not found in t...

Minimum Path Sum

Question: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either dow...

Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total numb...

Closest node to the target in BST

Question: Give a BST, find a node whose value is closest to the target. public static int closestValue(Node root, int value, int maxDiff) { if (root == null) return Integer.MAX_VALUE; int tempDiff...

n 个人围成一圈（编号1-n），数到3的出列，最后剩下的人的编号

143条 共8页1 2 3 4 5 ... 下一页 尾页
个人资料
• 访问：598936次
• 积分：6544
• 等级：
• 排名：第4059名
• 原创：122篇
• 转载：21篇
• 译文：0篇
• 评论：177条
文章分类
评论排行
最新评论