归档: 2016/6

记录点滴,修炼自己。
0

leetcode [#258]

题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one d

0

leetcode [#242]

题目 Given two strings s and t, write a function to determine if t is an anagram of s. Example:s = “anagram”, t = “nagaram”, return true.s = “rat”, t = “car”, return false. Note:You may assume the stri

0

leetcode [#24]

题目 Given a linked list, swap every two adjacent nodes and return its head. Example:Given 1->2->3->4, you should return the list as 2->1->4->3. Note:Your algorithm should use only co

0

leetcode [#237]

题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node

0

leetcode [#237]

题目 Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O

0

leetcode [#234]

题目 Given a singly linked list, determine if it is a palindrome. 解决方案1234567891011121314151617181920212223242526272829303132/** * Definition for singly-linked list. * public class ListNode { *

0

leetcode [#232]

题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. empt

0

leetcode [#231]

题目 Given an integer, write a function to determine if it is a power of two. 解决方案1234567891011public class Solution { public boolean isPowerOfTwo(int n) { if(n < 0) return fal

0

leetcode [#219]

题目 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 解决方

0

leetcode [#226]

题目 解决方案1234567891011121314151617181920212223/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x)

0

leetcode [#225]

题目 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whe

0

leetcode [#226]

题目 解决方案12345678910111213public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int s1 = (C - A) * (D - B); int s2 = (G

0

leetcode [#22]

题目 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: [ “((()))”, “(()())”, “(())()”, “()(())”,

0

leetcode [#217]

题目 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 elemen

0

leetcode [#21]

题目 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. 解决方案123456789101112131415161718192021222324252627282

0

leetcode [#206]

题目 Reverse a singly linked list. 解决方案123456789101112131415161718192021222324252627282930313233/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n

0

leetcode [#204]

题目 Count the number of prime numbers less than a non-negative number, n. 解决方案12345678910111213141516171819public class Solution { public int countPrimes(int n) { if(n <= 2) r

0

leetcode [#205]

题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with anothe

0

leetcode [#203]

题目 Remove all elements from a linked list of integers that have value val. Example:Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6Return: 1 –> 2 –> 3 –> 4 –> 5 解决方案123

0

leetcode [#202]

题目 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 square

0

leetcode [#20]

题目 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 all valid

0

leetcode [#19]

题目 Given a linked list, remove the nth node from the end of list and return its head. Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the

0

leetcode [#189]

题目 Rotate an array of n elements to the right by k steps. 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 you can, ther

0

leetcode [#179]

题目 Given a list of non negative integers, arrange them such that they form the largest number. Example:Given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very lar

0

leetcode [#171]

题目 Given a column title as appear in an Excel sheet, return its corresponding column number. Note:A -> 1B -> 2C -> 3…Z -> 26AA -> 27AB -> 28 解决方案1234567public class Solution

0

leetcode [#172]

题目 Given an integer n, return the number of trailing zeroes in n!. Note:Your solution should be in logarithmic time complexity. 解决方案12345678910public class Solution { public int trailingZer

0

leetcode [#169]

题目 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 a

0

leetcode [#168]

题目 Given a positive integer, return its corresponding column title as appear in an Excel sheet. Example:1 -> A2 -> B3 -> C…26 -> Z27 -> AA28 -> AB 解决方案123456789101112131415161718

0

leetcode [#167]

题目 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbe

0

leetcode [#165]

题目 Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty

0

leetcode [#155]

题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get

0

leetcode [#160]

题目 Write a program to find the node at which the intersection of two singly linked lists begins. Example:the following two linked lists:A: a1 → a2 ↘ c1

0

leetcode [#142]

题目 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解决方案12345678910111213141516171819202122232425/** * Definition for singly-linked list. * class List

0

leetcode [#147]

题目 Sort a linked list using insertion sort. 解决方案12345678910111213141516171819202122232425262728293031323334353637383940/** * Definition for singly-linked list. * public class ListNode { *

0

leetcode [#141]

题目 Given a linked list, determine if it has a cycle in it. 解决方案12345678910111213141516171819202122232425/** * Definition for singly-linked list. * class ListNode { * int val; * ListNod

0

leetcode [#14]

题目 Write a function to find the longest common prefix string amongst an array of strings. 解决方案1234567891011121314151617181920212223242526272829303132public class Solution { public String lo

0

leetcode [#13]

题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解决方案1234567891011121314151617181920212223242526272829303132333435363738394041424344454

0

leetcode [#136]

题目 Given an array of integers, every element appears twice except for one. Find that single one. 解决方案123456789101112131415161718import java.util.Hashtable;public class Solution { public int

0

leetcode [#125]

题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example“A man, a plan, a canal: Panama” is a palindrome.“race a car” is not a palindro

0

leetcode [#122]

题目 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 as many transactions as you like (ie, buy o

0

leetcode [#121]

题目 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock)

0

leetcode [#119]

题目 Given an index k, return the kth row of the Pascal’s triangle. Example:given k = 3,Return [1,3,3,1]. 解决方案1234567891011121314151617181920212223public class Solution { public List<Integ

0

leetcode [#118]

题目 Given numRows, generate the first numRows of Pascal’s triangle. Example:given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] 解决方案1234567891011121314151617181920publi

0

leetcode [#1]

题目 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example:Given nums = [2, 7

0

leetcode [#104]

题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解决方案123456789101112131415161718192