- 博客(52)
- 收藏
- 关注
原创 49.Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘
2015-10-27 16:08:22 233
原创 48.Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?分析:题
2015-10-27 16:01:17 247
原创 47.Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?分析:给一个链表判断其中是否有环。思路:分别取两个快慢指针,每次fast走两步,slow走一步,在fast和fast.next不为null的前提下,判断fa
2015-10-27 15:37:34 236
原创 46.Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.分析:题目要求将给定的链表按照向右走k步。Step1:让p
2015-10-26 22:58:26 194
原创 45.Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to
2015-10-25 16:46:26 255
原创 44.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-10-25 12:02:15 243
原创 43.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Difficulty: EasyFollow up:Could you do it in O(n) time and O(1) space?分析:思路是首先把链表分成前后两段(两段长度差0或1),然后转置其中的一段,再比较两个链表的值是否相等。/*
2015-10-24 23:07:02 231
原创 42.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Difficulty: EasyExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5分析,这个题目是删除
2015-10-24 12:24:22 235
原创 41.Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.(Difficulty: Easy)For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node
2015-10-24 11:49:24 220
原创 40.Sqrt(x) (二分查找)
mplement int sqrt(int x).Compute and return the square root of x.分析:
2015-10-23 20:41:15 728
原创 39.Ugly Number II(动态规划)
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first
2015-10-23 12:27:44 407
原创 38.Missing Number
Missing Number Difficulty: MediumGiven an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3
2015-10-23 09:20:55 226
原创 37.Add Binary
Difficulty: EasyGiven two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".分析:这个题目是两个二进制字符串相加,然后返回二进制字符串。方法一:把这两个字符串都转换成整数,然后整数相加后,将结
2015-10-22 22:10:51 210
原创 36.Add Two Numbers (链表) Difficulty: Medium
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-10-22 17:30:12 239
原创 36.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
2015-10-21 16:30:42 298
原创 两种二分查找
二分查找的关键是控制low和high,以及返回值。最后要返回的是low的值比如给定拍好序的数组nums[]和要查找的关键字key,返回关键字key在数组中的下标。假设要查找的关键字在数组中存在。 public int binarySearch(int nums[],int key){ int high = nums.length-1; int low = 0;
2015-10-21 15:42:34 330
原创 35.First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the
2015-10-21 15:32:50 238
原创 34.Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as yo
2015-10-21 10:31:52 284
原创 33.Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
2015-10-20 21:47:28 213
原创 32.Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20
2015-10-20 21:17:19 233
原创 31.Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
2015-10-20 17:49:34 353
原创 30.Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is
2015-10-20 11:43:49 252
原创 29.Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the in
2015-10-20 11:30:55 237
原创 28.Single Number
Note:Given an array of integers, every element appears twice except for one. Find that single one.Your algorithm should have a linear runtime complexity. Could you implement it without usi
2015-10-19 22:57:26 295
原创 27.Factorial Trailing Zeroes(求n!有几个0)
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.分析: 阶乘末尾一个零表示一个进位,则相当于乘以10 而10 是由2*5所得,在1~n当中,可以产生10的有:0 2
2015-10-19 22:40:39 263
原创 26.Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum
2015-10-19 21:51:48 292
原创 25. 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-10-19 12:04:49 211
原创 24.House Robber (DP)
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house
2015-10-19 11:49:54 253
原创 23.Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]分析:要看清楚下一行与上一行的关系。控制好循环结
2015-10-19 11:00:22 233
原创 22.Power of Two
Given an integer, write a function to determine if it is a power of two.分析:题目意思是给定一个整数n,判断其是否为2的幂数。拿到题目之后很容易想到的方法是,我一直用n除以2,直到不能除尽,最后判断该值是否为1,为1返回true,不为1则返回false。方法二:用如果一个数为2的幂数,则n&(n-1)的结果应该是0
2015-10-19 10:01:14 242
原创 21.Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.分析:首先判断所有的是否都为数字9,是的
2015-10-18 11:06:58 216
原创 20.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?分析:f(n)=f(n-1)+f(n-2)。看到
2015-10-18 10:35:36 252
原创 19.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?分析:题目要求是实现链表的转置。用了三种实现:1.递归/*递归实现
2015-10-18 10:04:10 251
原创 18.Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee
2015-10-18 09:12:45 249
原创 17.Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.分析,在做的时候设置两个节点p,q。始终保持h
2015-10-17 11:30:24 253
原创 16.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.分析:我在做的是设置了一个临时头结点head,和一个当前已拍好序的链表的最后一个节点p,时刻保持head.ne
2015-10-17 11:00:54 243
原创 15.Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares
2015-10-16 17:28:51 289
原创 14.Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc
2015-10-16 17:26:20 286
原创 13.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass
2015-10-16 12:20:39 249
原创 12.Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
2015-10-16 11:41:28 258
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人