ITeye博客

2016 年文章归档

共 209 篇文章

Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: Given the total number of courses and a list of prerequisite pairs, return the ordering of cours…

2016-02-20 05:51 · Leetcode

Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array and s = 7, the subarray has the minimal length under the problem constraint. 在一个数组中,找到一个长度最小的子数组,使它的和大于或等于给定…

2016-02-20 04:46 · Leetcode

Implement Trie (Prefix Tree) 字典树

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 设计一个字典树(前缀树),题目中假设字典中包含的字母为a-z, 我们可以设定树中每个节点都有两个属性,一个为TrieNode, 另一个为一个布尔变量isLast。TrieNode 用来存放insert操作时对应的字符,isLast对应search操作是是否包含这个元素。代码如下: class TrieNode { bool…

2016-02-20 03:55 · Leetcode

Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: Given the total number of courses and a list of prerequisite pairs, is it possible for you to fi…

2016-02-20 03:08 · Leetcode

Reverse Linked List

Reverse a singly linked list. 反转一个单向链表,采用头插法,设定两个指针pre和tem,tem记录head的下一个元素,从head开始依次指向它前面的元素,同时pre不断后移。代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode reverseList(ListN…

2016-02-19 03:09 · Leetcode

Isomorphic Strings

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 another character while preserving the order of characters. No two characters may map to the same character b…

2016-02-19 03:04 · Leetcode

Count Primes

Description: Count the number of prime numbers less than a non-negative number, n. 给定一个整数,返回小于它的所有质数。我们借助一个布尔数组,来记录哪些数已经被标记为不为质数,每次循环的时候都从一个质数开始。最后只需要遍历一遍布尔数组中值为true的元素个数就可以了。代码如下: public class Solution { public int countPrimes(int n) { boolean[] prime = new boolean[n]; Arrays.fill(prime, true); in…

2016-02-19 02:39 · Leetcode

Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 从链表中删除节点为给定值得所有节点。对于在链表中删除节点的问题,如果头结点可能被删除的情况下,我们往往创建一个helper节点,让它指向头结点。这样从helper节点的next开始处理,最后返回help.next就可以了。代码如下: /** * Definiti…

2016-02-19 02:05 · Leetcode

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 of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops end…

2016-02-19 01:57 · Leetcode

Bitwise AND of Numbers Range

Given a range where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range , you should return 4. 给定一个范围,通过位与运算计算这个范围内所有的整数,返回计算后的结果。我们只需要从两个数的高位开始比较,如果遇到不相等的就停止,因为当这个范围内所有的数位与之后,只有高位相等且为1的情况下,这些位上的数才为1,其它都为0。我们借助一个help,long help = 1L…

2016-02-18 03:54 · Leetcode

Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: 11110 11010 11000 00000 Answer:…

2016-02-18 03:32 · Leetcode

Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return . 给定一颗二叉树,输出它的right view,所谓right view就是从右边观察这棵树所能看到的节点。很容易想到的是用…

2016-02-18 02:51 · Leetcode

House Robber

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 houses have security system connected and it will automatically contact the police if two adjacent house…

2016-02-18 02:20 · Leetcode

Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. 计算一个整数中1的个数。通过位运算,每次右移一位,判断是否为1,如果为1计数器加1,代…

2016-02-18 02:02 · Leetcode

Reverse Bits

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 给定一个无符号的整数,将它的每个比特位都逆转,返回逆转之后新的数。通过位运算,取32个不同位上的数字,同时通过左移来得到结果。代码如下: public …

2016-02-17 04:00 · Leetcode

Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array is rotated to . 将一个数组右移k步,如果我们直接移动k次,会超时。我们可以先反转后k个元素,然后在反转前length - k个元素,最后将整个数组反转,就得到了结果。k在运算的时候要先于数组取模,因为k可能大于数组的长度。代码如下: public class Solution { public void rotate(int[] nums, int k) { if(nums ==…

2016-02-17 03:46 · Leetcode

Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given , the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an integer. 给定一个数组,里面存放的都是非负整数,要求我们用里面的元素组成一个最大的数。这里我们首先将数组转换成一个一个字符串数…

2016-02-17 03:14 · Leetcode

Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. 一…

2016-02-17 02:03 · Leetcode

Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 给定一个整数n,让我们判断n的阶乘中末尾有几个零。我们只需要知道n的阶乘中有几个2和5的组合就可以,每个5前面都会有一个尾数为2的值,对于本身尾数为0的我们可以理解为它本身就是一个2和5的组合。这样我们只需要知道5的个数就可以了。代码如下: public class Solution { public int trailingZeroes(in…

2016-02-17 01:43 · Leetcode

Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 给定一个数,转换成Excel的字母表示形式,实际上就是一个十进制数字转26进制运算。代码如下: public class Solution { public String convertToTitle(int n) { StringBuilder sb = new StringBui…

2016-02-16 04:40 · Leetcode