将字符串数字中为偶数位的数字进行翻转,将翻转后的结果进行输出。
public class Solution { public String change (String number) { char[] chars = number.toCharArray(); int len = chars.length; LinkedList<Character> temp = new LinkedList<Character>(); for(int i = 0; i < len; i++){ if(chars[i] % 2 == 0){ temp.add(chars[i]); chars[i] = '0'; } } for(int i=0; i < len; i++){ if(chars[i] == '0'){ chars[i] = temp.removeLast(); } } return String.valueOf(chars); } }
public class Solution { /** * * @param number string字符串 * @return string字符串 */ public String change (String number) { // write code here if(number.equals("")) { return number; }//如果是空字符串原样返回 char[] cs = number.toCharArray(); int first=0;//定义头指针 int last=cs.length-1;//定义尾指针 while(first<last) {//循环执行条件,即头指针的位置在尾指针前面,如果在后面说明已经交换完毕 if((cs[first]%2==0) && (cs[last]%2==0)) {//如果两数都是偶数执行交换操作(Ascll码编号与对应数字的奇偶一致) char temp = cs[first]; cs[first]=cs[last]; cs[last]=temp; first++;//头指针后移 last--;//尾指针前移 }else if((cs[first]%2==0) && (cs[last]%2!=0)) {//头指针所在为偶数 last--;//头指针等待,尾指针前移 }else if((cs[first]%2!=0) && (cs[last]%2==0)) { //尾指针所在为偶数 first++;//尾指针等待,头指针后移 }else {//头尾指针都不是偶数 last--;//尾指针前移 first++;//头指针后移 } } String str = new String(cs); return str; } }
class Solution { public: /** * * @param number string字符串 * @return string字符串 */ string change(string number) { bool l=false,r=false; for(int i=0,j=number.length()-1;i<j;){ if(number[i]&1) i++; else l = true; if(number[j]&1) j--; else r = true; if(l && r){ swap(number[i], number[j]); l = r = false; i++; j--; } } return number; } };
import java.util.*; /* 思路:设置双指针,依次从前后进行判断,遇到偶数交换 可能有的情况如下: 左边为奇数,右边为奇数,指针后移,指针前移 左边为奇数,右边为偶数,左指针后移,右指针等待 左边为偶数,右边为奇数,左指针等待,右指针前移 左边,右边都是偶数,数值交换,左右指针都移动 */ public class Solution { /** * * @param number string字符串 * @return string字符串 */ public String change (String number) { // write code here if(number.length()<=1)return number; char[] ch = number.toCharArray(); int left = 0; int right = ch.length-1; while(left < right){ int a = ch[left] - '0'; int b = ch[right] - '0'; if(a%2!=0 && b%2 !=0){left++;right--;} else if(a%2!=0 && b%2==0)left++; else if(a%2==0 && b%2!=0)right--; else if(a%2==0 && b%2==0){ char temp = ch[left]; ch[left] = ch[right]; ch[right] = temp; left++; right--; } } return String.valueOf(ch); } }
//字符串转char数组 char[] numbers = number.toCharArray(); //定义左右指针 int left=0; int right=number.length()-1; //都找到偶数则交换 while (left<right){ //从左边找到第一个偶数 while(left<right&&numbers[left]%2!=0) left++; //从右边找到第一个偶数 while(left<right&&numbers[right]%2!=0) right--; //交换 char temp=numbers[left]; numbers[left]=numbers[right]; numbers[right]=temp; //更新指针 left++; right--; } return String.valueOf(numbers);
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param number string字符串 * @return string字符串 */ public String change (String number) { // write code here char[] s = number.toCharArray(); int head = 0; int tail = s.length-1; while(head<=tail){ if (Integer.valueOf(s[head])%2!=0){ head++; }else if (Integer.valueOf(s[tail])%2!=0){ tail--; }else{ char temp=s[tail]; s[tail] = s[head]; s[head] = temp; head++; tail--; } } return new String(s); } }
方法不难但是写的多
func change(number string) string { if len(number) == 1 { return number } b := []byte(number) i := 0 j := len(b) - 1 fmt.Println(j) for { if i >= j { break } //i<j //fmt.Println(b[i]) //fmt.Println(b[j]) if b[i] == 48 || b[i] == 50 || b[i] == 52 || b[i] == 54 || b[i] == 56 { if b[j] == 48 || b[j] == 50 || b[j] == 52 || b[j] == 54 || b[j] == 56 { //i偶数j偶数 b[i], b[j] = b[j], b[i] i++ j-- } else { //i偶数j奇数 j-- } } if b[i] == 49 || b[i] == 51 || b[i] == 53 || b[i] == 55 || b[i] == 57 { if b[j] == 48 || b[j] == 50 || b[j] == 52 || b[j] == 54 || b[j] == 56 { //i奇数j偶数 i++ } else { //i奇数j奇数 i++ j-- } } } ret := string(b) return ret }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param number string字符串 * @return string字符串 */ public String change (String number) { // write code here char[] c = number.toCharArray(); int p = 0; int q = c.length-1; while(p<q){ while(p<q){ int i = Integer.valueOf(String.valueOf(c[p])); if(i%2==0){ break; }else{ p++; } } while(p<q){ int i = Integer.valueOf(String.valueOf(c[q])); if(i%2==0){ break; }else{ q--; } } char temp = c[p]; c[p] = c[q]; c[q] = temp; p++; q--; } return new String(c); } }
function change( number ) { // write code here let left = 0, right = number.length - 1; let arr = number.split(''); while(left <= right) { if (parseInt(arr[left]) % 2 != 0 ){ left++; } else if (parseInt(arr[right]) % 2 != 0) { right--; } else { [arr[left],arr[right]] = [arr[right],arr[left]]; left++; right--; } } return arr.join(''); } 死在一直在对字符串操作, 而且符合都是偶数后没有对左右指针处理
func change( number string ) string { var left, right = 0, len(number)-1 var nums = []byte(number) for left < right { for left < right && (nums[left]-48)%2!=0 { left++ } for left < right && (nums[right]-48)%2!=0 { right-- } nums[left], nums[right] = nums[right], nums[left] left++ right-- } return string(nums) }
import java.util.*; public class Solution { /** * * @param number string字符串 * @return string字符串 */ public String change (String number) { StringBuilder str = new StringBuilder(); int len = number.length(); int start = 0; int end = len-1; while(start <= len-1){ if(number.charAt(start) % 2 == 0){ while(number.charAt(end) % 2 != 0 && end >= 0){ end--; } str.append(number.charAt(end)); end--; }else{ str.append(number.charAt(start)); } start++; } return str.toString(); } }
class Solution: def change(self , number ): # write code here if len(number)==0: return number i=0 j=len(number)-1 lst = list(map(int, number)) while(i<j): if(j<0&nbs***bsp;i> len(lst)): break while i<j and lst[i]%2 != 0 and i < len(lst): i+=1 while i<j and lst[j]%2 != 0 and j > 0: j-=1 lst[i],lst[j] =lst[j],lst[i] i+=1 j-=1 string = "" for i in lst: string = string+str(i) return string
class Solution { public: /** * * @param number string字符串 * @return string字符串 */ string change(string number) { // write code here int n=number.length(); int l=0; int r=n-1; while(true){ while((number[r]-'0')&1!=0 && r>l) --r; while((number[l]-'0')&1!=0 && l<r) ++l; if(r>l){ swap(number[l],number[r]); --r; ++l; } else break; } return number; } };
import java.util.*; public class Solution { /** * * @param number string字符串 * @return string字符串 */ public String change (String number) { // write code here int n = number.length(); int i = 0; int j = n - 1; char[] a = number.toCharArray(); while (i < j) { while (i < j && a[i] % 2 != 0) {//Ascll码编号与对应数字的奇偶一致 i++; } while (i < j && a[j] % 2 != 0) { j--; } char temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; } return new String(a); } }
class Solution: def change(self , number ): # write code here tmp = list(number) ji = [i for i in tmp if int(i)%2==1] # 获取奇数 ou = [i for i in tmp if int(i)%2==0][::-1] # 获取偶数 res = '' k1, k2 = 0, 0 for i in tmp: if k1<len(ji) and i == ji[k1]: # 判断奇数 res += i k1 += 1 elif k2<len(ou): #否则偶数 res += ou[k2] k2 += 1 return res