記事のディレクトリ
[タイトル] 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.二つの数字一緒に
我々は2つの非空リストは二つの非負整数を表すために使用されて示されています。ここで、それぞれのビットは、方法の逆の順序で格納され、それらは各ノードが一つだけ桁を格納することができます。
我々はこれらの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と目標値の目標を考えると、配列内の2つの整数の目標値を特定し、その配列の添字に戻るにお願いします。
あなたは、各入力が一つだけ答えに対応することを想定することができます。ただし、同じ配列要素を再使用することはできません。
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)));
}
}