Pineapple

Record Something

  • 首页
  • 归档
  • 标签
  • 日程
  • 搜索

对某题的吐槽

发表于 2015-11-20   |  

#65 Valid Number

Validate if a given string is numeric.
Some examples:
“0” => true
“ 0.1 “ => true
“abc” => false
“1 a” => false
“2e10” => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

第一次看到这道题的时候我的表情是这样的 →
主要考的应该是归纳能力吧,虽然好像有很多情况的样子,但好好列出来应该也是没问题的吧?
too simple too naive

阅读全文 »

Some About Cycle

发表于 2015-11-19   |  

判断环

#141 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?

设两个指向头部的变量one & two,one每次前进一个节点,two另一个每次前进两个。

  • 如果链表中存在环,则最后两个变量都会指向同一个节点。
    • 设两变量都已在环中,两者相距x步。当one继续走x步时,two走了2x步,这时两者重合。
  • 若果链表中没有环,则最后two的指针会走向链尾null。
1
2
3
4
5
6
7
8
9
10
11
12
public boolean hasCycle(ListNode head) {
if (head == null)
return false;
ListNode one = head, two = head;
while (two.next != null && two.next.next != null) {
one = one.next;
two = two.next.next;
if (one == two)
return true;
}
return false;
}
阅读全文 »

Basic Binary Search

发表于 2015-11-18   |  

刷了几道二分查找相关的题,稍微总结一下。

二分查找是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半。
–wikipedia

无重复有序数组

即最基本的二分查找

1
2
3
4
5
6
7
8
9
10
int binarySearch (int[] nums, target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (target == nums[mid]) retrun mid;
else if (target < nums[mid]) right = mid - 1;
else left = mid + 1;
}
return -1;
}

阅读全文 »

Markdown Grammar

发表于 2015-11-17   |  

1.Title

1
2
3
4
5
6
7
8
# Title 1
## Title 2
### Title 3
#### Title 4
##### Title 5
###### Title 6
---
***

Title 1

Title 2

Title 3

Title 4

Title 5
Title 6

阅读全文 »
123
butterfly

butterfly

14 日志
10 标签
RSS
xidui
© 2017 butterfly
由 Hexo 强力驱动
主题 - NexT.Muse