您的位置:首页 > 职场人生

面试笔试杂项积累-leetcode 91-95

2016-02-03 23:04 537 查看

91.91-Decode Ways-Difficulty: Medium

A message containing letters from
A-Z
is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,

Given encoded message
"12"
,it could be decoded as
"AB"
(1 2) or
"L"
(12).

The number of ways decoding
"12"
is 2.

思路

题意是照上面的编码规则,有多少种英文字母组合的可能性

使用动态规划

参考:
http://blog.csdn.net/Maggie_2015/article/details/50587269 http://blog.csdn.net/claviclelau/article/details/11092001
public class Solution {//动态规划
public int NumDecodings(string s) {
if (s == null || s.Length == 0 || s[0] == '0') return 0;
int pre = 1, cur = 1;
int a = s[0] - '0', b = 0;
for (int i = 1; i < s.Length; i++)
{
b = s[i] - '0';
if (b == 0 && (a > 2 || a <= 0)) return 0;
int save = cur;
if (b == 0)
{
cur = pre;
}
else if (a != 0 && a * 10 + b <= 26)
{
cur = cur + pre;
}
pre = save;
a = b;
}
return cur;
}
}


92.92-Reverse Linked List II-Difficulty: Medium

Reverse a linked list from position m ton. Do it in-place and in one-pass.

For example:

Given
1->2->3->4->5->NULL
, m = 2 and n = 4,

return
1->4->3->2->5->NULL
.

Note:

Given m, n satisfy the following condition:

1 ≤ m ≤ n ≤ length of list.

思路

题目要求对给定范围全部反向,

博主开始误会题意把两个节点颠过来了。。。。

很简单,先遍历找到这两个节点为start和last,再把start的next逐个往start前面堆,也可以想象成在这个范围内指向全部反过来,在把start,end各自安放即可

/**
* Definition for singly-linked list.
* public class ListNode {
*     public int val;
*     public ListNode next;
*     public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode ReverseBetween(ListNode head, int m, int n) {
if (head.next == null || m == n)
return head;
ListNode temp = new ListNode(0);
temp.next = head;
ListNode fin = temp;
ListNode pointer1 = head;
ListNode pointer2 = head;
for (int i = 0; i < n; i++)
{

if (i == m - 1)
pointer1 = temp;
if (i == n - 1)
pointer2 = temp;

temp = temp.next;

}
ListNode last_temp = pointer2.next.next;
ListNode last = pointer2.next;
ListNode start_temp = pointer1.next;
ListNode start = pointer1.next;
temp = start.next;
ListNode temp2 = start.next;
for (int i = 0; i < n - m; i++)
{
temp2 = temp2.next;
temp.next = start;
start = temp;
temp = temp2;
}
start_temp.next = last_temp;
pointer1.next = last;
return fin.next;
}
}


94.94-Binary Tree Inorder Traversal-Difficulty: Medium

Given a binary tree, return the inorder traversal of its nodes' values.

For example:

Given binary tree
{1,#,2,3}
,

1
\
2
/
3

return
[1,3,2]
.

Note: Recursive solution is trivial, could you do it iteratively?

confused what
"{1,#,2,3}"
means?> read more on how binary tree is serialized on OJ.

思路

二叉树的中序遍历

/**
* Definition for a binary tree node.
* public class TreeNode {
*     public int val;
*     public TreeNode left;
*     public TreeNode right;
*     public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<int> fin = new List<int>();
public IList<int> InorderTraversal(TreeNode root)
{
if (root == null)
return fin;
printTreeDepth1_2(root);
return fin;
}

public void printTreeDepth1_2(TreeNode temp)
{

if (temp.left != null)
{
printTreeDepth1_2(temp.left);

}
fin.Add(temp.val);
if (temp.right != null)
{
printTreeDepth1_2(temp.right);
}
}
}

95.95-Unique Binary Search Trees II-Difficulty: Medium

Given n, generate all structurally unique
BST's (binary search trees) that store values 1...n.

For example,

Given n = 3, your program should return all 5 unique BST's shown below.

1         3     3      2      1
\       /     /      / \      \
3     2     1      1   3      2
/     /       \                 \
2     1         2                 3

confused what
"{1,#,2,3}"
means?

> read more on how binary tree is serialized on OJ.

思路

往后一部分都是树的题了,感觉挺熟练的,但是动态规划还是我的死穴啊。。。还是因为太懒。

给予一个数,返回这个数节点的的所有二叉查找树

递归,每个都是根节点

参考:
http://www.cnblogs.com/ganganloveu/p/4138344.html
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<TreeNode> GenerateTrees(int n)
{
if (n == 0)
{ IList<TreeNode> fin = new List<TreeNode>(); return fin; }
return Recursion(1, n);
}
IList<TreeNode> Recursion(int start, int end)
{
IList<TreeNode> fin = new List<TreeNode>();
if (end < start)
fin.Add(null);
else if (end == start)
{
TreeNode temp = new TreeNode(start);
fin.Add(temp);
}
else
for (int i = start; i <= end; i++)
{
IList<TreeNode> l = Recursion(start, i - 1);
IList<TreeNode> r = Recursion(i + 1, end);

for (int j = 0; j < l.Count; j++)
for (int w = 0; w < r.Count; w++)
{
TreeNode root = new TreeNode(i);
root.left = l[j];
root.right = r[w];
fin.Add(root);
}

}

return fin;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: