OneCoder


  • 首页

  • 归档

  • 标签

  • 关于

  • 搜索

LeetCode Count And Say

发表于 2017-10-31 | 阅读次数

Problem

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
  2. 11
  3. 21
  4. 1211
  5. 111221

1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, then one 1” or 1211. Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1 Output: “1” Example 2:

Input: 4 Output: “1211”

题目的意思就是反复的计算字符中,从左向右,每种字符的数量和值。
1 -> 1个1,即11。 11 -> 2个1,即21。 21 -> 1个2,1个1,即1211。以此类推。

阅读全文 »

LeetCode Implement strStr()

发表于 2017-10-29 | 阅读次数

Problem

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

题目其实就是实现Java中String的indexOf函数。因此,直接调用该方法,显然是不合适的。

阅读全文 »

LeetCode Remove Element

发表于 2017-10-26 | 阅读次数

Problem

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example: Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

跟从排好序的队列出移除重复元素问题类似。从给定的队列中移除给定的元素。最后,都需要把剩余的有效元素放在队列的开头,可以理解为同一个问题。

阅读全文 »

LeetCode Remove Duplicates from Sorted Array

发表于 2017-10-22 | 阅读次数

Problem

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example, Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

题目其实包含两部分要求。首先,需要返回一个已排序数组,值不相同的元素的个数。例如:[1,1,2]返回2。这也是题目代码可以自动校验的部分。另一个隐含的要求是,要求不使用新的空间,并且在算出个数n后,将值不相同的元素,依次放置到数组的前N位。

阅读全文 »

LeetCode Merge Two Sorted Lists

发表于 2017-10-19 | 阅读次数

Problem

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.

即合并两个已经排好序的链表。并且要求返回的新链表是由原两个链表元素组合而成的。链表的结构定义也已经固定给出:

public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
阅读全文 »

LeetCode Valid Parentheses

发表于 2017-10-18 | 阅读次数

Problem

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 but “(]” and “([)]” are not.

即判断一个只包含括号的字符串,是不是合法的。

阅读全文 »

LeetCode Longest Common Prefix

发表于 2017-10-17 | 阅读次数

Problem

Write a function to find the longest common prefix string amongst an array of strings.

就是找出一个字符串数组中元素,最长的通用前缀。例如:{“ab”,”abc”,”abd”},答案是 “ab”。

阅读全文 »

LeetCode Integer To Roman

发表于 2017-10-16 | 阅读次数

Problem

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

跟Roman To Integer是对应问题。即将整数转换成对应的罗马数字。罗马数字规则见wiki:罗马数字规则

阅读全文 »

LeetCode Roman to Integer

发表于 2017-10-13 | 阅读次数

Problem

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

即将罗马数字1-3999转换成对应的整数。罗马数字规则见wiki:罗马数字规则

阅读全文 »

LeetCode Palindrome Number

发表于 2017-10-12 | 阅读次数

Problem

Determine whether an integer is a palindrome. Do this without extra space.

即返回一个数是否是回数。例如:1,1221,12321是回数,负数不是回数。题目特别提醒,不要使用额外的空间,即不要考虑转换成String来处理。

阅读全文 »
1 … 9 10 11 … 33
LiHongZhe

LiHongZhe

onecoder's blog.

326 日志
8 分类
RSS
Creative Commons
Links
  • 酷壳
  • 酸菜鱼
  • 私塾在线学习网
  • 煮酒品茶
  • 点滴技术博客
  • 思考者日记网·束洋洋
  • 开源视窗
  • 小弟子的网络之路
  • 寄存心–移动开发
  • TicmyBlog
  • 中国程序员人才网
  • 图表秀-在线图表制作
  • IT热血青年
© 2012 - 2022 LiHongZhe
由 Jekyll 强力驱动
主题 - NexT.Muse