刷题记录,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
) {
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
.next
=null
;
head
=l3
;
}else {
ListNode tl3
=new ListNode(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
.toCharArray();
if(s
.length()<=0)
return 0;
int k
=1;
for(int i
=1;i
<s
.length();i
++) {
if(isSubstring(i
,k
)) {
k
++;
}
}
System
.out
.println(k
);
return k
;
}
private static boolean isSubstring(int i
, int k
) {
int flag
=0;
for(int j
=(i
-k
);j
<=i
;j
++) {
for(int jk
=j
+1;jk
<=i
;jk
++) {
if(charas
[j
]==charas
[jk
]) {
return false;
}
}
}
return true;
}
}
code4
没做出来
code5
public class Code5 {
public static void main(String
[] args
) {
String string
="bb";
string
=longestPalindrome(string
);
System
.out
.println(string
);
}
private static char[] arr
;
private static int extend
=0;
public static String
longestPalindrome(String s
) {
arr
=s
.toCharArray();
int start
=0;
int index
=0;
int k
=1;
if(s
.length()<=0)
return "";
if(s
.length()>=2) {
if(arr
[0]==arr
[1])
k
=2;
else {
k
=1;
}
}
for(int i
=2;i
<s
.length();i
++) {
if(getSub(i
, k
)==1) {
k
++;
index
=i
;
continue;
}
if(getSub(i
, k
)==2) {
k
+=2;
index
=i
;
continue;
}
}
if(index
==0)
s
=(String
) s
.subSequence(index
, index
+k
);
else {
s
=(String
) s
.subSequence(index
-k
+1, index
+1);
}
return s
;
}
private static int getSub(int i
, int step
) {
int flag
=1;
int count
=step
+1;
int begin
=i
-count
+1;
int end
=i
;
while (begin
<end
) {
if(arr
[begin
++]!=arr
[end
--]) {
flag
=-1;
break;
}
}
if(flag
==1) return 1;
flag
=1;
count
=step
+2;
begin
=i
-count
+1;
if(begin
<0)
return 0;
end
=i
;
while (begin
<end
) {
if(arr
[begin
++]!=arr
[end
--]) {
flag
=-1;
break;
}
}
if(flag
==1) return 2;
return 0;
}
}
code6
public class Code6 {
public static void main(String
[] args
) {
Code6 code6
=new Code6();
code6
.convert("abc", 3);
}
public String
convert(String s
, int numRows
) {
if(s
.length()<=2||numRows
<=1) return s
;
int size
=(int)( s
.length()/2);
if(s
.length()%2!=0)
size
++;
char[] ori
=new char[s
.length()];
ori
=s
.toCharArray();
char[] res
=new char[s
.length()];
int[][] arr
=new int[numRows
][size
];
for(int i
=0;i
<numRows
;i
++)
for(int j
=0;j
<size
;j
++)
arr
[i
][j
]=-1;
int x
=0,y
=0;
for(int i
=0;i
<s
.length();i
++) {
if( Move(i
,numRows
)==1) {
arr
[x
][y
]=i
;
x
++;
}else {
arr
[x
][y
]=i
;
y
++;
x
--;
}
}
int temp
=0;
for(int i
=0;i
<numRows
;i
++) {
for(int j
=0;j
<size
;j
++) {
if(arr
[i
][j
]<0)
continue;
else {
res
[temp
]=ori
[arr
[i
][j
]];
temp
++;
}
}
}
s
=String
.valueOf(res
);
return s
;
}
private int Move(int i
,int num
) {
if(i
%(2*num
-2)<(num
-1)) {
return 1;
}
return 0;
}
}
Code25
public class Code25 {
static class ListNode {
int val
;
ListNode next
;
ListNode() {}
ListNode(int val
) { this.val
= val
; }
ListNode(int val
, ListNode next
) { this.val
= val
; this.next
= next
; }
}
public static void main(String
[] args
) {
ListNode node1
=new ListNode(1);
ListNode node2
=new ListNode(2);
ListNode node3
=new ListNode(3);
ListNode node4
=new ListNode(4);
ListNode node5
=new ListNode(5);
node1
.next
=node2
;
node2
.next
=node3
;
node3
.next
=node4
;
node4
.next
=node5
;
Code25 code25
=new Code25();
ListNode tem
=node2
;
tem
=code25
.reverseKGroup(node1
, 2);
while (tem
!=null
) {
System
.out
.println(""+tem
.val
);
tem
=tem
.next
;
}
}
private ListNode
reverseKGroup(ListNode head
, int k
) {
int len
=0;
ListNode tem
=head
;
while (tem
!=null
) {
tem
=tem
.next
;
len
++;
}
head
= finalLists(head
,k
,len
);
return head
;
}
private ListNode
finalLists(ListNode head
, int k
, int len
) {
ListNode res
=new ListNode();
if(len
<k
) {
return head
;
}
else {
ListNode tem
=head
;
ListNode end
=head
;
for(int i
=1;i
<k
;i
++) {
end
=end
.next
;
}
for(int i
=1;i
<=k
;i
++) {
tem
=tem
.next
;
}
res
= reverseList(head
, k
);
head
.next
=finalLists(tem
, k
, len
-k
);
return res
;
}
}
private ListNode
getNode(ListNode head
,int n
) {
ListNode tem
=head
;
while (n
>=0) {
tem
=tem
.next
;
n
--;
}
return tem
;
}
private ListNode
reverseList(ListNode head
,int k
) {
if(k
==1) return head
;
if(k
==2) {
ListNode end
=head
.next
;
ListNode temp
=end
.next
;
head
.next
=temp
;
end
.next
=head
;
return end
;
}
else {
ListNode tem
=head
;
ListNode end
=head
;
for(int i
=1;i
<k
;i
++) {
end
=end
.next
;
}
tem
=head
;
for(int i
=1;i
<k
/2;i
++) {
tem
=tem
.next
;
}
ListNode tem1
=tem
.next
;
reverseList(head
, k
/2);
reverseList(tem1
, k
-k
/2);
tem1
.next
=tem
;
head
.next
=null
;
return end
;
}
}
}
Code121
public int maxProfit(int[] prices
) {
int maxpro
=0;
for (int i
= 0; i
< prices
.length
; i
++) {
for(int j
=i
+1;j
<prices
.length
;j
++) {
int profit
=prices
[j
]-prices
[i
];
if(profit
>maxpro
)
maxpro
=profit
;
}
}
return maxpro
;
}
Code155
import java
.util
.ArrayList
;
import java
.util
.List
;
import java
.util
.Stack
;
public class MinStack {
Stack
<Integer> stack
;
Stack
<Integer> origin
;
public static void main(String
[] args
) {
MinStack minStack
=new MinStack();
minStack
.push(2);
minStack
.push(0);
minStack
.push(3);
minStack
.push(0);
minStack
.pop();
minStack
.pop();
minStack
.pop();
System
.out
.println(minStack
.getMin());
}
public MinStack() {
stack
=new Stack<Integer>();
origin
=new Stack<Integer>();
}
public void push(int x
) {
origin
.push(x
);
stack
.push(Math
.min(stack
.peek(), x
));
}
public void pop() {
origin
.pop();
stack
.pop();
}
public int top() {
int res
=origin
.peek();
return res
;
}
public int getMin() {
int res
=stack
.peek();
return res
;
}
}
code124 解题思路: 1.想好遍历顺序,我用的是后序遍历。 2.题目要求:一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。所以,一条路径从每个节点出发有3种情况,可以左-中-右、左-中、右-中**;对于左-中-父即为其父亲节点的左-中**,这一点我卡了很久,导致我这道题做了很久。 3.递归体设计:应当设计为经过该点的最大左/右子树的路径长度,而利用全局变量记录最大路径和,当所有的点经过遍历,最大路径即被求出来了。
public class Code124 {
public class TreeNode {
int val
;
TreeNode left
;
TreeNode right
;
TreeNode() {}
TreeNode(int val
) { this.val
= val
; }
TreeNode(int val
, TreeNode left
, TreeNode right
) {
this.val
= val
;
this.left
= left
;
this.right
= right
;
}
}
private int pathSum
=Integer
.MIN_VALUE
;
public int maxPathSum(TreeNode root
) {
resgetMaxSum(root
);
return pathSum
;
}
private int resgetMaxSum(TreeNode root
) {
if(root
==null
) return 0;
else {
int countl
=Math
.max(0, resgetMaxSum(root
.left
));
int countr
=Math
.max(0, resgetMaxSum(root
.right
));
int count
=countl
+countr
+root
.val
;
pathSum
=Math
.max(pathSum
, count
);
return Math
.max(countl
+root
.val
,countr
+root
.val
);
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-17209.html