2. Add Two Numbers(费劲了。。。)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode p = null;
ListNode newListNode=null;
ListNode templ1=l1;
ListNode templ2=l2;
if(templ1.val+templ2.val>=10)
{
ListNode tempNode = new ListNode(templ1.val+templ2.val-10);
if(templ1.next!=null){
templ1.next.val = templ1.next.val+1;
p = tempNode;
newListNode=p;
}
else if(templ2.next!=null)
{
templ2.next.val = templ2.next.val+1;
p = tempNode;
newListNode=p;
}else{
p = tempNode;
newListNode=p;
ListNode tempNodetemp = new ListNode(1);
p.next = tempNodetemp;
return newListNode;
}
}else{
ListNode tempNode = new ListNode(templ1.val+templ2.val);
p = tempNode;
newListNode=p;
}
templ1=templ1.next;
templ2=templ2.next;
while(templ1!=null&&templ2!=null){
if(templ1.val+templ2.val>=10)
{
ListNode tempNode = new ListNode(templ1.val+templ2.val-10);
if(templ1.next!=null){
templ1.next.val = templ1.next.val+1;
p.next=tempNode;
p=p.next;
}
else if(templ2.next!=null)
{
templ2.next.val = templ2.next.val+1;
p.next=tempNode;
p=p.next;
}else{
p.next = tempNode;
ListNode tempNodetemp = new ListNode(1);
p.next.next = tempNodetemp;
return newListNode;
}
}else{
ListNode tempNode = new ListNode(templ1.val+templ2.val);
p.next=tempNode;
p=p.next;
}
templ1 = templ1.next;
templ2 = templ2.next;
}
if(templ1!=null){
while(templ1.val==10)
{
ListNode tempNodetemp = new ListNode(0);
p.next = tempNodetemp;
if(templ1.next!=null){
templ1.next.val = templ1.next.val+1;
p=p.next;
}
else{
p.next = tempNodetemp;
ListNode tempNodeAppend = new ListNode(1);
p.next.next = tempNodeAppend;
return newListNode;
}
templ1=templ1.next;
}
if(templ1.val!=10){
p.next = templ1;
return newListNode;
}
}
else if(templ2!=null)
{
while(templ2.val==10)
{
ListNode tempNodetemp = new ListNode(0);
p.next = tempNodetemp;
if(templ2.next!=null){
templ2.next.val = templ2.next.val+1;
p=p.next;}
else{
p.next = tempNodetemp;
ListNode tempNodeAppend = new ListNode(1);
p.next.next = tempNodeAppend;
return newListNode;
}
templ2=templ2.next;
}
if(templ2.val!=10){
p.next = templ2;
return newListNode;
}
}else{
return newListNode;
}
return newListNode;
}
}
3. Longest Substring Without Repeating Characters 尴尬的我来了

class Solution {
public int lengthOfLongestSubstring(String s) {
int pos = 0;
int[] letters = new int [256];
for(int i=0;i<letters.length;i++){
letters[i]=0;
}
int max = 0;
int auto_length=0;
for(int i=0;i<s.length();i++){
if(letters[s.charAt(i)]==1){
if(auto_length>max){
max = auto_length;
}
pos = s.indexOf(Character.toString(s.charAt(i)));
letters[s.charAt(i)]=0;
auto_length = 0;
for(int j=0;j<i;j++){
letters[s.charAt(j)]=0;
}
s=s.substring(pos+1);
i=-1;
}
else{
letters[s.charAt(i)]=1;
auto_length++;
}
}
if(auto_length>max){
max = auto_length;
}
return max;
}
}
19. Remove Nth Node From End of List(感觉中等题也像简单题,水题很多呀)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode p = head;
List<ListNode> pNodeList = new ArrayList<ListNode>();
while(p!=null){
pNodeList.add(p);
p=p.next;
}
int pos = pNodeList.size()-n;
pNodeList.remove(pos);
int i=0;
p=head;ListNode pre = null;
while(pNodeList.size()>0&&pNodeList.get(i).val==p.val){
pre = p;
p = p.next;
i++;
if(i>=pNodeList.size()){
break;
}
}
if(i==0&&pNodeList.size()!=0){
head= head.next;
}else if(pNodeList.size()!=0){
pre.next = p.next;
}else{
head=null;
}
return head;
}
}
最后
以上就是神勇画笔最近收集整理的关于来吧,每天做点Leetcode(中等难度题) 实时更新~尴尬---相信所有so easy的全部内容,更多相关来吧,每天做点Leetcode(中等难度题)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复