刷题记录,code代表题目顺序
code1
class Solution {
public int[] twoSum(int[] nums, int target) {
int res[]=new int[2];
int len=nums.length;
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(nums[i]+nums[j]==target){
res[0]=i;
res[1]=j;
}
}
}
return res;
}
}
code2
public class Code2 {
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x; }
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode t=new ListNode(8);
ListNode t1=new ListNode(5);
//测试代码
t= addTwoNumbers(t1, t);
while (t!=null) {
System.out.println(t.val);
t=t.next;
}
}
private boolean flag=false;
private static int topInt=0;
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//找到头节点出队,相加
ListNode l3=null;
ListNode temp=null;
ListNode temp2=null;
ListNode head=null;
boolean flag=false;
while (l1!=null||l2!=null||flag!=false) {
int val1=0;
int val2=0;
if(l1!=null) {
val1=l1.val;
}else {
val1=0;
}
if(l2!=null) {
val2=l2.val;
}else {
val2=0;
}
if(l1==null) {
l1=null;
}else {
l1=l1.next;
}
if(l2==null) {
l2=null;
}else {
l2=l2.next;
}
if(flag) {
val1=val1+1+val2;
flag=false;
}else {
val1=val1+val2;
}
if(val1>=10) {
flag=true;
}
val1=val1%10;
if(l3==null) {
l3=new ListNode(val1);
//l3.val=val1;
l3.next=null;
head=l3;
}else {
ListNode tl3=new ListNode(val1);
//tl3.val=val1;
tl3.next=null;
head.next=tl3;
head=tl3;
}
}
return l3;
}
}
code3
class Solution {
public static void main(String[] args) {
//模拟动态规划做此题
lengthOfLongestSubstring("abcd");
}
private static char[] charas;
public static int lengthOfLongestSubstring(String s) {
charas=new char[s.length()];
charas=s.