[题目]字符串S中,只包含字符'A'/'B'/'C'/'D', 求字符串S中的逆序对.
public class ReverseOrder
{
public static void main(String[] args
{
int aa = reverseNums("DBCBBA");
System.out.println(aa);
}
public static int reverseNums(String str)
{
int totalResult = 0;
int totalA = 0, totalB = 0;
int totalC = 0, totalD = 0;
int len = str.length();
int[] nums = new int[len];
for (int i = 0; i < len; i++)
{
switch(str.charAt(i))
{
case ‘D’:
totalD++;
nums[i] = 0;
break;
case ‘C’:
totalC++;
nums[i] = totalD;
break;
case ‘B’:
totalB++;
nums[i] = totalD + totalC;
break;
case ‘A’:
totalA++;
nums[i] = totalD + totalC + totalB;
break;
}
}
for (int i = 0; i < len; i++)
{
totalResult += nums[i];
}
return totalResult;
}
}