档案日期2015的25

2015年6月22日 - 2015年6月28日

[LeetCode]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和slow在链表的某处相遇,则说明链表中有环

Python代码(快慢指针):

# Definition for singly-linked list.
# class ListNode: ...

继续阅读

[LeetCode]Summary Ranges

题目描述:

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

题目大意:

给定一组排好序且无重复的整数,返回整数范围的汇总。

例如给定数组 [0,1,2,4,5,7], 返回 ["0->2","4->5","7"]。

解题思路:

将逐一递增的整数序列化简为(起点->终点)即可。

Python代码:

class Solution:
    # @param {integer[]} nums
    # @return {string[]}
    def summaryRanges(self, ...

继续阅读

浮点精度问题简析

Why don’t my numbers add up?

为什么我的数加起来对不上?

So you’ve written some absurdly simple code, say for example:

你写了一段极其简单的代码,比如:

    0.1 + 0.2

and got a really unexpected result:

然后得到了一个意想不到的结果:

    0.30000000000000004

Why don’t my numbers, like 0.1 + 0.2 add ...

继续阅读

每日归档

上周

下周

归档