【题目】459
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
【思路】
整体思路:当前字符串去掉头字母加上当前字符串去掉尾字母,然后比较新的大字符串是否包含原来的那个字符串
【代码】
public class IsRepetedSubString {
//整体思路:当前字符串去掉头字母加上当前字符串去掉尾字母,然后比较新的大字符串是否包含原来的那个字符串
public static boolean repeatedSubstringPattern(String s){
StringBuffer stringBuffer = new StringBuffer(s.substring(1,s.length()));
stringBuffer.append(s.substring(0,s.length() - 1));
if(stringBuffer.toString().contains(s)){
return true;
}
return false;
}
public static void main(String[] args) {
String s = "abcabc";
System.out.println(repeatedSubstringPattern(s));
}
}
【结果】
2. 两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
package practice_FactorialAndDp;
import java.util.List;
import java.util.Stack;
/**
* @Author: Next
* @Date: 2020/2/10
**/
public class LeetCode_02_addTwoNumber {
public static class ListNode{
public int val;
public ListNode next;
public ListNode(int data){
this.val = data;
}
}
public static ListNode addTwoNumber(ListNode node1, ListNode node2){
Stack<Integer> stack = new Stack<>();
while (node1 != null){
stack.push(node1.val);
node1 = node1.next;
}
StringBuffer sb = new StringBuffer();
while (!stack.isEmpty()){
sb.append(stack.pop());
}
int num1 = 0;
if(sb.length() <= 9){
num1 = Integer.parseInt(sb.toString().trim());
}
sb.delete(0,sb.length());
while (node2 != null){
stack.push(node2.val);
node2 = node2.next;
}
while (!stack.isEmpty()){
sb.append(stack.pop());
}
int num2 = 0;
if(sb.length() <= 9){
num2 = Integer.parseInt(sb.toString().trim());
}
sb.delete(0,sb.length());
int resNum = num1 + num2;
sb = new StringBuffer(resNum + "");
String resStr = sb.reverse().toString();
char[] chs = resStr.toCharArray();
int[] resNums = new int[chs.length];
for(int i = 0;i < chs.length;i++){
if(Integer.parseInt(String.valueOf(chs[i])) < Integer.MAX_VALUE){
resNums[i] = Integer.parseInt(String.valueOf(chs[i]));
}
}
ListNode res = new ListNode(resNums[0]);//将数组中的数据转移到链表中去
ListNode help = res;//生成另一个节点,并让help指向res节点,help在此作为一个临时变量,help和res指向同一地址
for(int i = 1;i < resNums.length;i++){//由于已给res赋值,所以i从1开始
ListNode temp = new ListNode(resNums[i]);//每循环一次生成一个新的节点,并给当前节点赋值
help.next = temp;//将help的下一个节点指向生成的新的节点
help = temp;//将help指向最后一个节点(help的下一个节点)
}
printNode(help);
System.out.println("==============================");
return res;
}
// public static ListNode constructNode(int[] resNums,int i){
// if(i == resNums.length){
//
// }
// ListNode res = new ListNode(resNums[])
// }
public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
ListNode resHead = new ListNode(0);
ListNode help1 = l1;
ListNode help2 = l2;
ListNode cur = resHead;
int carry = 0;
while (help1 != null || help2 != null){
int h1 = (help1 != null) ? help1.val : 0;
int h2 = (help2 != null) ? help2.val : 0;
int sum = h1 + h2 + carry;
carry = sum / 10;
cur.next = new ListNode(sum % 10);
cur = cur.next;
if(help1 != null){
help1 = help1.next;
}
if(help2 != null){
help2 = help2.next;
}
}
if(carry > 0){
cur.next = new ListNode(carry);
}
return resHead.next;
}
public static void printNode(ListNode node){
while (node != null){
System.out.print(node.val + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
ListNode node1 = new ListNode(2);
node1.next = new ListNode(4);
node1.next.next = new ListNode(3);
// ListNode node1 = new ListNode(9);
printNode(node1);
System.out.println("===========================");
ListNode node2 = new ListNode(5);
node2.next = new ListNode(6);
node2.next.next = new ListNode(4);
// ListNode node2 = new ListNode(1);
// node2.next = new ListNode(9);
// node2.next.next = new ListNode(9);
// node2.next.next.next = new ListNode(9);
// node2.next.next.next.next = new ListNode(9);
// node2.next.next.next.next.next = new ListNode(9);
// node2.next.next.next.next.next.next = new ListNode(9);
// node2.next.next.next.next.next.next.next = new ListNode(9);
// node2.next.next.next.next.next.next.next.next = new ListNode(9);
// node2.next.next.next.next.next.next.next.next.next = new ListNode(9);
printNode(node2);
System.out.println("===========================");
// ListNode res = addTwoNumber(node1,node2);
ListNode res = addTwoNumbers(node1,node2);
printNode(res);
}
}
3.两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
package practice_FactorialAndDp;
import java.util.Arrays;
import java.util.HashMap;
/**
* @Author: Next
* @Date: 2020/2/10
**/
public class LeetCode_01_TwoSum {
public static int[] twoSum(int[] nums,int target){
if(nums == null || nums.length < 1){
return null;
}
for(int i = 0;i < nums.length;i++){
for(int j = i + 1;j < nums.length;j++){
if(nums[j] == target - nums[i]){
return new int[]{i,j};
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
public static int[] twoSum2(int[] nums,int target){
if(nums == null || nums.length < 1){
return null;
}
int i = 0;
HashMap<Integer,Integer> map_numI = new HashMap<>();
while(i < nums.length){
map_numI.put(nums[i],i++);
}
for(i = 0;i < nums.length;i++){
if (map_numI.containsKey(target - nums[i]) && (map_numI.get(target - nums[i]) != i)){
return new int[]{i, map_numI.get(target - nums[i])};
}
}
return null;
}
public static int[] twoSum3(int[] nums,int target){
if(nums == null || nums.length < 1){
return null;
}
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0;i < nums.length;i++){
int complement = target - nums[i];
if(map.containsKey(complement)){
return new int[]{map.get(complement),i};
}
map.put(nums[i],i);
}
return null;
}
public static void main(String[] args) {
int[] nums = {3,2,4};
int target = 6;
System.out.println(Arrays.toString(twoSum(nums,target)));
System.out.println(Arrays.toString(twoSum2(nums,target)));
System.out.println(Arrays.toString(twoSum3(nums,target)));
}
}