本周按照顺序继续做leetcode中的题目,随着难度的递增,做题已经不能像之前那样非常快地想出算法了。虽然都是简单题,但是通过率也都缩小到了百分之三十多。最重要的是,这样的基础题会让我对于java和python的使用更加熟悉,起初那几周我要用到什么函数还要去查一下书,现在有些函数我就直接记住并且使用了。这一个月以来,我觉得我收获还是很大的,每周虽然只有五道题,但是通过练习,感觉对于java和python的理解都在每周加深。
13. Roman to Integer
这道题目就是让我们将罗马数字转化为阿拉伯数字,说难起始也不难,说简单也不简单。因为不了解罗马数字,我上网查了一下。发现罗马数字的几个基本特性。首先就是罗马数字当中只有几种特殊的符号,通过排列顺序不同,组合出不同的数字。第二就是如果小数字在大数字的前面,那么这个数字所代表的就是大数字减去小数字的值。有了这两点,我们就可以很快想到算法。从前往后加,如果碰到下一位比当前位大的话,就加上后一位,减去当前位。算法复杂度为O(n)
java版本:
public class Solution
{
public int romanToInt(String s)
{
HashMap<Character,Integer> pp=new HashMap<Character,Integer>();
pp.put('I',1);
pp.put('V',5);
pp.put('X',10);
pp.put('L',50);
pp.put('C',100);
pp.put('D',500);
pp.put('M',1000);
char[] ss=s.toCharArray();
int answer=0;
int i;
for(i=0;i<ss.length-1;i++)
{
if(pp.get(ss[i])<pp.get(ss[i+1]))
{
i++;
answer=answer+pp.get(ss[i])-pp.get(ss[i-1]);
}
else answer=answer+pp.get(ss[i]);
}
if(i==ss.length) return answer;
else return answer+pp.get(ss[i]);
}
}python版本:
python当中,如果按照java当中的算法来写,会出现问题。所以我换了另一种计算方法,就是先全加起来,最后再减去两倍那个小的数字。
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
answer=0
p=[('I',1),('V',5),('X',10),('L',50),('C',100),('D',500),('M',1000)]
pp=dict(p)
for i in range(0,len(s)):
answer=answer+pp[s[i]]
if pp[s[i-1]]<pp[s[i]] and i>0:
answer=answer-2*pp[s[i-1]]
return answer
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode reverseList(ListNode head)
{
Stack<ListNode> tt=new Stack<ListNode>();
if(head==null) return null;
ListNode t=head;
while(t!=null)
{
tt.push(t);
t=t.next;
}
head=tt.pop();
t=head;
while(!tt.isEmpty())
{
t.next=tt.pop();
t=t.next;
}
t.next=null;
return head;
}
}python版本:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
t=head
tt=[]
while t:
tt.insert(0,t.val)
t=t.next
t=head
for i in tt:
t.val=i
t=t.next
return head
public class Solution
{
public boolean isPowerOfThree(int n)
{
if(n==1) return true;
if(n==0) return false;
while(n!=0)
{
if(n==1) return true;
else if(n%3!=0) return false;
n=n/3;
}
return true;
}
}
python版本:
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n==1:
return True
if n==0:
return False
while n!=0:
if n==1:
return True
elif n%3!=0:
return False
n=n/3
return True
202. Happy Number
public class Solution
{
public boolean isHappy(int n)
{
boolean[] pd=new boolean[100000];
int sum=Happy(n);
while(sum!=1)
{
if(pd[sum]==true)
{
return false;
}
pd[sum]=true;
sum=Happy(sum);
}
return true;
}
public int Happy(int n)
{
int answer=0;
while(n!=0)
{
int y=n%10;
answer+=y*y;
n/=10;
}
return answer;
}
}
python版本:
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
pd={}
sum=self.Happy(n)
while sum!=1:
if sum in pd:
return False
pd[sum]=True
sum=self.Happy(sum)
return True
def Happy(self,n):
answer=0
while n!=0:
i=n%10
answer+=i*i
n=n/10
return answer
83. Remove Duplicates from Sorted List
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode deleteDuplicates(ListNode head)
{
int value;
if(head==null) return null;
ListNode t=head;
while(t.next!=null)
{
if(t.val!=t.next.val)
{
t=t.next;
}
while(t.next!=null&&t.next.val==t.val)
{
t.next=t.next.next;
}
}
return head;
}
}python版本:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head==None:
return head
t=head
while t.next!=None:
if t.next.val!=t.val:
t=t.next
while (t.next!=None) and (t.next.val==t.val):
t.next=t.next.next
return head
本周在LeetCode上挑战更高难度的题目,虽然基础题通过率降低到30%左右,但对Java和Python的熟悉度显著提升。初期需查阅函数用法,现在能直接应用。通过每周五道题的练习,编程理解不断加深。详细讨论了13. Roman to Integer的解题思路,Java和Python的不同实现方法。
1222

被折叠的 条评论
为什么被折叠?



