1.字符串S和 T 只包含小写字符。在S中,所有字符只会出现一次。
S 已经根据某种规则进行了排序。我们要根据S中的字符顺序对T进行排序。更具体地说,如果S中x在y之前出现,那么返回的字符串中x也应出现在y之前。
返回任意一种符合条件的字符串T。
示例:
输入:
S = "cba"
T = "abcd"
输出: "cbad"
解释:
S中出现了字符 "a", "b", "c", 所以 "a", "b", "c" 的顺序应该是 "c", "b", "a".
由于 "d" 没有在S中出现, 它可以放在T的任意位置. "dcba", "cdba", "cbda" 都是合法的输出。
class Solution {
public String customSortString(String S, String T) {
//将出入的S,T转换为数组
char []s = S.toCharArray();
char []t = T.toCharArray();
int index = 0;
//循环遍历数组中的
for(int i = 0; i < s.length; i ++) {
for(int j = index ; j < t.length; j ++) {
if(s[i] == t[j]) {
char temp = t[index];
t[index] = t[j];
t[j] = temp;
index ++;
}
}
}
return new String(t);
}
}
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例 1:
输入: “Let’s take LeetCode contest”
输出: “s’teL ekat edoCteeL tsetnoc”
class Solution {
public String reverseWords(String s) {
String[] words = s.split(" ");
StringBuilder sb = new StringBuilder();
int len = words.length;
if (words.length == 1) {
return reverseString(s);
}
for (int i = 0; i < len - 1; i++) {
sb.append(reverseString(words[i]) + " ");
}
sb.append(reverseString(words[len - 1]));
return sb.toString();
}
public String reverseString(String s) {
char[] chars = s.toCharArray();
int i = 0;
int j = chars.length - 1;
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
return new String(chars);
}
}
给定一个仅包含数字 0-9 的字符串和一个目标值,在数字之间添加二元运算符(不是一元)+、- 或 * ,返回所有能够得到目标值的表达式。
示例 1:
输入: num = “123”, target = 6
输出: [“1+2+3”, “123”]
示例 2:
输入: num = “232”, target = 8
输出: [“23+2", "2+32”]
示例 3:
输入: num = “105”, target = 5
输出: [“1*0+5”,“10-5”]
示例 4:
输入: num = “00”, target = 0
输出: [“0+0”, “0-0”, “0*0”]
class Solution {
List<String> res;
public List<String> addOperators(String num, int target) {
res=new ArrayList<String>();
search(num,target,"",(long) 0,(long)0);
return res;
}
/***
* 递归的时候,要想好需要什么样的参数,什么时候返回
* @param num 初始集合
* @param target 目标值
* @param tmp 当前集
* @param cures 当前结果
* @param prenum 上一次的结果,主要是为了用于乘法计算,因为优先级最高
*/
public void search(String num,int target, String tmp,Long cures,long prenum)
{
if(target==cures&&num.length()==0)
{
res.add(new String(tmp));
return ;
}
for(int i=1;i<=num.length();i++)
{
String cur=num.substring(0, i);
/**去除掉00X的结果,但是允许单个0*/
if(cur.length()>1&&cur.charAt(0)=='0')
return;
/**因为有可能越界,所以使用long*/
long curnum=Long.parseLong(cur);
String sub=num.substring(i);
/**第一个数的话不用添加符号*/
if(tmp.length()==0)
{
search(sub,target,cur,curnum,curnum);
}
else
{
/**加法*/
search(sub,target,tmp+"+"+curnum,cures+curnum,curnum);
/**减法,注意当前结果值是-curnum*/
search(sub,target,tmp+"-"+curnum,cures-curnum,-curnum);
/**乘法*/
search(sub,target,tmp+"*"+curnum,(cures-prenum)+prenum*curnum,prenum*curnum);
}
}
}
}
4.给定一串链表,只保留没有重复出现的节点
与下一题结合看
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
//baseCase
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
//如果是这种情况
// 1 --> 1 --> 1 --> 2 --> 3
// head next
//1.则需要移动next直到出现与当前head.value不相等的情况(含null)
//2.并且此时的head已经不能要了,因为已经head是重复的节点
//--------------else-------------
// 1 --> 2 --> 3
// head next
//3.如果没有出现1的情况,则递归返回的节点就作为head的子节点
if (head.val == next.val) {
//1
while (next != null && head.val == next.val) {
next = next.next;
}
//2
head = deleteDuplicates(next);
} else {
//3
head.next = deleteDuplicates(next);
}
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
//baseCase
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
if (head.val == next.val) {
while (next != null && head.val == next.val) {
//删除节点
next = next.next;
}
//递归使每个节点都变成head节点保留头节点值,从head.next开始,若从head开始,则不保留出现重复节点的值
head.next = deleteDuplicates(next);
} else {
head.next = deleteDuplicates(next);
}
return head;
}
}