您的位置:首页 > 编程语言 > Java开发

[2016/07/01] LeetCode / Java - Day 09 -

2016-07-01 09:12 477 查看
下半年加油~不要问我为啥今天这么早→ →

206. Reverse Linked List

Reverse
a singly linked list.

思路:逆序链表,呃我一下子就想到了堆栈。。其实这个O(2n)的时间复杂度+O(n)的空间复杂度不太好的。。看了一下别人的程序,昨晚有点头昏,就没有仔细看,大概是循环,然后首尾交换,就是改next关系,而我是直接改的val。

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
import java.util.Stack;
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return head;
Stack<Integer> s = new Stack<Integer>();
ListNode p = head;
while(p!=null){
s.push(p.val);
p = p.next;
}
p = head;
while(p!=null){
p.val = s.pop();
p = p.next;
}
return head;
}
}

94. Binary Tree Inorder Traversal

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

For example:

Given binary tree 
[1,null,2,3]
,

1
\
2
/
3


return 
[1,3,2]
.

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

思路:这个和昨天的那个没啥区别啊,就是把前根遍历改成了中根遍历。。(关键在于我不肯动脑子想怎么用迭代来解决!)

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
import java.util.ArrayList;
import java.util.List;
public class Solution {

List<Integer> l = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root==null) return l;
if(root.left!=null){
l = inorderTraversal(root.left);
}
l.add(root.val);
if(root.right!=null){
l = inorderTraversal(root.right);
}

return l;
}
}

13. Roman to Integer

Given a roman numeral, convert it to an integer.

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

思路:挺喜欢这样有意思的题~然后百度了下,发现很多罗马数字我不知道的知识。。。我原来只会1~10的罗马数字,现在我可以自由到无限啦哈哈哈。

规则主要(我记得的)有以下几个:

1、基本数字代表

I      V     X     L     C     D       M

1    5     10    50   100  500   1000

2、重复规则

一个字母最多重复三次,重复表示相加

3、左右规则

小数在大数左边,表示相减,且在大数左边最多只能有一位,比如你不能IIV

小数在大数右边,表示相加,在右边的位数不超过3位,VIII是可以的~

好像有些左边的限制条件,但记不得了

4、其它 编程不涉及我就不啰嗦了

import java.util.ArrayList;
import java.util.List;

public class Solution {
public int romanToInt(String s) {
//l 存放涉及到的字母,并且与i数组中的数字一一对应
List<Character> l = new ArrayList<Character>();
l.add('I');l.add('V');l.add('X');l.add('L');l.add('C');l.add('D');l.add('M');
int[] i = new int[]{1,5,10,50,100,500,1000};
int[] num = new int[s.length()];//新建一个数组,用于存放字符串处理后的数字
if(s.length()==1) return i[l.indexOf(s.charAt(0))];//如果s只有一位,就直接对应字母返回数字(这一行应该在上一行上面的。。没仔细看)
for(int k=0;k<s.length();k++){
num[k]=i[l.indexOf(s.charAt(k))];
}
int sum=0;int k=0;//和,指针
while(k<s.length()){
//检测连续
int lianxu=1;
while(k+1<s.length()){
if(num[k]==num[k+1])
{	lianxu++;
k++;
}
else break;
}
//连续的部分的值是有讲究的。如果后面跟着个大数,就只能算一个减,别的加。如果后面跟着的不是大数,那么就加所有的
if(k+1==s.length()-1){
if(num[k]<num[k+1] )
sum=sum-num[k]+(lianxu-1)*num[k]+num[k+1];
else  sum=sum+lianxu*num[k]+num[k+1];
break;
}else if(k+1>s.length()-1){
sum=sum+lianxu*num[k];
break;
}
if(num[k]<num[k+1] )
sum=sum-num[k]+(lianxu-1)*num[k];
else  sum=sum+lianxu*num[k];
k++;
}

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