- 博客(27)
- 资源 (2)
- 收藏
- 关注
原创 Reverse Linked List
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路:本来想到借用stack来实现逆转,但是 Memory Limit Exc
2015-06-30 19:42:28 234
原创 Contains Duplicate
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
2015-06-30 19:08:29 203
原创 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before c
2015-06-25 19:26:22 256
原创 Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 思路:简单说就是找两个sorted arrays合
2015-06-24 18:35:40 280
转载 startActivityForResult和setResult详解
startActivityForResult与startActivity的不同之处在于: 1、startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( )。 2、startActivityForResult( Intent,requestCode) 可以一次性完成这项任务,当程序执行到这段代码的时候,假若从T1Activi
2015-06-18 20:45:32 312
原创 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
2015-06-18 15:16:55 251
原创 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
2015-06-18 10:20:25 296
原创 Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. For
2015-06-17 15:06:30 267
原创 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 典型的DFS,循环递归,典型的递归回溯法,多做形成这个递归回溯头脑,就好办了。 1 递归一次,填入一个数字 2 填入的数字,不能是小于当前数字的值,防止重复 3 回溯:记得pop_back()最后
2015-06-17 09:26:27 265
原创 Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 思路:本题常规思路就是利用二分查找的思想,一个数x的平方根小于x/2+1,所以只需在这0到x/2+1这个范围二分查找一个数,使得平方后等于(小于但很接近,毕竟返回值是int,不可能太精确) 值得注意的细节是:如果用mid*mid==x作为判断条件
2015-06-16 16:44:11 386
原创 Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路:和之前的Add Two Numbers那题很像,注意进位的处理就行,而且这个要倒着相加,那个不用倒着。 public class Solution {
2015-06-15 18:19:07 346
原创 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 首先简单介绍一下Anagram(回文构词法)。Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty ro
2015-06-14 10:29:54 440
原创 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 值
2015-06-14 09:49:01 252
原创 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. Y
2015-06-13 17:11:49 189
原创 Merge k sorted linked lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Hide Tags Divide and Conquer Linked List Heap 首先容易想到的就是首先容易想到的就是讲链表里的所有数都遍历一遍存到一个list中,再对list
2015-06-13 16:35:51 305
原创 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: "((()))", "(()())", "(())()", "()(())", "()()
2015-06-12 10:06:07 348
转载 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的之间给定的数字用罗马数字的形式表示。思路罗马数字表示其实就是相加的过程,例如1689 = 1000+500+100+50+3个10+(10-1)即MD
2015-06-11 09:37:22 234
原创 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link
2015-06-10 09:57:00 211
转载 Validate Binary Search Tree
这哥们总结的全面到位,就看他的吧 【题目】 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with
2015-06-10 00:50:50 269
原创 Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold add
2015-06-09 09:26:26 248
原创 Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution using O(m
2015-06-09 09:14:33 570
原创 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 动态规划dp问题,其实就是斐波那契数列问题,f(1)=1
2015-06-08 08:51:30 214
转载 Valid Number
Valid Number Total Accepted: 26805 Total Submissions: 233843My Submissions Question Solution Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true
2015-06-05 09:58:51 406
转载 赛马问题
据说,这是Google的面试题。面试题目如下: 一共有25匹马,有一个赛场,赛场有5个赛道,就是说最多同时可以有5匹马一起比赛。假设每匹马都跑的很稳定,不用任何其他工具,只通过马与马之间的比赛,试问,最少得比多少场才能知道跑得最快的5匹马?(不能使用撞大运的算法) 很明显这是一个算法题,网上有很多贴子在讨论这个问题,不过都没有给出一个明确的答案。我想了想,想到下面的一个算法: 1)分成5
2015-06-05 09:43:01 449
原创 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. E
2015-06-05 08:56:05 545
转载 Java 中Comparable和Comparator区别比较
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明 Comparable 简介 Comparable 是排序接口。 若一个类实现了Comparable接口,就意味着“该类支持排序”。 即然实现Comparable接口的类支持排序,假设现在存在“实现Comparable接口的类的对象的List列表(或数组)”,则该
2015-06-04 09:06:31 337
原创 Pow(x, n)
Pow(x, n) Implement pow(x, n). 思路:直接for循环,效率太低,可以用剑指offer的P93页的方法用递归的方法,或者另一个思路 Consider the binary representation of n. For example, if it is "10001011", then x^n = x^(1+2+8+128) = x^1 * x^2
2015-06-03 13:00:14 299
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人