发现不整理,老是断,决定开一篇来整理下,主要包括力扣和SQL题;
力扣
简单
package com.leetcode.wyk;
import com.alibaba.fastjson.JSON;
public class questionAndAnswer {
package com.leetcode.wyk;
import com.alibaba.fastjson.JSON;
public class questionAndAnswer {
//简单
/** 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
*/
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length;i++) {
for(int j=i+1;j<nums.length;j++) {
if(nums[i]+nums[j]==target) {
return new int[] {i,j};
}
}
}
return null;
}
/**
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
*/
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode curr=head;
while(curr!=null) {
ListNode next=curr.next;
curr.next=pre;
pre=curr;
curr=next;
}
return pre;
}
/**
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4],
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
*/
public int maxSubArray(int[] nums) {
int sum=0;
int res=nums[0];
for(int i=0;i<nums.length;i++) {
if(sum>0) {
sum=sum+nums[i];
}else {
sum=nums[i];
}
if(res<sum) {
res=sum;
}
}
return res;
}
public static void main(String args[]) {
questionAndAnswer qa= new questionAndAnswer();
//两数之和
System.out.println(JSON.toJSON(qa.twoSum(new int[]{1, 2, 3, 4, 5}, 6)));
}
}